Files
fotospiel-app/resources/js/guest-v2/services/photosApi.ts
Codex Agent 0a08f2704f
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled
refactor(guest): retire legacy guest app and move shared modules
2026-02-06 08:42:53 +01:00

79 lines
2.5 KiB
TypeScript

import { fetchJson } from './apiClient';
import { getDeviceId } from '../lib/device';
export { likePhoto, unlikePhoto, createPhotoShareLink, uploadPhoto, deletePhoto } from '@/shared/guest/services/photosApi';
export type GalleryPhoto = Record<string, unknown>;
type GalleryResponse = {
data?: GalleryPhoto[];
next_cursor?: string | null;
latest_photo_at?: string | null;
};
const galleryCache = new Map<string, { etag: string | null; data: GalleryResponse }>();
export async function fetchGallery(
eventToken: string,
params: { cursor?: string; since?: string; limit?: number; locale?: string } = {}
) {
const search = new URLSearchParams();
if (params.cursor) search.set('cursor', params.cursor);
if (params.since) search.set('since', params.since);
if (params.limit) search.set('limit', params.limit.toString());
if (params.locale) search.set('locale', params.locale);
const cacheKey = `${eventToken}:${search.toString()}`;
const cached = galleryCache.get(cacheKey);
const response = await fetchJson<GalleryResponse>(
`/api/v1/events/${encodeURIComponent(eventToken)}/photos${search.toString() ? `?${search.toString()}` : ''}`,
{
headers: {
'X-Device-Id': getDeviceId(),
},
etag: cached?.etag ?? null,
noStore: true,
}
);
if (response.notModified && cached) {
return { ...cached.data, notModified: true };
}
const payload = response.data ?? { data: [], next_cursor: null, latest_photo_at: null };
const items = Array.isArray((payload as GalleryResponse).data)
? (payload as GalleryResponse).data ?? []
: Array.isArray((payload as { photos?: GalleryPhoto[] }).photos)
? (payload as { photos?: GalleryPhoto[] }).photos ?? []
: Array.isArray(payload)
? (payload as GalleryPhoto[])
: [];
const data = {
data: items,
next_cursor: (payload as GalleryResponse).next_cursor ?? null,
latest_photo_at: (payload as GalleryResponse).latest_photo_at ?? null,
};
galleryCache.set(cacheKey, { etag: response.etag, data });
return { ...data, notModified: false };
}
export async function fetchPhoto(photoId: number, locale?: string) {
const search = locale ? `?locale=${encodeURIComponent(locale)}` : '';
const response = await fetchJson<GalleryPhoto>(`/api/v1/photos/${photoId}${search}`, {
headers: {
'X-Device-Id': getDeviceId(),
...(locale ? { 'X-Locale': locale } : {}),
},
noStore: true,
});
return response.data;
}
export function clearGalleryCache() {
galleryCache.clear();
}