Files
fltr-app/apis/seasonApi.ts
Cron1cle aedd87416f updated
2025-10-09 17:20:40 +02:00

77 lines
1.8 KiB
TypeScript

export type RawSeasonParticipant = {
id: { seasonId: number; personId: number };
person: {
personId: number;
name: string;
birthDate: string;
imageUrl: string | null;
};
partner: unknown | null;
};
export type RawSeason = {
seasonId: number;
show: number;
seasonNumber: number;
startDate?: string;
endDate?: string | null;
moderators: unknown[];
seasonParticipants: RawSeasonParticipant[];
};
export type SeasonParticipant = {
id: number;
name: string;
birthYear?: number;
imageUri: string;
};
export type Season = {
id: number;
showId: number;
seasonNumber: number;
startDate?: string;
endDate?: string | null;
participants: SeasonParticipant[];
};
const SEASON_BASE_URL = "http://45.157.177.99:8080/shows";
export async function getSeason(
showId: number,
seasonNumber: number
): Promise<Season | null> {
const url = `${SEASON_BASE_URL}/${showId}/seasons/${seasonNumber}`;
try {
console.log("[getSeason] Fetch:", url);
const res = await fetch(url);
console.log("[getSeason] Status:", res.status);
if (res.status === 404) return null;
if (!res.ok) throw new Error("Season fetch failed " + res.status);
const raw: RawSeason = await res.json();
const participants: SeasonParticipant[] = raw.seasonParticipants.map(
(p) => ({
id: p.person.personId,
name: p.person.name,
birthYear: p.person.birthDate
? Number(p.person.birthDate.slice(0, 4))
: undefined,
imageUri:
p.person.imageUrl ??
`https://i.pravatar.cc/300?img=${Math.random() * 70}`,
})
);
return {
id: raw.seasonId,
showId: raw.show,
seasonNumber: raw.seasonNumber,
startDate: raw.startDate,
endDate: raw.endDate,
participants,
};
} catch (e) {
console.error("getSeason error:", e);
throw e;
}
}