From aa0de08f27c259892e8893a9086091e31285cb87 Mon Sep 17 00:00:00 2001 From: Arthur L-Brjc Date: Thu, 31 Oct 2024 16:24:42 +0100 Subject: [PATCH] fix: context actualites --- services/actualites.service.ts | 4 ++-- utils/actualitesContext.tsx | 18 +++++------------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/services/actualites.service.ts b/services/actualites.service.ts index 414663323..dcf537881 100644 --- a/services/actualites.service.ts +++ b/services/actualites.service.ts @@ -1,9 +1,9 @@ import { Actualites } from 'interfaces/actualites' import { fetchJson } from 'utils/httpClient' -export async function getActualites(): Promise { +export async function getActualites(): Promise { const url = process.env.NEXT_PUBLIC_WORDPRESS_ACTUS_CEJ_LINK - if (!url) return null + if (!url) return const { content: { diff --git a/utils/actualitesContext.tsx b/utils/actualitesContext.tsx index ac446a7d0..33350b3df 100644 --- a/utils/actualitesContext.tsx +++ b/utils/actualitesContext.tsx @@ -11,24 +11,21 @@ import { import { Actualites } from 'interfaces/actualites' import { getActualites } from 'services/actualites.service' -const ActualitesContext = createContext( - undefined -) +const ActualitesContext = createContext(undefined) export function ActualitesProvider({ children, actualitesForTests, }: { children: ReactNode - actualitesForTests?: Actualites | null + actualitesForTests?: Actualites }) { - const [actualites, setActualites] = useState( + const [actualites, setActualites] = useState( actualitesForTests ) useEffect(() => { if (actualites === undefined) getActualites().then(setActualites) - return () => setActualites(undefined) }, [actualites]) return ( @@ -38,11 +35,6 @@ export function ActualitesProvider({ ) } -export function useActualites(): Actualites | null { - const actualites = useContext(ActualitesContext) - if (actualites === undefined) { - throw new Error('useActualites must be used within ActualitesProvider') - } - - return actualites +export function useActualites(): Actualites | undefined { + return useContext(ActualitesContext) }