api: fetch shows implemented

This commit is contained in:
Cron1cle
2025-10-02 13:24:38 +02:00
parent 921a53a504
commit badf355a2d
5 changed files with 152 additions and 30 deletions

42
contexts/ShowContext.tsx Normal file
View File

@@ -0,0 +1,42 @@
import { getShows, Show } from "@/apis/showApi";
import { createContext, useContext, useEffect, useState } from "react";
type ShowContextType = {
shows: Show[];
loading: boolean;
error: string | null;
};
const ShowContext = createContext<ShowContextType | null>(null);
export const ShowProvider = ({ children }: { children: React.ReactNode }) => {
const [shows, setShows] = useState<Show[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
(async () => {
try {
const data = await getShows();
setShows(data);
} catch {
setError("Failed to fetch shows");
} finally {
setLoading(false);
}
})();
}, []);
return (
<ShowContext.Provider value={{ shows, loading, error }}>
{children}
</ShowContext.Provider>
);
};
export const useShowContext = () => {
const ctx = useContext(ShowContext);
if (!ctx)
throw new Error("useShowContext must be used within a ShowProvider");
return ctx;
};