mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 07:30:04 +00:00
fix(journey): keep the journal shown when a reload after a change fails
The journal views read the journal again after their own changes (saved settings, a finished entry, an added trip, picked provider photos) by calling loadJourney and dropping the promise. loadJourney rethrows every failure, so a reload that failed, offline or timed out, became an unhandled rejection with nothing shown to the user. In CI the same happened whenever a test ended with such a read still open: the teardown runs the pending timers, the request timeout fired, and the run failed on the unhandled error. The store gets refreshJourney, which reads the journal and answers whether the read landed instead of rejecting; a 404 still turns into notFound. The shared journal hook hands it to the desktop and the phone view and to the map layers in place of the raw loader, and the Studio uses it for its own reads.
This commit is contained in:
@@ -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<JourneyMapHandle>(null)
|
||||
const fullMapRef = useRef<JourneyMapHandle>(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.
|
||||
|
||||
@@ -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[]) => {
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -191,6 +191,11 @@ interface JourneyState {
|
||||
|
||||
loadJourneys: () => Promise<void>
|
||||
loadJourney: (id: number) => Promise<void>
|
||||
/**
|
||||
* 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<boolean>
|
||||
createJourney: (data: { title: string; subtitle?: string; trip_ids?: number[] }) => Promise<Journey>
|
||||
updateJourney: (id: number, data: Record<string, unknown>) => Promise<void>
|
||||
deleteJourney: (id: number) => Promise<void>
|
||||
@@ -276,6 +281,14 @@ export const useJourneyStore = create<JourneyState>((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] }))
|
||||
|
||||
Reference in New Issue
Block a user