Files
marketplaces/src/app/app.ts

99 lines
3.2 KiB
TypeScript
Raw Normal View History

2026-01-18 18:57:06 +04:00
2026-02-19 01:23:25 +04:00
import { Component, OnInit, signal, ApplicationRef, inject, DestroyRef } from '@angular/core';
2026-02-14 01:28:08 +04:00
import { Router, RouterOutlet, NavigationEnd } from '@angular/router';
2026-01-18 18:57:06 +04:00
import { Title } from '@angular/platform-browser';
import { HeaderComponent } from './components/header/header.component';
import { FooterComponent } from './components/footer/footer.component';
2026-02-14 01:28:08 +04:00
import { BackButtonComponent } from './components/back-button/back-button.component';
2026-01-18 18:57:06 +04:00
import { ApiService } from './services';
2026-02-19 01:23:25 +04:00
import { interval, concat } from 'rxjs';
2026-02-14 01:28:08 +04:00
import { filter, first } from 'rxjs/operators';
2026-02-19 01:23:25 +04:00
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
2026-01-18 18:57:06 +04:00
import { environment } from '../environments/environment';
import { SwUpdate } from '@angular/service-worker';
@Component({
selector: 'app-root',
2026-02-19 01:23:25 +04:00
imports: [RouterOutlet, HeaderComponent, FooterComponent, BackButtonComponent],
2026-01-18 18:57:06 +04:00
templateUrl: './app.html',
styleUrl: './app.scss'
})
2026-02-19 01:23:25 +04:00
export class App implements OnInit {
2026-01-18 18:57:06 +04:00
protected title = environment.brandName;
2026-02-14 01:28:08 +04:00
isHomePage = signal(true);
2026-02-19 01:23:25 +04:00
checkingServer = signal(true);
serverAvailable = signal(false);
2026-01-18 18:57:06 +04:00
2026-02-19 01:23:25 +04:00
private destroyRef = inject(DestroyRef);
private apiService = inject(ApiService);
private titleService = inject(Title);
private swUpdate = inject(SwUpdate);
private appRef = inject(ApplicationRef);
private router = inject(Router);
2026-01-18 18:57:06 +04:00
ngOnInit(): void {
this.titleService.setTitle(`${environment.brandFullName} - Маркетплейс товаров и услуг`);
this.checkServerHealth();
this.setupAutoUpdates();
2026-02-14 01:28:08 +04:00
// Track route changes to show/hide back button
2026-02-19 01:23:25 +04:00
this.router.events
.pipe(
filter(event => event instanceof NavigationEnd),
takeUntilDestroyed(this.destroyRef)
)
2026-02-14 01:28:08 +04:00
.subscribe((event) => {
const url = (event as NavigationEnd).urlAfterRedirects || (event as NavigationEnd).url;
this.isHomePage.set(url === '/' || url === '/home' || url === '');
});
2026-01-18 18:57:06 +04:00
}
2026-02-19 01:23:25 +04:00
private checkServerHealth(): void {
this.checkingServer.set(true);
this.apiService.ping()
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe({
next: () => {
this.serverAvailable.set(true);
this.checkingServer.set(false);
},
error: () => {
this.serverAvailable.set(false);
this.checkingServer.set(false);
}
});
}
retryConnection(): void {
this.checkServerHealth();
2026-01-18 18:57:06 +04:00
}
2026-02-19 01:23:25 +04:00
private setupAutoUpdates(): void {
2026-01-18 18:57:06 +04:00
if (!this.swUpdate.isEnabled) {
return;
}
const appIsStable$ = this.appRef.isStable.pipe(first(isStable => isStable === true));
const every6Hours$ = interval(6 * 60 * 60 * 1000);
const checkInterval$ = concat(appIsStable$, every6Hours$);
2026-02-19 01:23:25 +04:00
checkInterval$
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(async () => {
try {
await this.swUpdate.checkForUpdate();
} catch (err) {
console.error('Update check failed:', err);
}
});
2026-01-18 18:57:06 +04:00
2026-02-19 01:23:25 +04:00
this.swUpdate.versionUpdates
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(event => {
if (event.type === 'VERSION_READY') {
console.log('New app version ready');
}
});
2026-01-18 18:57:06 +04:00
}
}