add logic

This commit is contained in:
2026-10-07 17:32:25 +03:00
parent cdcdd22526
commit f86777e2cd
69 changed files with 1519 additions and 637 deletions
+212 -18
View File
@@ -1,5 +1,12 @@
import { createFeature, createReducer, on } from '@ngrx/store';
import {
areaAttractions,
type AreaDetails,
type AreaProgress,
type Attraction,
type AttractionDetails,
} from '../areas/area.models';
import { readAuthSession } from '../auth/auth-session';
import { GorodarActions } from './gorodar.actions';
import { RESEND_SECONDS, sessionToAuth, type AuthState, type ClaimState, type GorodarState } from './gorodar.models';
@@ -8,7 +15,10 @@ const initialClaim: ClaimState = {
landmarkId: null,
photo: null,
stage: 'idle',
step: 1,
step: 0,
failure: null,
confidence: null,
alreadyVisited: false,
};
const blankAuth: AuthState = {
@@ -46,6 +56,15 @@ export const initialState: GorodarState = {
phone: '+7 900 123-45-67',
joinedLabel: 'Сентябрь 2026',
},
areas: [],
areasStatus: 'loading',
areasError: '',
areaDetails: {},
areaDetailStatus: {},
areaDetailError: {},
attractions: {},
attractionStatus: {},
attractionError: {},
};
const gorodarReducer = createReducer(
@@ -54,27 +73,19 @@ const gorodarReducer = createReducer(
GorodarActions.photoCaptured,
(state, { landmarkId, photo }): GorodarState => ({
...state,
claim: {
landmarkId,
photo,
stage: 'check',
step: 1,
},
claim: { ...initialClaim, landmarkId, photo, stage: 'check', step: 1 },
}),
),
on(
GorodarActions.claimStepChanged,
(state, { step }): GorodarState => ({
...state,
claim: {
...state.claim,
step,
},
claim: { ...state.claim, step },
}),
),
on(
GorodarActions.claimCompleted,
(state, { landmarkId, photo, collectedAt }): GorodarState => ({
(state, { landmarkId, photo, collectedAt, alreadyVisited, progress }): GorodarState => ({
...state,
collectedIds: state.collectedIds.includes(landmarkId)
? state.collectedIds
@@ -87,12 +98,16 @@ const gorodarReducer = createReducer(
...state.collectedAt,
[landmarkId]: collectedAt,
},
claim: {
landmarkId,
photo,
stage: 'success',
step: 3,
},
areaDetails: markAreaVisited(state.areaDetails, landmarkId, progress),
attractions: markAttractionVisited(state.attractions, landmarkId),
claim: { ...initialClaim, landmarkId, photo, stage: 'success', step: 3, alreadyVisited },
}),
),
on(
GorodarActions.claimFailed,
(state, { failure, confidence }): GorodarState => ({
...state,
claim: { ...state.claim, stage: 'failed', failure, confidence },
}),
),
on(
@@ -253,15 +268,194 @@ const gorodarReducer = createReducer(
GorodarActions.logout,
(state): GorodarState => ({
...state,
collectedIds: [],
photos: {},
collectedAt: {},
claim: initialClaim,
auth: blankAuth,
profile: {
...state.profile,
email: '',
},
areaDetails: {},
areaDetailStatus: {},
areaDetailError: {},
attractions: {},
attractionStatus: {},
attractionError: {},
}),
),
on(
GorodarActions.areasRequested,
(state): GorodarState => ({
...state,
areasStatus: 'loading',
areasError: '',
}),
),
on(
GorodarActions.areasLoaded,
(state, { areas }): GorodarState => ({
...state,
areas,
areasStatus: 'ready',
areasError: '',
}),
),
on(
GorodarActions.areasFailed,
(state, { error }): GorodarState => ({
...state,
areasStatus: 'error',
areasError: error,
}),
),
on(
GorodarActions.areaRequested,
(state, { id }): GorodarState => ({
...state,
areaDetailStatus: {
...state.areaDetailStatus,
[id]: 'loading',
},
areaDetailError: {
...state.areaDetailError,
[id]: '',
},
}),
),
on(
GorodarActions.areaLoaded,
(state, { area }): GorodarState => ({
...state,
areaDetails: {
...state.areaDetails,
[area.id]: area,
},
areaDetailStatus: {
...state.areaDetailStatus,
[area.id]: 'ready',
},
areaDetailError: {
...state.areaDetailError,
[area.id]: '',
},
}),
),
on(
GorodarActions.areaFailed,
(state, { id, error }): GorodarState => ({
...state,
areaDetailStatus: {
...state.areaDetailStatus,
[id]: 'error',
},
areaDetailError: {
...state.areaDetailError,
[id]: error,
},
}),
),
on(
GorodarActions.attractionRequested,
(state, { id }): GorodarState => ({
...state,
attractionStatus: {
...state.attractionStatus,
[id]: 'loading',
},
attractionError: {
...state.attractionError,
[id]: '',
},
}),
),
on(
GorodarActions.attractionLoaded,
(state, { attraction }): GorodarState => ({
...state,
attractions: {
...state.attractions,
[attraction.id]: attraction,
},
attractionStatus: {
...state.attractionStatus,
[attraction.id]: 'ready',
},
attractionError: {
...state.attractionError,
[attraction.id]: '',
},
}),
),
on(
GorodarActions.attractionFailed,
(state, { id, error }): GorodarState => ({
...state,
attractionStatus: {
...state.attractionStatus,
[id]: 'error',
},
attractionError: {
...state.attractionError,
[id]: error,
},
}),
),
);
function markVisited(item: Attraction, id: string): Attraction {
return item.id === id ? { ...item, visited: true } : item;
}
function markAreaVisited(
details: Record<string, AreaDetails>,
landmarkId: string,
progress: AreaProgress,
): Record<string, AreaDetails> {
return Object.fromEntries(
Object.entries(details).map(([id, area]) => {
const contains = areaAttractions(area).some((item) => item.id === landmarkId);
if (!contains) {
return [id, area];
}
return [
id,
{
...area,
progress: progress ?? area.progress,
attractions: area.attractions.map((item) => markVisited(item, landmarkId)),
children: area.children.map((child) => ({
...child,
attractions: child.attractions.map((item) => markVisited(item, landmarkId)),
})),
},
];
}),
);
}
function markAttractionVisited(
attractions: Record<string, AttractionDetails>,
landmarkId: string,
): Record<string, AttractionDetails> {
const current = attractions[landmarkId];
if (!current) {
return attractions;
}
return {
...attractions,
[landmarkId]: {
...current,
visited: true,
},
};
}
export const gorodarFeature = createFeature({
name: 'gorodar',
reducer: gorodarReducer,