mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 19:16:08 +00:00
f833a05461
A tour can now point at the packing list of its trip, as a whole or at one of its categories (say "Hiking gear" for a hike), and shows it in its detail. The new Packing list section reads the list live as the user may see it: the backpack tile with the item count on its corner and the category's own colour, how many are packed with a thin progress bar, the first items ticked off or not with their quantity, how many more there are, and a button that opens the trip's Lists tab. Who may change the tour links one, changes it or lets go through a dialog that lists the whole list and every category with its colour and item count, and writes only on its confirm. It is a reference, never a copy: the items stay in Packing, are ticked off and edited there, and each traveller sees in the tour only the items Packing shows them, so an item kept to oneself never shows through a tour. A category that is renamed, cleared or only holds items of others reads as missing, an empty one as empty, a link that could not be read says so with a retry, and while Lists are switched off the link stays, dimmed, and can still be let go. Removing the link, or the tour, leaves the list and every item as they are. Nothing suggests or changes an item by itself. On the server the link is a row of its own in the tours domain (tour_packing_links, migration 046110), one per tour and gone with it. GET, PUT and DELETE /api/trips/:tripId/tours/:placeId/packing-link read it with the trip and change it with place_edit; linking needs the Lists addon and, for a category, an item of it the user can see. The writes answer the tour with its link and tell the trip through tours:changed, so the offline client queues them as writes of the tour and replays them like one. A copied trip keeps its tours' links, a tour saved as new or taken over does not. The MCP tools get_tour_packing, link_tour_packing and unlink_tour_packing run over the same service and checks; the read also hands the tour's facts as context for suggesting what to pack, which only becomes an item through the packing tools. The empty category placeholder now lives in @trek/shared so both sides leave it out alike. All 27 locales carry the new strings, and the wiki describes the link and the tools.
101 lines
4.4 KiB
TypeScript
101 lines
4.4 KiB
TypeScript
import type { TourDetailResponse, TourPackingLink, TourPackingLinkRequest } from '@trek/shared';
|
|
import { tourPackingLinkApi, tourPackingLinkPath } from '../api/tourPackingLink';
|
|
import { cacheTourAnswer } from '../db/cacheTour';
|
|
import { offlineDb } from '../db/offlineDb';
|
|
import { generateUUID, mutationQueue } from '../sync/mutationQueue';
|
|
import { onlineThenCache } from './withOfflineFallback';
|
|
|
|
/** A link write is a write of its tour: it queues behind the tour's own writes and replays like them. */
|
|
const RESOURCE = 'tours';
|
|
|
|
/** The link as this device last read it: null for none, undefined while it never read one. */
|
|
async function cachedLink(placeId: number): Promise<TourPackingLink | null | undefined> {
|
|
return (await offlineDb.tours.get(placeId))?.packing_link;
|
|
}
|
|
|
|
/** Keeps the link on the cached tour, where this device holds that tour. */
|
|
async function keepLink(placeId: number, link: TourPackingLink | null): Promise<void> {
|
|
await offlineDb.tours.update(placeId, { packing_link: link });
|
|
}
|
|
|
|
/** Now, in the stored spelling the server's own link time has. */
|
|
function storedNow(): string {
|
|
return new Date().toISOString().slice(0, 19).replace('T', ' ');
|
|
}
|
|
|
|
/**
|
|
* A link write. Offline, for a tour that has not synced yet, or behind an
|
|
* older write of the same tour still queued, the cached tour takes the link
|
|
* at once and the write waits in the queue; otherwise it goes straight to the
|
|
* server and its answer is cached like any tour write.
|
|
*/
|
|
async function write(
|
|
tripId: number | string,
|
|
placeId: number,
|
|
request: { method: 'PUT'; body: TourPackingLinkRequest } | { method: 'DELETE' },
|
|
local: TourPackingLink | null
|
|
): Promise<TourPackingLink | null> {
|
|
const unsynced = placeId < 0;
|
|
if (unsynced || (await mutationQueue.mustQueue(RESOURCE, placeId))) {
|
|
await keepLink(placeId, local);
|
|
await mutationQueue.enqueue({
|
|
id: generateUUID(),
|
|
tripId: Number(tripId),
|
|
method: request.method,
|
|
url: tourPackingLinkPath(tripId, unsynced ? '{id}' : placeId),
|
|
body: request.method === 'PUT' ? request.body : undefined,
|
|
resource: RESOURCE,
|
|
entityId: placeId,
|
|
...(unsynced ? { tempEntityId: placeId } : {}),
|
|
});
|
|
mutationQueue.sendSoon();
|
|
return local;
|
|
}
|
|
const answer: TourDetailResponse =
|
|
request.method === 'PUT'
|
|
? await tourPackingLinkApi.link(tripId, placeId, request.body)
|
|
: await tourPackingLinkApi.unlink(tripId, placeId);
|
|
await cacheTourAnswer(Number(tripId), answer);
|
|
return answer.packing_link ?? null;
|
|
}
|
|
|
|
/**
|
|
* P2D-XD-01 through the offline core. The link is kept on the cached tour, so
|
|
* it reads offline as last read, and a link made or let go offline waits in the
|
|
* queue as a write of its tour. The items it shows are not kept here: they
|
|
* come from the trip's packing list, which has an offline copy of its own.
|
|
*/
|
|
export const tourPackingLinkRepo = {
|
|
async read(tripId: number | string, placeId: number, signal?: AbortSignal): Promise<TourPackingLink | null> {
|
|
const known = await cachedLink(placeId);
|
|
// A tour written here offline has no link but the one made here.
|
|
if (placeId < 0) return known ?? null;
|
|
// Behind a queued write of the tour (offline, every write is), this device's copy is the newer one.
|
|
if (known !== undefined && (await mutationQueue.mustQueue(RESOURCE, placeId))) return known;
|
|
return onlineThenCache(
|
|
async () => {
|
|
const { packing_link } = await tourPackingLinkApi.read(tripId, placeId, signal);
|
|
await keepLink(placeId, packing_link);
|
|
return packing_link;
|
|
},
|
|
async () => {
|
|
// An aborted request is not a lost connection: the caller moved on.
|
|
signal?.throwIfAborted();
|
|
// Never read here: whether the tour points anywhere is unknown, not "nowhere".
|
|
if (known === undefined) throw new Error('Packing link not available offline');
|
|
return known;
|
|
}
|
|
);
|
|
},
|
|
|
|
/** Points the tour at the whole list (category null) or at one category. */
|
|
async link(tripId: number | string, placeId: number, body: TourPackingLinkRequest): Promise<TourPackingLink | null> {
|
|
return write(tripId, placeId, { method: 'PUT', body }, { category: body.category, linked_at: storedNow() });
|
|
},
|
|
|
|
/** Lets go of what the tour points at; the packing list stays as it is. */
|
|
async unlink(tripId: number | string, placeId: number): Promise<TourPackingLink | null> {
|
|
return write(tripId, placeId, { method: 'DELETE' }, null);
|
|
},
|
|
};
|