2021-10-24 06:09:43 +02:00
|
|
|
import { createContext, useState, useEffect, useMemo, useContext } from 'react'
|
2021-10-26 16:38:55 +02:00
|
|
|
import { useTheme } from 'next-themes'
|
2021-10-24 06:09:43 +02:00
|
|
|
import setLanguage from 'next-translate/setLanguage'
|
2022-02-19 23:20:33 +01:00
|
|
|
import useTranslation from 'next-translate/useTranslation'
|
2021-10-24 06:09:43 +02:00
|
|
|
|
2022-01-01 20:42:25 +01:00
|
|
|
import { Authentication, PagePropsWithAuthentication } from '.'
|
2021-10-26 16:38:55 +02:00
|
|
|
import { UserCurrent } from '../../models/User'
|
2022-02-19 23:20:33 +01:00
|
|
|
import { Language, Theme } from '../../models/UserSettings'
|
2021-10-24 06:09:43 +02:00
|
|
|
|
|
|
|
export interface AuthenticationValue {
|
|
|
|
authentication: Authentication
|
|
|
|
user: UserCurrent
|
2022-02-19 23:20:33 +01:00
|
|
|
setUser: React.Dispatch<React.SetStateAction<UserCurrent>>
|
2021-10-24 06:09:43 +02:00
|
|
|
}
|
|
|
|
|
2022-02-19 23:20:33 +01:00
|
|
|
const defaultAuthenticationContext: AuthenticationValue = {} as any
|
2021-10-24 06:09:43 +02:00
|
|
|
const AuthenticationContext = createContext<AuthenticationValue>(
|
2022-02-19 23:20:33 +01:00
|
|
|
defaultAuthenticationContext
|
2021-10-24 06:09:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
export const AuthenticationProvider: React.FC<PagePropsWithAuthentication> = (
|
|
|
|
props
|
|
|
|
) => {
|
2022-02-19 23:20:33 +01:00
|
|
|
const { lang } = useTranslation()
|
|
|
|
const { theme, setTheme } = useTheme()
|
|
|
|
const [user, setUser] = useState<UserCurrent>(props.authentication.user)
|
2021-10-24 06:09:43 +02:00
|
|
|
|
|
|
|
const authentication = useMemo(() => {
|
|
|
|
return new Authentication(props.authentication.tokens)
|
2022-01-13 18:21:45 +01:00
|
|
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps -- We only want to run this memo once
|
|
|
|
}, [])
|
2021-10-24 06:09:43 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-02-19 23:20:33 +01:00
|
|
|
setLanguage(props.authentication.user.settings.language).catch(() => {})
|
|
|
|
setTheme(props.authentication.user.settings.theme)
|
|
|
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps -- We only want to run this effect once
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
authentication.api
|
|
|
|
.put('/users/current/settings', { theme, language: lang })
|
|
|
|
.then(() => {
|
|
|
|
setUser((oldUser) => {
|
|
|
|
return {
|
|
|
|
...oldUser,
|
|
|
|
settings: {
|
|
|
|
...oldUser.settings,
|
|
|
|
theme: theme as Theme,
|
|
|
|
language: lang as Language
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(() => {})
|
|
|
|
}, [theme, lang, authentication.api])
|
2021-10-24 06:09:43 +02:00
|
|
|
|
|
|
|
return (
|
2022-02-19 23:20:33 +01:00
|
|
|
<AuthenticationContext.Provider value={{ authentication, user, setUser }}>
|
2021-10-24 06:09:43 +02:00
|
|
|
{props.children}
|
|
|
|
</AuthenticationContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useAuthentication = (): AuthenticationValue => {
|
|
|
|
return useContext(AuthenticationContext)
|
|
|
|
}
|