Files
fotospiel-app/resources/js/guest/services/eventApi.ts

66 lines
1.6 KiB
TypeScript

import { getDeviceId } from '../lib/device';
export interface EventData {
id: number;
slug: string;
name: string;
default_locale: string;
created_at: string;
updated_at: string;
join_token?: string | null;
type?: {
slug: string;
name: string;
icon: string;
};
}
export interface PackageData {
id: number;
name: string;
max_photos: number;
}
export interface EventPackage {
id: number;
used_photos: number;
expires_at: string;
package: PackageData;
}
export interface EventStats {
onlineGuests: number;
tasksSolved: number;
latestPhotoAt: string | null;
}
export async function fetchEvent(eventKey: string): Promise<EventData> {
const res = await fetch(`/api/v1/events/${encodeURIComponent(eventKey)}`);
if (!res.ok) throw new Error('Event fetch failed');
return await res.json();
}
export async function fetchStats(eventKey: string): Promise<EventStats> {
const res = await fetch(`/api/v1/events/${encodeURIComponent(eventKey)}/stats`, {
headers: {
'X-Device-Id': getDeviceId(),
},
});
if (!res.ok) throw new Error('Stats fetch failed');
const json = await res.json();
return {
onlineGuests: json.online_guests ?? json.onlineGuests ?? 0,
tasksSolved: json.tasks_solved ?? json.tasksSolved ?? 0,
latestPhotoAt: json.latest_photo_at ?? json.latestPhotoAt ?? null,
};
}
export async function getEventPackage(slug: string): Promise<EventPackage | null> {
const res = await fetch(`/api/v1/events/${encodeURIComponent(slug)}/package`);
if (!res.ok) {
if (res.status === 404) return null;
throw new Error('Failed to load event package');
}
return await res.json();
}