import { Location } from '@angular/common'; import { ChangeDetectionStrategy, Component, computed, HostListener, inject, input, signal, } from '@angular/core'; import { MatButton } from '@angular/material/button'; import { Router } from '@angular/router'; import { Store } from '@ngrx/store'; import { AuthPromptService } from '../../auth/auth-prompt.service'; import { ClaimFlow } from '../../claim/claim-flow/claim-flow'; import { CityCatalogService } from '../../root/services/city-catalog.service'; import { AudioGuide } from '../../shared/audio-guide/audio-guide'; import { Icon } from '../../shared/icon/icon'; import { GorodarActions, selectClaim, selectCollectedAt, selectCollectedIdSet, selectPhotos, } from '../../state'; @Component({ selector: 'app-map-item-page', imports: [MatButton, Icon, ClaimFlow, AudioGuide], templateUrl: './map-item-page.html', styleUrl: './map-item-page.scss', changeDetection: ChangeDetectionStrategy.OnPush, }) export class MapItemPage { private readonly catalog = inject(CityCatalogService); private readonly store = inject(Store); private readonly location = inject(Location); private readonly router = inject(Router); private readonly authPrompt = inject(AuthPromptService); readonly id = input.required(); protected readonly city = this.catalog.city; protected readonly landmark = computed(() => this.catalog.landmarkById(this.id())); protected readonly collectedIds = this.store.selectSignal(selectCollectedIdSet); protected readonly photos = this.store.selectSignal(selectPhotos); protected readonly collectedAtMap = this.store.selectSignal(selectCollectedAt); protected readonly claim = this.store.selectSignal(selectClaim); protected readonly collected = computed(() => { const item = this.landmark(); return item ? this.collectedIds().has(item.id) || item.collected : false; }); protected readonly userPhoto = computed(() => { const item = this.landmark(); return item ? (this.photos()[item.id] ?? null) : null; }); protected readonly collectedLabel = computed(() => { if (!this.collected()) { return 'Не получен'; } const item = this.landmark(); const at = item ? this.collectedAtMap()[item.id] : undefined; if (!at) { return 'Получен'; } const formatted = new Intl.DateTimeFormat('ru-RU', { day: 'numeric', month: 'long', }).format(new Date(at)); return `Получено · ${formatted}`; }); protected readonly claiming = computed(() => { const claim = this.claim(); return claim.landmarkId === this.id() && claim.stage !== 'idle'; }); protected readonly photoOpen = signal(false); protected goBack(): void { if (window.history.length > 1) { this.location.back(); return; } void this.router.navigateByUrl('/city'); } protected claimItem(): void { void this.authPrompt.prompt('claim').then((result) => { if (result !== 'authenticated') { return; } this.store.dispatch(GorodarActions.takePhoto({ landmarkId: this.id() })); }); } protected openPhoto(): void { if (this.userPhoto()) { this.photoOpen.set(true); } } protected closePhoto(): void { this.photoOpen.set(false); } @HostListener('document:keydown.escape') protected onEscape(): void { this.closePhoto(); } }