mirror of
https://github.com/Steffo99/festa.git
synced 2024-12-22 22:54:22 +00:00
Globally configure SWR
This commit is contained in:
parent
6042e305a3
commit
9d20277706
1 changed files with 19 additions and 1 deletions
|
@ -9,18 +9,36 @@ import { StaticImageData } from 'next/image'
|
||||||
import { appWithTranslation } from 'next-i18next'
|
import { appWithTranslation } from 'next-i18next'
|
||||||
import { FestaLoginData } from '../types/user'
|
import { FestaLoginData } from '../types/user'
|
||||||
import {useStoredLogin} from "../hooks/useStoredLogin"
|
import {useStoredLogin} from "../hooks/useStoredLogin"
|
||||||
|
import { Fetcher, SWRConfig } from 'swr'
|
||||||
|
import axios, { AxiosRequestConfig } from 'axios'
|
||||||
|
|
||||||
|
|
||||||
const App = ({ Component, pageProps }: AppProps): JSX.Element => {
|
const App = ({ Component, pageProps }: AppProps): JSX.Element => {
|
||||||
const [login, setLogin] = useState<FestaLoginData | null>(null)
|
|
||||||
const [postcard, setPostcard] = useState<string | StaticImageData>(defaultPostcard)
|
const [postcard, setPostcard] = useState<string | StaticImageData>(defaultPostcard)
|
||||||
|
|
||||||
|
const [login, setLogin] = useState<FestaLoginData | null>(null)
|
||||||
useStoredLogin(setLogin)
|
useStoredLogin(setLogin)
|
||||||
|
|
||||||
|
const axiosConfig = {
|
||||||
|
headers: {
|
||||||
|
"Authorization": login ? `Bearer ${login.token}` : "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const swrConfig = {
|
||||||
|
fetcher: async (resource: string, localAxiosConfig: AxiosRequestConfig<any>) => {
|
||||||
|
const response = await axios.get(resource, {...axiosConfig, ...localAxiosConfig})
|
||||||
|
return response.data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PostcardContext.Provider value={[postcard, setPostcard]}>
|
<PostcardContext.Provider value={[postcard, setPostcard]}>
|
||||||
<LoginContext.Provider value={[login, setLogin]}>
|
<LoginContext.Provider value={[login, setLogin]}>
|
||||||
|
<SWRConfig value={swrConfig}>
|
||||||
<Postcard/>
|
<Postcard/>
|
||||||
<Component {...pageProps} />
|
<Component {...pageProps} />
|
||||||
|
</SWRConfig>
|
||||||
</LoginContext.Provider>
|
</LoginContext.Provider>
|
||||||
</PostcardContext.Provider>
|
</PostcardContext.Provider>
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue