mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 15:17:30 +00:00
fix(atlas): keep tour exclusions right when their answers come back out of order
Each exclusion replaced the whole list with its answer, so two quick exclusions answered in the other order, or a read answered after an exclusion landed, put back a tour as counted although the server held it excluded. Only the answer to the latest request is now taken whole, with the exclusions still on their way laid over it; an older answer only confirms its own tour.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// FE-STA2-HOOK-001 to FE-STA2-HOOK-007
|
||||
// FE-STA2-HOOK-001 to FE-STA2-HOOK-009
|
||||
// STA-02: the user's own tour activities are read when the tab is first shown,
|
||||
// narrowed by period and type, and a tour left out shows at once and comes back
|
||||
// with a message when the server refuses.
|
||||
@@ -153,4 +153,74 @@ describe('useTourActivityStats', () => {
|
||||
expect(result.current.data).toBeNull();
|
||||
expect(toast.error).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('FE-STA2-HOOK-008: an older exclusion answered last never puts a newer one back', async () => {
|
||||
const answers: Array<(value: TourActivityStatsResponse) => void> = [];
|
||||
repo.setExcluded.mockImplementation(() => new Promise((resolve) => answers.push(resolve)));
|
||||
const { result } = renderHook(() => useTourActivityStats(true));
|
||||
await waitFor(() => expect(result.current.status).toBe('ready'));
|
||||
const excludedOf = (placeId: number) =>
|
||||
result.current.data?.entries.find((item) => item.place_id === placeId)?.excluded;
|
||||
const withExcluded = (...ids: number[]) => ({
|
||||
...stats,
|
||||
entries: stats.entries.map((item) => ({ ...item, excluded: ids.includes(item.place_id) })),
|
||||
});
|
||||
|
||||
let first: Promise<void> = Promise.resolve();
|
||||
let second: Promise<void> = Promise.resolve();
|
||||
act(() => {
|
||||
first = result.current.setExcluded(entry(), true);
|
||||
});
|
||||
act(() => {
|
||||
second = result.current.setExcluded(stats.entries[1] as TourActivityStatsEntry, true);
|
||||
});
|
||||
// The second answer comes first and carries both; the first, from before the second, only its own.
|
||||
await act(async () => {
|
||||
answers[1]?.(withExcluded(1, 2));
|
||||
await second;
|
||||
});
|
||||
await act(async () => {
|
||||
answers[0]?.(withExcluded(1));
|
||||
await first;
|
||||
});
|
||||
expect(excludedOf(1)).toBe(true);
|
||||
expect(excludedOf(2)).toBe(true);
|
||||
|
||||
// The other way round: the first comes first, without the second still on its way, which stays left out.
|
||||
act(() => {
|
||||
first = result.current.setExcluded(entry(), false);
|
||||
});
|
||||
act(() => {
|
||||
second = result.current.setExcluded(stats.entries[1] as TourActivityStatsEntry, false);
|
||||
});
|
||||
await act(async () => {
|
||||
answers[2]?.(withExcluded(2));
|
||||
await first;
|
||||
});
|
||||
expect(excludedOf(1)).toBe(false);
|
||||
expect(excludedOf(2)).toBe(false);
|
||||
await act(async () => {
|
||||
answers[3]?.(withExcluded());
|
||||
await second;
|
||||
});
|
||||
expect(excludedOf(2)).toBe(false);
|
||||
});
|
||||
|
||||
it('FE-STA2-HOOK-009: a read asked before an exclusion and answered after it does not undo it', async () => {
|
||||
const { result } = renderHook(() => useTourActivityStats(true));
|
||||
await waitFor(() => expect(result.current.status).toBe('ready'));
|
||||
let read: (value: TourActivityStatsResponse) => void = () => undefined;
|
||||
repo.get.mockReturnValue(new Promise((resolve) => (read = resolve)));
|
||||
act(() => result.current.retry());
|
||||
repo.setExcluded.mockResolvedValue({
|
||||
...stats,
|
||||
entries: stats.entries.map((item) => (item.place_id === 1 ? { ...item, excluded: true } : item)),
|
||||
});
|
||||
await act(async () => {
|
||||
await result.current.setExcluded(entry(), true);
|
||||
});
|
||||
await act(async () => read(stats));
|
||||
expect(result.current.status).toBe('ready');
|
||||
expect(result.current.data?.entries.find((item) => item.place_id === 1)?.excluded).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,7 +4,7 @@ import {
|
||||
type TourActivityStatsResponse,
|
||||
type TourActivitySummary,
|
||||
} from '@trek/shared';
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { useTranslation } from '../../../i18n';
|
||||
import { TourActivityStatsOfflineError, tourActivityStatsRepo } from '../../../repo/tourActivityStatsRepo';
|
||||
import { localIsoDate } from '../../../utils/localDate';
|
||||
@@ -51,12 +51,27 @@ function flagged(data: TourActivityStatsResponse, placeId: number, excluded: boo
|
||||
};
|
||||
}
|
||||
|
||||
/** Every entry with the flags of the exclusions still on their way laid over it, the way they show already. */
|
||||
function withFlags(data: TourActivityStatsResponse, flags: ReadonlyMap<number, boolean>): TourActivityStatsResponse {
|
||||
if (flags.size === 0) return data;
|
||||
return {
|
||||
...data,
|
||||
entries: data.entries.map((entry) =>
|
||||
flags.has(entry.place_id) ? { ...entry, excluded: flags.get(entry.place_id) === true } : entry
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* STA-02: the user's own tour activities in the Atlas. Read live when the tab
|
||||
* is first shown (and again on retry), never cached on the device; narrowed by
|
||||
* period and type here, summed by the shared summary the assistant uses too.
|
||||
* Leaving a tour out shows at once and is undone with a message when the
|
||||
* server refuses it; the plan of the tour never changes.
|
||||
* server refuses it; the plan of the tour never changes. Answers can come
|
||||
* back in any order: only the answer to the latest read or write is taken
|
||||
* whole, with the exclusions still on their way laid over it; an older one
|
||||
* only confirms its own tour's flag, so it never puts back a state from
|
||||
* before a newer change.
|
||||
*/
|
||||
export function useTourActivityStats(active: boolean): TourActivityStats {
|
||||
const { t } = useTranslation();
|
||||
@@ -69,14 +84,19 @@ export function useTourActivityStats(active: boolean): TourActivityStats {
|
||||
const [pending, setPending] = useState<ReadonlySet<number>>(() => new Set());
|
||||
const [attempt, setAttempt] = useState(0);
|
||||
const today = localIsoDate();
|
||||
// Raised by every read and every write, so an answer knows whether a newer request went out after it.
|
||||
const latest = useRef(0);
|
||||
// The exclusions on their way to the server, by tour: the flag the user asked for.
|
||||
const inFlight = useRef(new Map<number, boolean>());
|
||||
|
||||
useEffect(() => {
|
||||
if (!active) return;
|
||||
const controller = new AbortController();
|
||||
const asked = ++latest.current;
|
||||
tourActivityStatsRepo.get(controller.signal).then(
|
||||
(answer) => {
|
||||
if (controller.signal.aborted) return;
|
||||
setData(answer);
|
||||
if (asked === latest.current) setData(withFlags(answer, inFlight.current));
|
||||
setAnswered({ attempt, status: 'ready' });
|
||||
},
|
||||
(err: unknown) => {
|
||||
@@ -102,11 +122,17 @@ export function useTourActivityStats(active: boolean): TourActivityStats {
|
||||
const setExcluded = useCallback(
|
||||
async (entry: TourActivityStatsEntry, excluded: boolean) => {
|
||||
const placeId = entry.place_id;
|
||||
const asked = ++latest.current;
|
||||
inFlight.current.set(placeId, excluded);
|
||||
setPending((current) => new Set(current).add(placeId));
|
||||
setData((current) => (current ? flagged(current, placeId, excluded) : current));
|
||||
try {
|
||||
setData(await tourActivityStatsRepo.setExcluded({ place_id: placeId, excluded }));
|
||||
const answer = await tourActivityStatsRepo.setExcluded({ place_id: placeId, excluded });
|
||||
inFlight.current.delete(placeId);
|
||||
if (asked === latest.current) setData(withFlags(answer, inFlight.current));
|
||||
else setData((current) => (current ? flagged(current, placeId, excluded) : current));
|
||||
} catch {
|
||||
inFlight.current.delete(placeId);
|
||||
setData((current) => (current ? flagged(current, placeId, !excluded) : current));
|
||||
toast.error(t('tours.stats.excludeFailed'));
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user