Files
front/src/app/pages/map-item-page/map-item-page.ts
T
2026-10-08 14:39:17 +03:00

134 lines
4.1 KiB
TypeScript

import { Location } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
computed,
effect,
HostListener,
inject,
input,
signal,
} from '@angular/core';
import { MatButton } from '@angular/material/button';
import { Router } from '@angular/router';
import { Store } from '@ngrx/store';
import { areaOpenedCount, areaTotalCount, type AreaDetails } from '../../areas/area.models';
import { ClaimFlow } from '../../claim/claim-flow/claim-flow';
import { AudioGuide } from '../../shared/audio-guide/audio-guide';
import { Icon } from '../../shared/icon/icon';
import {
GorodarActions,
selectAreaDetails,
selectAreaDetailStatus,
selectAttractionError,
selectAttractions,
selectAttractionStatus,
selectAuthenticated,
selectClaim,
selectCollectedAt,
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 store = inject(Store);
private readonly location = inject(Location);
private readonly router = inject(Router);
private readonly authenticated = this.store.selectSignal(selectAuthenticated);
private readonly details = this.store.selectSignal(selectAreaDetails);
private readonly detailStatus = this.store.selectSignal(selectAreaDetailStatus);
private readonly attractions = this.store.selectSignal(selectAttractions);
private readonly attractionStatuses = this.store.selectSignal(selectAttractionStatus);
private readonly attractionErrors = this.store.selectSignal(selectAttractionError);
readonly areaId = input.required<string>();
readonly id = input.required<string>();
protected readonly status = computed(() => this.attractionStatuses()[this.id()] ?? 'loading');
protected readonly error = computed(() => this.attractionErrors()[this.id()] ?? '');
protected readonly area = computed(
(): AreaDetails | null => this.details()[this.areaId()] ?? null,
);
protected readonly landmark = computed(() => this.attractions()[this.id()] ?? null);
protected readonly photos = this.store.selectSignal(selectPhotos);
protected readonly collectedAtMap = this.store.selectSignal(selectCollectedAt);
protected readonly claim = this.store.selectSignal(selectClaim);
protected readonly openedCount = computed(() => {
const area = this.area();
return area ? areaOpenedCount(area) : 0;
});
protected readonly totalCount = computed(() => {
const area = this.area();
return area ? areaTotalCount(area) : 0;
});
protected readonly collected = computed(() => this.landmark()?.visited ?? false);
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';
});
constructor() {
effect(() => {
const areaId = this.areaId();
const status = this.detailStatus()[areaId];
if (this.details()[areaId] || status === 'loading' || status === 'ready') {
return;
}
this.store.dispatch(GorodarActions.areaRequested({ id: areaId }));
});
effect(() => {
this.store.dispatch(GorodarActions.attractionRequested({ id: this.id() }));
});
}
protected goBack(): void {
if (window.history.length > 1) {
this.location.back();
return;
}
void this.router.navigate(['/city', this.areaId()]);
}
protected claimItem(): void {
if (this.authenticated()) {
this.store.dispatch(GorodarActions.takePhoto({ landmarkId: this.id() }));
return;
}
this.store.dispatch(GorodarActions.authSheetOpened({ intent: 'claim', landmarkId: this.id() }));
}
}