Fix themes

This commit is contained in:
Justin Edmund 2022-12-25 20:11:50 -08:00
parent 6d8786e370
commit 61a02649cf
3 changed files with 17 additions and 8 deletions

View file

@ -1,4 +1,4 @@
import React, { useState } from 'react' import React, { useEffect, useState } from 'react'
import { getCookie, setCookie } from 'cookies-next' import { getCookie, setCookie } from 'cookies-next'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import { useTranslation } from 'next-i18next' import { useTranslation } from 'next-i18next'
@ -23,6 +23,7 @@ import { pictureData } from '~utils/pictureData'
import CrossIcon from '~public/icons/Cross.svg' import CrossIcon from '~public/icons/Cross.svg'
import './index.scss' import './index.scss'
import { useTheme } from 'next-themes'
type StateVariables = { type StateVariables = {
[key: string]: boolean [key: string]: boolean
@ -48,6 +49,10 @@ const AccountModal = (props: Props) => {
const locale = const locale =
router.locale && ['en', 'ja'].includes(router.locale) ? router.locale : 'en' router.locale && ['en', 'ja'].includes(router.locale) ? router.locale : 'en'
// useEffect only runs on the client, so now we can safely show the UI
const [mounted, setMounted] = useState(false)
const { theme: appTheme, setTheme: setAppTheme } = useTheme()
// Cookies // Cookies
const accountCookie = getCookie('account') const accountCookie = getCookie('account')
const userCookie = getCookie('user') const userCookie = getCookie('user')
@ -109,6 +114,7 @@ const AccountModal = (props: Props) => {
function handleThemeChange(value: string) { function handleThemeChange(value: string) {
setTheme(value) setTheme(value)
setAppTheme(value)
} }
// API calls // API calls
@ -131,7 +137,6 @@ const AccountModal = (props: Props) => {
.update(accountState.account.user?.id, object) .update(accountState.account.user?.id, object)
.then((response) => { .then((response) => {
const user = response.data const user = response.data
console.log(user)
const cookieObj = { const cookieObj = {
picture: user.avatar.picture, picture: user.avatar.picture,
@ -155,7 +160,6 @@ const AccountModal = (props: Props) => {
setOpen(false) setOpen(false)
changeLanguage(router, user.language) changeLanguage(router, user.language)
router.push(router.asPath, undefined)
}) })
} }
} }
@ -255,6 +259,14 @@ const AccountModal = (props: Props) => {
</SelectTableField> </SelectTableField>
) )
useEffect(() => {
setMounted(true)
}, [])
if (!mounted) {
return null
}
return ( return (
<Dialog open={open} onOpenChange={openChange}> <Dialog open={open} onOpenChange={openChange}>
<DialogTrigger asChild> <DialogTrigger asChild>

View file

@ -20,10 +20,6 @@ function MyApp({ Component, pageProps }: AppProps) {
user: userCookie ? JSON.parse(userCookie as string) : undefined, user: userCookie ? JSON.parse(userCookie as string) : undefined,
} }
const [useSystem, setUseSystem] = useState(
cookieData.user?.theme === 'system'
)
useEffect(() => { useEffect(() => {
setUserToken() setUserToken()
@ -46,7 +42,7 @@ function MyApp({ Component, pageProps }: AppProps) {
}, []) }, [])
return ( return (
<ThemeProvider enableSystem={useSystem}> <ThemeProvider>
<Layout> <Layout>
<Component {...pageProps} /> <Component {...pageProps} />
</Layout> </Layout>

View file

@ -7,5 +7,6 @@ export default function changeLanguage(
) { ) {
if (newLanguage !== router.locale) { if (newLanguage !== router.locale) {
setCookie('NEXT_LOCALE', newLanguage, { path: '/' }) setCookie('NEXT_LOCALE', newLanguage, { path: '/' })
router.push(router.asPath, undefined, { locale: newLanguage })
} }
} }