-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
r - samler vår context til en provider
- Loading branch information
Showing
12 changed files
with
85 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {createContext, Dispatch} from "react"; | ||
import {ValideringActionTypes, ValideringState} from "../validering.ts"; | ||
|
||
type TDigisosContext = { | ||
analytics: { | ||
analyticsData: Partial<AnalyticsData>; | ||
setAnalyticsData: (data: Partial<AnalyticsData>) => void; | ||
}; | ||
validering: { | ||
state: ValideringState; | ||
dispatch: Dispatch<ValideringActionTypes>; | ||
}; | ||
}; | ||
|
||
export const DigisosContext = createContext<TDigisosContext | undefined>(undefined); | ||
|
||
export type AnalyticsData = { | ||
selectedKategorier?: string[]; | ||
situasjonEndret?: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {ReactNode, useReducer, useState} from "react"; | ||
import {initialValideringState, valideringsReducer} from "../validering.ts"; | ||
import {AnalyticsData, DigisosContext} from "./DigisosContext.tsx"; | ||
|
||
export const DigisosContextProvider = ({children}: {children: ReactNode}) => { | ||
const [state, dispatch] = useReducer(valideringsReducer, initialValideringState); | ||
|
||
const [analyticsData, setAnalyticsDataState] = useState<AnalyticsData>({}); | ||
const setAnalyticsData = (data: Partial<AnalyticsData>) => { | ||
setAnalyticsDataState((prevData) => ({ | ||
...prevData, | ||
...data, | ||
})); | ||
}; | ||
|
||
return ( | ||
<DigisosContext.Provider | ||
value={{ | ||
analytics: { | ||
analyticsData, | ||
setAnalyticsData, | ||
}, | ||
validering: { | ||
state, | ||
dispatch, | ||
}, | ||
}} | ||
> | ||
{children} | ||
</DigisosContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,17 @@ | ||
import {QueryClient, QueryClientProvider} from "@tanstack/react-query"; | ||
import {ValideringsContextProvider} from "./ValideringContextProvider.tsx"; | ||
import {AnalyticsProvider} from "./AnalyticsContextProvider.tsx"; | ||
import {DigisosContextProvider} from "./DigisosContextProvider.tsx"; | ||
import {ReactQueryDevtools} from "@tanstack/react-query-devtools"; | ||
|
||
const queryClient = new QueryClient(); | ||
export const Providers = ({children}: {children: React.ReactNode}) => { | ||
return ( | ||
<ValideringsContextProvider> | ||
<DigisosContextProvider> | ||
<QueryClientProvider client={queryClient}> | ||
<AnalyticsProvider> | ||
{children} | ||
<div aria-hidden={"true"}> | ||
<ReactQueryDevtools initialIsOpen={false} /> | ||
</div> | ||
</AnalyticsProvider> | ||
{children} | ||
<div aria-hidden={"true"}> | ||
<ReactQueryDevtools initialIsOpen={false} /> | ||
</div> | ||
</QueryClientProvider> | ||
</ValideringsContextProvider> | ||
</DigisosContextProvider> | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {useContext} from "react"; | ||
import {DigisosContext} from "./DigisosContext.tsx"; | ||
|
||
export const useAnalyticsContext = () => { | ||
const context = useContext(DigisosContext)?.analytics; | ||
if (!context) throw new Error("useAnalyticsContext must be used within a AnalyticsProvider"); | ||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {useContext} from "react"; | ||
import {DigisosContext} from "./DigisosContext.tsx"; | ||
|
||
export const useValideringContext = () => { | ||
const context = useContext(DigisosContext)?.validering; | ||
if (!context) { | ||
throw new Error("useValideringContext must be used within a ValideringContextProvider"); | ||
} | ||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters