2026-03-05 22:55:19 +01:00
|
|
|
import { useStoryblokApi } from '@storyblok/astro';
|
2026-03-05 22:22:32 +01:00
|
|
|
import type { SbBlokData } from '@storyblok/astro';
|
|
|
|
|
|
|
|
|
|
function getVersion(): 'draft' | 'published' {
|
|
|
|
|
return import.meta.env.STORYBLOK_IS_PREVIEW === 'true' ? 'draft' : 'published';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface Spectacle {
|
|
|
|
|
id: string;
|
|
|
|
|
title: string;
|
|
|
|
|
category: 'jeune-public' | 'tout-public';
|
|
|
|
|
retired: boolean;
|
|
|
|
|
age: string;
|
|
|
|
|
duration: string;
|
|
|
|
|
summary: string;
|
|
|
|
|
credits: string;
|
|
|
|
|
image: string;
|
|
|
|
|
gallery: string[];
|
|
|
|
|
dossierPro: string;
|
|
|
|
|
_blok: SbBlokData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AgendaEvent {
|
|
|
|
|
id: string;
|
|
|
|
|
date: string;
|
|
|
|
|
location: string;
|
|
|
|
|
spectacleId: string;
|
|
|
|
|
bookingLink: string | null;
|
|
|
|
|
_blok: SbBlokData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function extractSlug(field: unknown): string {
|
|
|
|
|
if (!field) return '';
|
|
|
|
|
if (typeof field === 'string') return field;
|
|
|
|
|
if (typeof field === 'object' && field !== null) {
|
|
|
|
|
const link = field as Record<string, string>;
|
|
|
|
|
const fullSlug = link.cached_url || link.slug || '';
|
|
|
|
|
return fullSlug.split('/').filter(Boolean).pop() || '';
|
|
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function mapStoryToSpectacle(story: any): Spectacle {
|
|
|
|
|
const c = story.content;
|
|
|
|
|
return {
|
|
|
|
|
id: story.slug,
|
|
|
|
|
title: c.titre || story.name,
|
2026-03-05 22:55:19 +01:00
|
|
|
category: c.category || 'tout-public',
|
2026-03-05 22:22:32 +01:00
|
|
|
retired: c.retire || false,
|
|
|
|
|
age: c.age || '',
|
|
|
|
|
duration: c.duree || '',
|
2026-03-05 22:55:19 +01:00
|
|
|
summary: c.resume || '',
|
2026-03-05 22:22:32 +01:00
|
|
|
credits: c.credits || '',
|
|
|
|
|
image: c.image?.filename || '',
|
|
|
|
|
gallery: (c.galerie || []).map((a: any) => a.filename),
|
2026-03-05 22:55:19 +01:00
|
|
|
dossierPro: c.dossier?.filename || '',
|
2026-03-05 22:22:32 +01:00
|
|
|
_blok: c,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function mapStoryToEvent(story: any): AgendaEvent {
|
|
|
|
|
const c = story.content;
|
|
|
|
|
return {
|
|
|
|
|
id: story.uuid,
|
|
|
|
|
date: c.date || '',
|
|
|
|
|
location: c.lieu || '',
|
|
|
|
|
spectacleId: extractSlug(c.spectacle_slug),
|
|
|
|
|
bookingLink: c.lien_reservation || null,
|
|
|
|
|
_blok: c,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function fetchSpectacles(): Promise<Spectacle[]> {
|
|
|
|
|
const api = useStoryblokApi();
|
|
|
|
|
const { data } = await api.get('cdn/stories', {
|
|
|
|
|
content_type: 'spectacle',
|
|
|
|
|
version: getVersion(),
|
|
|
|
|
per_page: 100,
|
|
|
|
|
});
|
|
|
|
|
return data.stories.map(mapStoryToSpectacle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function fetchAgenda(): Promise<AgendaEvent[]> {
|
|
|
|
|
const api = useStoryblokApi();
|
|
|
|
|
const { data } = await api.get('cdn/stories', {
|
|
|
|
|
content_type: 'evenement',
|
|
|
|
|
version: getVersion(),
|
|
|
|
|
per_page: 100,
|
|
|
|
|
});
|
|
|
|
|
return data.stories.map(mapStoryToEvent);
|
|
|
|
|
}
|