mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 21:11:14 +00:00
fix(tours): restoring a recorded track and saving a copy wait for the tour's queued edits
Both go straight to the server. While an edit of the same tour still waited in the queue, the restore went out first and its answer replaced the cached edit, and the edit replayed afterwards planned the track over again. A copy copied the server's older line instead of the one on screen. Both now wait the way the actual track writes do: offline, behind a queued write of the tour, or for a tour the server has not got yet they refuse with OnlineOnlyError, and the planner says a connection is needed and the waiting changes go first, in all 27 languages.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import type { TourListItem } from '@trek/shared';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { OnlineOnlyError } from '../../repo/onlineOnly';
|
||||
import type { TourPlannerController } from './planner/useTourPlanner';
|
||||
import { useRecordedTrack } from './useRecordedTrack';
|
||||
|
||||
@@ -87,4 +88,21 @@ describe('useRecordedTrack', () => {
|
||||
expect(controller.returnToNeutral).not.toHaveBeenCalled();
|
||||
expect(onRestored).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('says it needs a connection when it is offline or behind an edit of the tour still waiting', async () => {
|
||||
repo.restoreRecorded.mockRejectedValueOnce(new OnlineOnlyError());
|
||||
const controller = planner(7);
|
||||
const onRestored = vi.fn();
|
||||
const { result } = renderHook(() =>
|
||||
useRecordedTrack({ tripId: 1, planner: controller, tours: [replaced], canEdit: true, onRestored })
|
||||
);
|
||||
|
||||
await act(async () => {
|
||||
await result.current.restore();
|
||||
});
|
||||
|
||||
expect(toast.error).toHaveBeenCalledWith('tours.recorded.offline');
|
||||
expect(controller.returnToNeutral).not.toHaveBeenCalled();
|
||||
expect(onRestored).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { TourListItem } from '@trek/shared';
|
||||
import { useCallback, useState } from 'react';
|
||||
import { useTranslation } from '../../i18n';
|
||||
import { OnlineOnlyError } from '../../repo/onlineOnly';
|
||||
import { tourRepo } from '../../repo/tourRepo';
|
||||
import { useToast } from '../shared/Toast';
|
||||
import type { TourPlannerController } from './planner/useTourPlanner';
|
||||
@@ -36,8 +37,9 @@ export function useRecordedTrack({ tripId, planner, tours, canEdit, onRestored }
|
||||
returnToNeutral();
|
||||
await onRestored(result.tour);
|
||||
toast.success(t('tours.recorded.restored'));
|
||||
} catch {
|
||||
toast.error(t('tours.recorded.failed'));
|
||||
} catch (err: unknown) {
|
||||
// Offline, or behind an edit of the tour still waiting to be sent: said as such, not as a failure.
|
||||
toast.error(t(err instanceof OnlineOnlyError ? 'tours.recorded.offline' : 'tours.recorded.failed'));
|
||||
} finally {
|
||||
setRestoring(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user