import { Location } from '@angular/common'; import { ChangeDetectionStrategy, Component, computed, HostListener, inject, input, signal } from '@angular/core'; import { Router, RouterLink } from '@angular/router'; import { MatButton } from '@angular/material/button'; import { Store } from '@ngrx/store'; import { ClaimFlow } from '../../claim/claim-flow/claim-flow'; import { CityCatalogService } from '../../root/services/city-catalog.service'; import { Icon } from '../../shared/icon/icon'; import { GorodarActions, selectClaim, selectCollectedAt, selectCollectedIdSet, selectPhotos, } from '../../state'; @Component({ selector: 'app-map-item-page', imports: [MatButton, Icon, ClaimFlow, RouterLink], 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); 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 photoExamples = [ { label: 'Днём', src: '/assets/illustrations/photo-day.svg' }, { label: 'Вечером', src: '/assets/illustrations/photo-evening.svg' }, { label: 'Зимой', src: '/assets/illustrations/photo-winter.svg' }, ]; protected readonly photoOpen = signal(false); protected goBack(): void { if (window.history.length > 1) { this.location.back(); return; } void this.router.navigateByUrl('/map'); } protected claimItem(): void { 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(); } }