Files
marketplaces/src/app/pages/static-page/static-page.component.ts

110 lines
3.7 KiB
TypeScript
Raw Normal View History

2026-07-10 13:52:01 +04:00
import { ChangeDetectionStrategy, Component, DestroyRef, SecurityContext, effect, inject, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ActivatedRoute, RouterLink } from '@angular/router';
2026-07-10 13:52:01 +04:00
import { DomSanitizer, Meta, SafeHtml, Title } from '@angular/platform-browser';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { StaticPageResolverService } from '../../core/config/static-page-resolver.service';
import { LanguageService } from '../../services/language.service';
import { TranslatePipe } from '../../i18n/translate.pipe';
@Component({
selector: 'app-static-page',
standalone: true,
imports: [CommonModule, RouterLink, TranslatePipe],
templateUrl: './static-page.component.html',
styleUrls: ['./static-page.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class StaticPageComponent {
private readonly route = inject(ActivatedRoute);
2026-07-10 13:52:01 +04:00
private readonly destroyRef = inject(DestroyRef);
private readonly sanitizer = inject(DomSanitizer);
private readonly staticPageResolver = inject(StaticPageResolverService);
private readonly languageService = inject(LanguageService);
2026-07-10 13:52:01 +04:00
private readonly titleService = inject(Title);
private readonly meta = inject(Meta);
readonly loading = signal(true);
readonly notFound = signal(false);
readonly title = signal('');
readonly homeRoute = signal('');
2026-07-10 13:52:01 +04:00
readonly dir = signal<'ltr' | 'rtl'>('ltr');
readonly safeHtml = signal<SafeHtml>(this.sanitizer.bypassSecurityTrustHtml(''));
2026-07-10 13:52:01 +04:00
private lastKey: string | null = null;
private lastPath: string | null = null;
constructor() {
2026-07-10 13:52:01 +04:00
effect(() => {
const lang = this.languageService.currentLanguage();
this.homeRoute.set(`/${lang}`);
this.dir.set(['ar', 'fa', 'he', 'ur'].includes(lang) ? 'rtl' : 'ltr');
if (this.lastKey) {
this.loadByKey(this.lastKey);
} else if (this.lastPath) {
this.loadByPath(this.lastPath);
}
});
2026-07-10 13:52:01 +04:00
this.route.paramMap.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(params => {
const keyFromParam = params.get('key');
const staticPath = params.get('staticPath');
if (keyFromParam) {
2026-07-10 13:52:01 +04:00
this.lastKey = keyFromParam;
this.lastPath = null;
this.loadByKey(keyFromParam);
return;
}
if (staticPath) {
2026-07-10 13:52:01 +04:00
this.lastKey = null;
this.lastPath = `/${staticPath}`;
this.loadByPath(`/${staticPath}`);
return;
}
this.notFound.set(true);
this.loading.set(false);
});
}
private loadByKey(key: string): void {
this.loading.set(true);
this.notFound.set(false);
this.staticPageResolver.resolveByKey(key, this.languageService.currentLanguage()).subscribe(page => {
this.applyPage(page?.title ?? '', page?.html ?? '', !page);
});
}
private loadByPath(path: string): void {
this.loading.set(true);
this.notFound.set(false);
this.staticPageResolver.resolveByRoute(path, this.languageService.currentLanguage()).subscribe(page => {
this.applyPage(page?.title ?? '', page?.html ?? '', !page);
});
}
private applyPage(title: string, html: string, notFound: boolean): void {
if (notFound) {
this.title.set('');
this.safeHtml.set(this.sanitizer.bypassSecurityTrustHtml(''));
this.notFound.set(true);
this.loading.set(false);
2026-07-10 13:52:01 +04:00
this.titleService.setTitle('404');
return;
}
const sanitized = this.sanitizer.sanitize(SecurityContext.HTML, html) ?? '';
this.title.set(title);
this.safeHtml.set(this.sanitizer.bypassSecurityTrustHtml(sanitized));
this.notFound.set(false);
this.loading.set(false);
2026-07-10 13:52:01 +04:00
this.titleService.setTitle(title);
this.meta.updateTag({ name: 'description', content: title });
}
}