Files
TREK/client/src/api/placesLibrary.ts
T
Maurice d758f4c592 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.
2026-11-29 23:36:43 +00:00

36 lines
1.6 KiB
TypeScript

import {
joinLibraryTagNames,
type PlacesLibraryDetailResponse,
type PlacesLibraryKind,
type PlacesLibraryPageResponse,
type PlacesLibraryQueryInput,
type PlacesLibraryRateResponse,
} from '@trek/shared';
import apiClient from './client';
const base = '/addons/collections/library/places';
/**
* The query as the server reads it: a tag filter (PLC-21) as its names joined by commas, a comma
* inside a name escaped (`joinLibraryTagNames`), not `tags[]=`.
*/
function queryParams(query: PlacesLibraryQueryInput): Record<string, unknown> {
const { tags, ...rest } = query;
if (tags === undefined) return rest;
const joined = Array.isArray(tags) ? joinLibraryTagNames(tags) : tags;
return joined === '' ? rest : { ...rest, tags: joined };
}
/** PLC-07/PLC-08 "All places": one page of entries, and one entry by the trip place or saved place it was opened from. */
export const placesLibraryApi = {
list: (query: PlacesLibraryQueryInput = {}, signal?: AbortSignal): Promise<PlacesLibraryPageResponse> =>
apiClient.get(base, { params: queryParams(query), signal }).then((r) => r.data),
detail: (kind: PlacesLibraryKind, id: number, signal?: AbortSignal): Promise<PlacesLibraryDetailResponse> =>
apiClient.get(`${base}/${kind}/${id}`, { signal }).then((r) => r.data),
/** The reader's vote on an entry, on every copy of it they may vote on; null takes it back. */
rate: (key: string, rating: number | null): Promise<PlacesLibraryRateResponse> =>
rating === null
? apiClient.delete(`${base}/rating`, { params: { key } }).then((r) => r.data)
: apiClient.put(`${base}/rating`, { key, rating }).then((r) => r.data),
};