mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 01:44:13 +00:00
feat(places): one place view for All places and the lists
The desktop dialog and the phone sheet of a place show its overview, notes, tours and bookings, photos and files and its history in tabs, with the tags of each list and trip, a markdown notes editor, uploads and the place picture. The rating bar of All places is the rating row the lists show, with its voters, and votes once for every copy. All places filters by a lowest rating and sorts by it, and the edit form changes the first copy the reader may edit, which the server carries to the others.
This commit is contained in:
@@ -1,49 +0,0 @@
|
||||
import type {
|
||||
PlacesLibraryBulkTagsRequest,
|
||||
PlacesLibraryBulkTagsResponse,
|
||||
PlacesLibraryEntry,
|
||||
Tag,
|
||||
} from '@trek/shared';
|
||||
|
||||
import { placeTagsApi } from '../api/placeTags';
|
||||
import { requireOnline } from './onlineOnly';
|
||||
|
||||
/**
|
||||
* PLC-21, online only on purpose like All places itself: the tags sit on
|
||||
* entries the server resolves from the trips and lists the user reaches at
|
||||
* the moment of asking, so there is no cache to answer from. Only the
|
||||
* user's personal tags come back (a trip's own tags never do).
|
||||
*/
|
||||
export const placeTagsRepo = {
|
||||
async list(): Promise<Tag[]> {
|
||||
requireOnline();
|
||||
const { tags } = await placeTagsApi.list();
|
||||
return tags.filter((tag) => tag.trip_id == null);
|
||||
},
|
||||
|
||||
async create(name: string, color?: string): Promise<Tag> {
|
||||
requireOnline();
|
||||
return (await placeTagsApi.create(color ? { name, color } : { name })).tag;
|
||||
},
|
||||
|
||||
async update(id: number, changes: { name?: string; color?: string }): Promise<Tag> {
|
||||
requireOnline();
|
||||
return (await placeTagsApi.update(id, changes)).tag;
|
||||
},
|
||||
|
||||
async remove(id: number): Promise<void> {
|
||||
requireOnline();
|
||||
await placeTagsApi.remove(id);
|
||||
},
|
||||
|
||||
/** The entry after the change, null once it is out of reach. */
|
||||
async setForEntry(key: string, tagIds: number[]): Promise<PlacesLibraryEntry | null> {
|
||||
requireOnline();
|
||||
return (await placeTagsApi.setForEntry({ key, tag_ids: tagIds })).entry;
|
||||
},
|
||||
|
||||
async bulk(body: PlacesLibraryBulkTagsRequest): Promise<PlacesLibraryBulkTagsResponse['entries']> {
|
||||
requireOnline();
|
||||
return (await placeTagsApi.bulk(body)).entries;
|
||||
},
|
||||
};
|
||||
@@ -3,11 +3,33 @@ import type {
|
||||
PlacesLibraryKind,
|
||||
PlacesLibraryPageResponse,
|
||||
PlacesLibraryQueryInput,
|
||||
PlacesLibraryRateResponse,
|
||||
PlacesLibraryTag,
|
||||
} from '@trek/shared';
|
||||
import { mapsApi, placesApi } from '../api/client';
|
||||
import { collectionsApi } from '../api/collections';
|
||||
import { placesLibraryApi } from '../api/placesLibrary';
|
||||
import { fileRepo } from './fileRepo';
|
||||
import { requireOnline } from './onlineOnly';
|
||||
import { tripTagRepo } from './tripTagRepo';
|
||||
|
||||
/**
|
||||
* PLC-21: where an origin's tags live: a saved place's in its list (the list's labels),
|
||||
* a trip place's in its trip (the trip's tags).
|
||||
*/
|
||||
export type OriginTagRef =
|
||||
| { kind: 'list'; collection_id: number; collection_place_id: number }
|
||||
| { kind: 'trip'; trip_id: number; place_id: number };
|
||||
|
||||
/** What the edit form of All places changes of a place's copy. */
|
||||
export interface PlaceDetailsPatch {
|
||||
name: string;
|
||||
category_id: number | null;
|
||||
address: string | null;
|
||||
description: string | null;
|
||||
lat?: number | null;
|
||||
lng?: number | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* PLC-07/PLC-08, online only on purpose, like the collections themselves:
|
||||
@@ -50,4 +72,97 @@ export const placesLibraryRepo = {
|
||||
if (origin.kind === 'list') await collectionsApi.updatePlace(origin.collection_place_id, { notes });
|
||||
else await placesApi.update(origin.trip_id, origin.place_id, { notes });
|
||||
},
|
||||
|
||||
/**
|
||||
* A place of All places edited where one of its copies lives: a saved place's
|
||||
* details in its list, a trip place's in its trip. The other copies keep theirs.
|
||||
*/
|
||||
async saveDetails(
|
||||
origin: { kind: 'trip'; trip_id: number; place_id: number } | { kind: 'list'; collection_place_id: number },
|
||||
patch: PlaceDetailsPatch
|
||||
): Promise<void> {
|
||||
requireOnline();
|
||||
if (origin.kind === 'list') await collectionsApi.updatePlace(origin.collection_place_id, patch);
|
||||
else await placesApi.update(origin.trip_id, origin.place_id, patch);
|
||||
},
|
||||
|
||||
/**
|
||||
* The reader's vote on a place of All places, cast where that copy lives: in its
|
||||
* list or in its trip. Null takes the vote back.
|
||||
*/
|
||||
async rate(
|
||||
origin: { kind: 'trip'; trip_id: number; place_id: number } | { kind: 'list'; collection_place_id: number },
|
||||
rating: number | null
|
||||
): Promise<void> {
|
||||
requireOnline();
|
||||
if (origin.kind === 'list') await collectionsApi.ratePlace(origin.collection_place_id, rating);
|
||||
else await placesApi.rate(origin.trip_id, origin.place_id, rating);
|
||||
},
|
||||
|
||||
/**
|
||||
* The reader's one vote on a place of All places, by its entry key: set (1 to 5) or
|
||||
* taken back (null) on every copy of it they may vote on, in each list they belong to
|
||||
* and each trip they are on. Answers the entry's votes as one afterwards.
|
||||
*/
|
||||
async rateEntry(key: string, rating: number | null): Promise<PlacesLibraryRateResponse> {
|
||||
requireOnline();
|
||||
return await placesLibraryApi.rate(key, rating);
|
||||
},
|
||||
|
||||
/**
|
||||
* A picture or file added to a place of All places, where that copy keeps such
|
||||
* things: a trip keeps it as a file of the trip on the place, a list as the
|
||||
* place's picture in the list (a picture only).
|
||||
*/
|
||||
async uploadMedia(
|
||||
origin: { kind: 'trip'; trip_id: number; place_id: number } | { kind: 'list'; collection_place_id: number },
|
||||
file: File
|
||||
): Promise<void> {
|
||||
requireOnline();
|
||||
const fd = new FormData();
|
||||
if (origin.kind === 'list') {
|
||||
fd.append('image', file);
|
||||
await collectionsApi.uploadPlaceImage(origin.collection_place_id, fd);
|
||||
return;
|
||||
}
|
||||
fd.append('file', file);
|
||||
fd.append('place_id', String(origin.place_id));
|
||||
await fileRepo.upload(origin.trip_id, fd);
|
||||
},
|
||||
|
||||
/** A picture of the place set as its picture in one of its copies (a list's or a trip's). */
|
||||
async setImage(
|
||||
origin: { kind: 'trip'; trip_id: number; place_id: number } | { kind: 'list'; collection_place_id: number },
|
||||
imageUrl: string
|
||||
): Promise<void> {
|
||||
requireOnline();
|
||||
if (origin.kind === 'list') await collectionsApi.updatePlace(origin.collection_place_id, { image_url: imageUrl });
|
||||
else await placesApi.update(origin.trip_id, origin.place_id, { image_url: imageUrl });
|
||||
},
|
||||
|
||||
/**
|
||||
* PLC-21: the tags one origin carries from now on, all of them (an empty list takes every
|
||||
* one off), written where they live: a saved place's list tags as its `label_ids`, a trip
|
||||
* place's trip tags as its `tags`. The list's or the trip's own rule decides who may.
|
||||
*/
|
||||
async setOriginTags(origin: OriginTagRef, tagIds: number[]): Promise<void> {
|
||||
requireOnline();
|
||||
if (origin.kind === 'list') await collectionsApi.updatePlace(origin.collection_place_id, { label_ids: tagIds });
|
||||
else await placesApi.update(origin.trip_id, origin.place_id, { tags: tagIds });
|
||||
},
|
||||
|
||||
/**
|
||||
* PLC-21: a new tag of the origin's list (a label of it) or trip, for putting on the place
|
||||
* with `setOriginTags`. A trip answers its tag of that name when it has one already; a list
|
||||
* refuses a name it has (409), so pick one of `available_tags` first.
|
||||
*/
|
||||
async createOriginTag(origin: OriginTagRef, tag: { name: string; color?: string }): Promise<PlacesLibraryTag> {
|
||||
requireOnline();
|
||||
if (origin.kind === 'list') {
|
||||
const label = await collectionsApi.createLabel(origin.collection_id, tag.name, tag.color);
|
||||
return { id: label.id, name: label.name, color: label.color ?? null };
|
||||
}
|
||||
const created = await tripTagRepo.create(origin.trip_id, tag);
|
||||
return { id: created.id, name: created.name, color: created.color };
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user