2023-10-23 23:33:39 +02:00
|
|
|
import { createContext, useState, useEffect, useMemo, useContext } from "react"
|
|
|
|
import { useTheme } from "next-themes"
|
|
|
|
import setLanguage from "next-translate/setLanguage"
|
|
|
|
import useTranslation from "next-translate/useTranslation"
|
2021-10-24 06:09:43 +02:00
|
|
|
|
2023-10-23 23:33:39 +02:00
|
|
|
import type { PagePropsWithAuthentication } from "."
|
|
|
|
import { Authentication } from "."
|
|
|
|
import type { UserCurrent } from "../../models/User"
|
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>(
|
2023-10-23 23:33:39 +02:00
|
|
|
defaultAuthenticationContext,
|
2021-10-24 06:09:43 +02:00
|
|
|
)
|
|
|
|
|
2022-05-12 20:35:46 +02:00
|
|
|
export const AuthenticationProvider: React.FC<
|
|
|
|
React.PropsWithChildren<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(() => {
|
2022-08-30 21:30:06 +02:00
|
|
|
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)
|
2022-08-24 17:22:55 +02:00
|
|
|
return () => {
|
|
|
|
authentication?.socket?.disconnect()
|
|
|
|
}
|
2022-02-19 23:20:33 +01:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps -- We only want to run this effect once
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
authentication.api
|
2023-10-23 23:33:39 +02:00
|
|
|
.put("/users/current/settings", { theme, language: lang })
|
2022-04-09 01:03:15 +02:00
|
|
|
.then(({ data: userCurrentSettings }) => {
|
2022-02-19 23:20:33 +01:00
|
|
|
setUser((oldUser) => {
|
|
|
|
return {
|
|
|
|
...oldUser,
|
|
|
|
settings: {
|
|
|
|
...oldUser.settings,
|
2023-10-23 23:33:39 +02:00
|
|
|
...userCurrentSettings.settings,
|
|
|
|
},
|
2022-02-19 23:20:33 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.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)
|
|
|
|
}
|