feat(journey): client repo and hook for draft entries from photos

Online-only api and repo over the new routes, parsed against the shared
contract, and a markup-free hook (with its pure choice model) that reads
the preview, keeps the chosen groups and titles across reloads and sends
the create, for the desktop dialog and the phone sheet to build on (#1920).
This commit is contained in:
Maurice
2026-11-30 14:32:46 +01:00
parent 9c8a33d400
commit 76b33a7e60
8 changed files with 658 additions and 0 deletions
@@ -0,0 +1,64 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { journeyEntriesFromPhotosApi } from '../api/journeyEntriesFromPhotos';
import { journeyEntriesFromPhotosRepo } from './journeyEntriesFromPhotosRepo';
import { OnlineOnlyError } from './onlineOnly';
// FE-REPO-JOURNEY-FROM-PHOTOS-001 to FE-REPO-JOURNEY-FROM-PHOTOS-003
const network = vi.hoisted(() => ({ offline: false }));
vi.mock('../sync/networkMode', () => ({ isEffectivelyOffline: () => network.offline }));
const CLUSTER = {
date: '2026-05-14',
startTime: '09:00',
endTime: '10:00',
timezone: 'Europe/Rome',
lat: 45.43,
lng: 12.33,
countryCode: 'IT',
locationName: 'Venezia',
suggestedTitle: 'Venezia',
titleSource: 'place' as const,
photoIds: [4, 5],
photoCount: 2,
coverPhotoId: 4,
};
beforeEach(() => {
network.offline = false;
vi.restoreAllMocks();
});
describe('journeyEntriesFromPhotosRepo', () => {
it('FE-REPO-JOURNEY-FROM-PHOTOS-001: asks nothing offline', async () => {
network.offline = true;
const preview = vi.spyOn(journeyEntriesFromPhotosApi, 'preview');
const create = vi.spyOn(journeyEntriesFromPhotosApi, 'create');
await expect(journeyEntriesFromPhotosRepo.preview(7, {})).rejects.toBeInstanceOf(OnlineOnlyError);
await expect(journeyEntriesFromPhotosRepo.create(7, { groups: [{ photoIds: [4] }] })).rejects.toBeInstanceOf(
OnlineOnlyError
);
expect(preview).not.toHaveBeenCalled();
expect(create).not.toHaveBeenCalled();
});
it('FE-REPO-JOURNEY-FROM-PHOTOS-002: answers what the contract describes', async () => {
const preview = { clusters: [CLUSTER], withoutCaptureTime: 1, maxGapMinutes: 180, maxDistanceKm: 20 };
vi.spyOn(journeyEntriesFromPhotosApi, 'preview').mockResolvedValue(preview);
vi.spyOn(journeyEntriesFromPhotosApi, 'create').mockResolvedValue({
entries: [{ id: 9, title: 'Venezia', entry_date: '2026-05-14', entry_time: '09:00', photoCount: 2 }],
skippedPhotoIds: [],
});
const controller = new AbortController();
expect(await journeyEntriesFromPhotosRepo.preview(7, { lang: 'it' }, controller.signal)).toEqual(preview);
expect(journeyEntriesFromPhotosApi.preview).toHaveBeenCalledWith(7, { lang: 'it' }, controller.signal);
expect((await journeyEntriesFromPhotosRepo.create(7, { groups: [{ photoIds: [4, 5] }] })).entries[0]?.id).toBe(9);
});
it('FE-REPO-JOURNEY-FROM-PHOTOS-003: refuses an answer the contract does not describe', async () => {
vi.spyOn(journeyEntriesFromPhotosApi, 'preview').mockResolvedValue({ clusters: 'none' } as never);
vi.spyOn(journeyEntriesFromPhotosApi, 'create').mockResolvedValue({ entries: [{ id: 'x' }] } as never);
await expect(journeyEntriesFromPhotosRepo.preview(7, {})).rejects.toThrow();
await expect(journeyEntriesFromPhotosRepo.create(7, { groups: [{ photoIds: [4] }] })).rejects.toThrow();
});
});
@@ -0,0 +1,40 @@
import {
journeyEntriesFromPhotosCreateResponseSchema,
journeyEntriesFromPhotosPreviewResponseSchema,
type JourneyEntriesFromPhotosCreateRequest,
type JourneyEntriesFromPhotosCreateResponse,
type JourneyEntriesFromPhotosPreviewRequest,
type JourneyEntriesFromPhotosPreviewResponse,
} from '@trek/shared';
import { journeyEntriesFromPhotosApi } from '../api/journeyEntriesFromPhotos';
import { requireOnline } from './onlineOnly';
/**
* #1920, online only like the journey itself: the grouping reads the gallery
* as the server holds it and names the groups through the geocoder, and the
* create links photos the server checks again inside its transaction, which
* an offline replay could not do. Answers are parsed against the shared
* contract, so a server that answered something else fails here.
*/
export const journeyEntriesFromPhotosRepo = {
async preview(
journeyId: number,
body: JourneyEntriesFromPhotosPreviewRequest,
signal?: AbortSignal
): Promise<JourneyEntriesFromPhotosPreviewResponse> {
requireOnline();
return journeyEntriesFromPhotosPreviewResponseSchema.parse(
await journeyEntriesFromPhotosApi.preview(journeyId, body, signal)
);
},
async create(
journeyId: number,
body: JourneyEntriesFromPhotosCreateRequest
): Promise<JourneyEntriesFromPhotosCreateResponse> {
requireOnline();
return journeyEntriesFromPhotosCreateResponseSchema.parse(
await journeyEntriesFromPhotosApi.create(journeyId, body)
);
},
};