diff --git a/client/src/pages/journeyDetail/useJourneyDetail.ts b/client/src/pages/journeyDetail/useJourneyDetail.ts index a22cb9679..f0c6be413 100644 --- a/client/src/pages/journeyDetail/useJourneyDetail.ts +++ b/client/src/pages/journeyDetail/useJourneyDetail.ts @@ -50,7 +50,7 @@ export function useJourneyDetail({ journeyId, embedded = false }: JourneyDetailO const navigate = useNavigate() const toast = useToast() const { t, locale } = useTranslation() - const { current, loading, notFound, loadJourney, updateEntry, deleteEntry, reorderEntries, uploadPhotos, deletePhoto } = useJourneyStore() + const { current, loading, notFound, loadJourney, refreshJourney, updateEntry, deleteEntry, reorderEntries, uploadPhotos, deletePhoto } = useJourneyStore() const mapRef = useRef(null) const fullMapRef = useRef(null) /** The gallery hands its file picker up here, so the hero button can open it. */ @@ -341,7 +341,7 @@ export function useJourneyDetail({ journeyId, embedded = false }: JourneyDetailO // Provider photos from the gallery picker and from the entry editor, the same // way on desktop and phone (#1587). - const providerPhotos = useProviderPhotoAdds(() => { void loadJourney(Number(id)) }) + const providerPhotos = useProviderPhotoAdds(() => { void refreshJourney(Number(id)) }) const restoreSuggestions = useCallback(async () => { if (!current) return @@ -539,7 +539,7 @@ export function useJourneyDetail({ journeyId, embedded = false }: JourneyDetailO recordingPrep, recordings, canTakeOverRecording, recordingNeedsConnection, mapTracks, mapLayers, entryTitleOf, dayImport, dayImportDates, } = useJourneyDetailLayers({ - journey: current, loaded: journeyLoaded, canEditEntries, loadJourney, dawarich, dawarichByDate, dawarichTrail, + journey: current, loaded: journeyLoaded, canEditEntries, loadJourney: refreshJourney, dawarich, dawarichByDate, dawarichTrail, tripDates, sidebarMapItems, photoCount: mapPhotos.length, showTripTracks, }) @@ -592,7 +592,8 @@ export function useJourneyDetail({ journeyId, embedded = false }: JourneyDetailO recordings, canTakeOverRecording, recordingNeedsConnection, dayImport, dayImportDates, recordingPrep, mapPhotos, openMapPhotos, feedEdge, scrollFeedTo, - loadJourney, updateEntry, deleteEntry, reorderEntries, uploadPhotos, deletePhoto, + // The views reload after their own changes; such a read never rejects. + loadJourney: refreshJourney, updateEntry, deleteEntry, reorderEntries, uploadPhotos, deletePhoto, addPickedProviderPhotos: providerPhotos.addPickedPhotos, addEntryProviderPhotos: providerPhotos.addEntryPhotos, // A planned place picked on the map that nothing was written about yet: write it up. diff --git a/client/src/pages/journeyStudio/useJourneyStudio.ts b/client/src/pages/journeyStudio/useJourneyStudio.ts index 784c9e6e2..9dac37d8d 100644 --- a/client/src/pages/journeyStudio/useJourneyStudio.ts +++ b/client/src/pages/journeyStudio/useJourneyStudio.ts @@ -63,7 +63,7 @@ export function useJourneyStudio() { const location = useLocation() const { t, locale } = useTranslation() const isMobile = useIsMobile() - const { current, loading, loadJourney } = useJourneyStore() + const { current, loading, refreshJourney } = useJourneyStore() const doc = useStudioStore(s => s.doc) const loadDoc = useStudioStore(s => s.load) @@ -211,8 +211,8 @@ export function useJourneyStudio() { useEffect(() => { if (!Number.isFinite(journeyId)) return - if (!current || current.id !== journeyId) void loadJourney(journeyId) - }, [journeyId, current, loadJourney]) + if (!current || current.id !== journeyId) void refreshJourney(journeyId) + }, [journeyId, current, refreshJourney]) // Lay the book out once, from the journey. After that it is an ordinary // document and this must not run again, or it would throw away the user's @@ -672,7 +672,7 @@ export function useJourneyStudio() { * the new ones the way it shows an upload. */ const photoProviders = useConnectedPhotoProviders(canEdit) - const providerAdds = useProviderPhotoAdds(() => { void loadJourney(journeyId) }) + const providerAdds = useProviderPhotoAdds(() => { void refreshJourney(journeyId) }) const [providerPick, setProviderPick] = useState<{ provider: string; entryId: number | null } | null>(null) const pickAnswer = useRef<((photoIds: number[]) => void) | null>(null) const answerPick = (photoIds: number[]) => { diff --git a/client/src/store/journeyStore.test.ts b/client/src/store/journeyStore.test.ts index 5f18fea70..ba8823c88 100644 --- a/client/src/store/journeyStore.test.ts +++ b/client/src/store/journeyStore.test.ts @@ -152,6 +152,19 @@ describe('journeyStore', () => { expect(useJourneyStore.getState().notFound).toBe(true); }); + it('FE-STORE-JOURNEY-022: refreshJourney answers whether the read landed and never rejects', async () => { + const detail = buildJourneyDetail({ id: 5 }); + server.use(http.get('/api/journeys/5', () => HttpResponse.json(detail))); + await expect(useJourneyStore.getState().refreshJourney(5)).resolves.toBe(true); + expect(useJourneyStore.getState().current?.id).toBe(5); + + // A failed read keeps the journal as shown instead of throwing into a view callback. + server.use(http.get('/api/journeys/5', () => HttpResponse.json({ error: 'down' }, { status: 500 }))); + await expect(useJourneyStore.getState().refreshJourney(5)).resolves.toBe(false); + expect(useJourneyStore.getState().current?.id).toBe(5); + expect(useJourneyStore.getState().notFound).toBe(false); + }); + // ── createJourney ──────────────────────────────────────────────────────── it('FE-STORE-JOURNEY-005: createJourney adds to store and returns journey', async () => { diff --git a/client/src/store/journeyStore.ts b/client/src/store/journeyStore.ts index 006275890..b5b13430f 100644 --- a/client/src/store/journeyStore.ts +++ b/client/src/store/journeyStore.ts @@ -191,6 +191,11 @@ interface JourneyState { loadJourneys: () => Promise loadJourney: (id: number) => Promise + /** + * Reads the journal again after a change the server already took, or when an + * event says it changed. Never rejects: it answers whether the read landed. + */ + refreshJourney: (id: number) => Promise createJourney: (data: { title: string; subtitle?: string; trip_ids?: number[] }) => Promise updateJourney: (id: number, data: Record) => Promise deleteJourney: (id: number) => Promise @@ -276,6 +281,14 @@ export const useJourneyStore = create((set, get) => ({ } }, + refreshJourney: (id) => + // A failed read keeps the journal as shown: a 404 has become `notFound` in + // loadJourney, anything else is read again with the next change or event. + get().loadJourney(id).then( + () => true, + () => false, + ), + createJourney: async (data) => { const journey = await journeyApi.create(data) set(s => ({ journeys: [journey, ...s.journeys] }))