Skip to content

Commit

Permalink
fix: context actualites
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurlbrjc committed Nov 5, 2024
1 parent e284655 commit aa0de08
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
4 changes: 2 additions & 2 deletions services/actualites.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Actualites } from 'interfaces/actualites'
import { fetchJson } from 'utils/httpClient'

export async function getActualites(): Promise<Actualites | null> {
export async function getActualites(): Promise<Actualites | undefined> {
const url = process.env.NEXT_PUBLIC_WORDPRESS_ACTUS_CEJ_LINK
if (!url) return null
if (!url) return

const {
content: {
Expand Down
18 changes: 5 additions & 13 deletions utils/actualitesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,21 @@ import {
import { Actualites } from 'interfaces/actualites'
import { getActualites } from 'services/actualites.service'

const ActualitesContext = createContext<Actualites | null | undefined>(
undefined
)
const ActualitesContext = createContext<Actualites | undefined>(undefined)

export function ActualitesProvider({
children,
actualitesForTests,
}: {
children: ReactNode
actualitesForTests?: Actualites | null
actualitesForTests?: Actualites
}) {
const [actualites, setActualites] = useState<Actualites | null | undefined>(
const [actualites, setActualites] = useState<Actualites | undefined>(
actualitesForTests
)

useEffect(() => {
if (actualites === undefined) getActualites().then(setActualites)
return () => setActualites(undefined)
}, [actualites])

return (
Expand All @@ -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)
}

0 comments on commit aa0de08

Please sign in to comment.