Files
TREK/client/src/pages/tripPlanner/placeImageSave.ts
T
Maurice 35566690e9 feat(places): the picture of a place is chosen in the editor, and the view opens its photos
- The editor's 'Pick a picture' is the one place a place's picture is set, on
  the desktop dialog and the phone sheet alike: upload one from the device,
  keep the current one (now shown as chosen), take an image attached to the
  place, or one of the sources' photos, or remove it. Nothing is sent before
  the save: the upload and the attached file are put in place once the place
  is saved, so Cancel leaves the picture as it was and a new place can get
  one too. Without the details column the same picker sits in the form.
- The view (inspector and phone sheet) shows the picture plainly; a click opens
  the gallery of its own picture and its sources' photos.
- The gallery drops an answer that arrives after the place changed, and its
  credits link to their source pages. The lightbox is a labelled modal dialog
  that takes and gives back focus, names its arrows and follows the reading
  direction.
- The picker leaves the details column (PlaceImagePicker), the column reads
  the shared enrichment cache, and PlaceAvatarUpload and usePlaceImagePick,
  which only the view used, are gone.
2026-10-23 21:01:11 +02:00

42 lines
1.6 KiB
TypeScript

import type { PendingImagePayload } from '../../components/Planner/placeImageChoice';
import { isEffectivelyOffline } from '../../sync/networkMode';
import type { Place } from '../../types';
interface ImageActions {
uploadPlaceImage: (tripId: number | string, placeId: number, file: File) => Promise<Place>;
setPlaceImageFromFile: (tripId: number | string, placeId: number, fileId: number) => Promise<Place>;
}
/**
* Takes the waiting picture off what the form saves. It goes to its own route
* once the place has an id, and that route needs a connection: offline the
* form stays open with it, the way it does with files, rather than saving a
* place that quietly loses its picture.
*/
export function takePendingImage(
data: { _pendingImage?: PendingImagePayload },
needsConnection: () => string
): PendingImagePayload | undefined {
const pending = data._pendingImage;
delete data._pendingImage;
if (pending && isEffectivelyOffline()) throw new Error(needsConnection());
return pending;
}
/** Makes the waiting picture the place's own: the file uploaded, or the attached file taken. */
export async function savePendingImage(
actions: ImageActions,
tripId: number | string,
placeId: number | undefined,
pending: PendingImagePayload | undefined,
onError: (err: unknown) => void
): Promise<void> {
if (!pending || !placeId) return;
try {
if (pending instanceof File) await actions.uploadPlaceImage(tripId, placeId, pending);
else await actions.setPlaceImageFromFile(tripId, placeId, pending.fileId);
} catch (err: unknown) {
onError(err);
}
}