1
Fork 0
mirror of https://github.com/Steffo99/festa.git synced 2024-10-16 23:17:26 +00:00
festa/pages/_app.tsx

50 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-05-20 11:59:24 +00:00
import '../styles/globals.css'
import type { AppProps } from 'next/app'
2022-05-24 16:55:21 +00:00
import { LoginContext } from '../contexts/login'
2022-05-30 03:19:49 +00:00
import { useState } from 'react'
2022-05-26 14:32:11 +00:00
import defaultPostcard from "../public/postcards/adi-goldstein-Hli3R6LKibo-unsplash.jpg"
2022-05-24 16:55:21 +00:00
import { Postcard } from '../components/Postcard'
import { PostcardContext } from '../contexts/postcard'
import { StaticImageData } from 'next/image'
2022-05-25 14:20:22 +00:00
import { appWithTranslation } from 'next-i18next'
2022-05-29 02:01:56 +00:00
import { FestaLoginData } from '../types/user'
import {useStoredLogin} from "../hooks/useStoredLogin"
2022-05-31 15:07:25 +00:00
import { Fetcher, SWRConfig } from 'swr'
import axios, { AxiosRequestConfig } from 'axios'
2022-05-27 00:46:30 +00:00
2022-05-20 11:59:24 +00:00
2022-05-25 14:20:22 +00:00
const App = ({ Component, pageProps }: AppProps): JSX.Element => {
2022-05-27 00:46:30 +00:00
const [postcard, setPostcard] = useState<string | StaticImageData>(defaultPostcard)
2022-05-31 15:07:25 +00:00
const [login, setLogin] = useState<FestaLoginData | null>(null)
2022-05-29 02:01:56 +00:00
useStoredLogin(setLogin)
2022-05-24 16:55:21 +00:00
2022-05-31 15:07:25 +00:00
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})
2022-06-01 16:54:59 +00:00
// To test loading uncomment the following line:
// await new Promise(res => setTimeout(res, 100000))
2022-05-31 15:07:25 +00:00
return response.data
}
}
2022-05-24 16:55:21 +00:00
return (
2022-05-27 00:46:30 +00:00
<PostcardContext.Provider value={[postcard, setPostcard]}>
<LoginContext.Provider value={[login, setLogin]}>
2022-05-31 15:07:25 +00:00
<SWRConfig value={swrConfig}>
2022-05-24 16:55:21 +00:00
<Postcard/>
<Component {...pageProps} />
2022-05-31 15:07:25 +00:00
</SWRConfig>
2022-05-24 16:55:21 +00:00
</LoginContext.Provider>
</PostcardContext.Provider>
)
2022-05-20 11:59:24 +00:00
}
2022-05-25 14:20:22 +00:00
export default appWithTranslation(App)