Skip to content

Commit

Permalink
Merge pull request #97 from elsoul/fix/router
Browse files Browse the repository at this point in the history
fix routing. use i18n router
  • Loading branch information
KishiTheMechanic authored Nov 25, 2023
2 parents 27e530c + 99cdaf4 commit 35e4cb0
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 44 deletions.
16 changes: 8 additions & 8 deletions src/components/pages/action/ResetPasswordAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { z } from 'zod'
import { useForm, Controller } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import useToastMessage from '@/hooks/useToastMessage'
import { useRouter } from 'next/router'
import useI18nRouter from '@/hooks/useI18nRouter'

const schema = z.object({
password: passwordSchema,
Expand All @@ -28,7 +28,7 @@ export default function ResetPasswordAction({ oobCode }: Props) {
const { t } = useTranslation()
const [email, setEmail] = useState('')
const addToast = useToastMessage()
const router = useRouter()
const { routerPush } = useI18nRouter()

const verifyEmail = useCallback(async () => {
try {
Expand All @@ -43,9 +43,9 @@ export default function ResetPasswordAction({ oobCode }: Props) {
title: t('auth:verifyErrorTitle'),
description: t('auth:verifyErrorBody'),
})
await router.push('/auth/login')
await routerPush('/auth/login')
}
}, [router, t, oobCode, addToast])
}, [routerPush, t, oobCode, addToast])

useEffect(() => {
void (async () => {
Expand Down Expand Up @@ -79,7 +79,7 @@ export default function ResetPasswordAction({ oobCode }: Props) {
title: t('auth:resetPasswordSuccessTitle'),
description: t('auth:resetPasswordSuccessBody'),
})
await router.push('/auth/login')
await routerPush('/auth/login')
} catch (err) {
console.error(err)
addToast({
Expand All @@ -91,12 +91,12 @@ export default function ResetPasswordAction({ oobCode }: Props) {
setRegisterLoading(false)
}
},
[oobCode, t, router, addToast]
[oobCode, t, routerPush, addToast],
)

const isDisabled = useMemo(
() => isLoading || isRegisterLoading || errors.password != null,
[isLoading, isRegisterLoading, errors.password]
[isLoading, isRegisterLoading, errors.password],
)

if (isLoading) {
Expand Down Expand Up @@ -155,7 +155,7 @@ export default function ResetPasswordAction({ oobCode }: Props) {
isDisabled
? 'cursor-not-allowed bg-gray-300 text-gray-500 dark:bg-gray-800 dark:text-gray-400'
: 'bg-gray-900 text-white hover:bg-gray-700 dark:bg-gray-50 dark:text-gray-900 dark:hover:bg-gray-200',
'w-full px-3 py-2 text-center text-lg font-bold'
'w-full px-3 py-2 text-center text-lg font-bold',
)}
>
{t('auth:registerAccount')}
Expand Down
8 changes: 4 additions & 4 deletions src/components/pages/action/VerifyEmailAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { useTranslation } from 'next-i18next'
import { auth } from '@/lib/firebase'
import { applyActionCode } from 'firebase/auth'
import AppLoading from '@/components/loading/AppLoading'
import { useRouter } from 'next/router'
import { CheckCircleIcon } from '@heroicons/react/24/outline'
import useToastMessage from '@/hooks/useToastMessage'
import Button from '@/components/common/atoms/Button'
import useI18nRouter from '@/hooks/useI18nRouter'

type Props = {
oobCode: string
Expand All @@ -16,7 +16,7 @@ type Props = {
export default function VerifyEmailAction({ oobCode }: Props) {
const [isLoading, setLoading] = useState(true)
const { t } = useTranslation()
const router = useRouter()
const { routerPush } = useI18nRouter()
const addToast = useToastMessage()

const verifyUser = useCallback(async () => {
Expand All @@ -36,10 +36,10 @@ export default function VerifyEmailAction({ oobCode }: Props) {
title: t('auth:verifyErrorTitle'),
description: t('auth:verifyErrorBody'),
})
await router.push('/auth/login')
await routerPush('/auth/login')
}
}
}, [router, t, oobCode, addToast, setLoading])
}, [routerPush, t, oobCode, addToast, setLoading])

useEffect(() => {
void (async () => {
Expand Down
14 changes: 7 additions & 7 deletions src/components/pages/auth/RegisterScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {
} from 'firebase/auth'
import { auth } from '@/lib/firebase'
import useToastMessage from '@/hooks/useToastMessage'
import { useRouter } from 'next/router'
import { emailSchema, passwordSchema, privacySchema } from '@/utils/form'
import { z } from 'zod'
import { zodResolver } from '@hookform/resolvers/zod'
import { useForm, Controller } from 'react-hook-form'
import Link from '@/components/routing/Link'
import useI18nRouter from '@/hooks/useI18nRouter'

const schema = z.object({
email: emailSchema,
Expand All @@ -29,7 +29,7 @@ export default function RegisterScreen() {
const isJapanese = useMemo(() => i18n.language === 'ja', [i18n])
const [isLoading, setLoading] = useState(false)
const addToast = useToastMessage()
const router = useRouter()
const { routerPush } = useI18nRouter()

const {
handleSubmit,
Expand All @@ -53,7 +53,7 @@ export default function RegisterScreen() {
const userCredential = await createUserWithEmailAndPassword(
auth,
data.email,
data.password
data.password,
)
await sendEmailVerification(userCredential.user)
await signOut(auth)
Expand All @@ -64,7 +64,7 @@ export default function RegisterScreen() {
description: t('auth:sentConfirmEmailBody'),
})

await router.push('/auth/check-email')
await routerPush('/auth/check-email')
} catch (err) {
console.error(err)

Expand All @@ -89,7 +89,7 @@ export default function RegisterScreen() {
}
}
},
[t, isJapanese, addToast, router]
[t, isJapanese, addToast, routerPush],
)

const isDisabled = useMemo(
Expand All @@ -98,7 +98,7 @@ export default function RegisterScreen() {
errors.email != null ||
errors.password != null ||
errors.privacy != null,
[isLoading, errors.email, errors.password, errors.privacy]
[isLoading, errors.email, errors.password, errors.privacy],
)

return (
Expand Down Expand Up @@ -211,7 +211,7 @@ export default function RegisterScreen() {
isDisabled
? 'cursor-not-allowed bg-gray-300 text-gray-500 dark:bg-gray-800 dark:text-gray-400'
: 'bg-gray-900 text-white hover:bg-gray-700 dark:bg-gray-50 dark:text-gray-900 dark:hover:bg-gray-200',
'w-full px-3 py-2 text-center text-lg font-bold'
'w-full px-3 py-2 text-center text-lg font-bold',
)}
>
{t('register')}
Expand Down
12 changes: 6 additions & 6 deletions src/components/pages/auth/ResetPasswordScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { zodResolver } from '@hookform/resolvers/zod'
import { useForm, Controller } from 'react-hook-form'
import Link from '@/components/routing/Link'
import useToastMessage from '@/hooks/useToastMessage'
import { useRouter } from 'next/router'
import useI18nRouter from '@/hooks/useI18nRouter'

const schema = z.object({
email: emailSchema,
Expand All @@ -22,7 +22,7 @@ export default function ResetPasswordScreen() {
const { t } = useTranslation()
const [isLoading, setLoading] = useState(false)
const addToast = useToastMessage()
const router = useRouter()
const { routerPush } = useI18nRouter()

const {
handleSubmit,
Expand All @@ -46,7 +46,7 @@ export default function ResetPasswordScreen() {
title: t('auth:sentResetPasswordRequest'),
description: t('auth:confirmEmail'),
})
await router.push('/auth/check-email')
await routerPush('/auth/check-email')
} catch (err) {
console.error(err)
if (err instanceof Error && err.message === 'Not verified') {
Expand Down Expand Up @@ -76,12 +76,12 @@ export default function ResetPasswordScreen() {
}
}
},
[t, addToast, router]
[t, addToast, routerPush],
)

const isDisabled = useMemo(
() => isLoading || errors.email != null,
[isLoading, errors.email]
[isLoading, errors.email],
)

return (
Expand Down Expand Up @@ -138,7 +138,7 @@ export default function ResetPasswordScreen() {
isDisabled
? 'cursor-not-allowed bg-gray-300 text-gray-500 dark:bg-gray-800 dark:text-gray-400'
: 'bg-gray-900 text-white hover:bg-gray-700 dark:bg-gray-50 dark:text-gray-900 dark:hover:bg-gray-200',
'w-full px-3 py-2 text-center text-lg font-bold'
'w-full px-3 py-2 text-center text-lg font-bold',
)}
>
{t('auth:reset')}
Expand Down
12 changes: 5 additions & 7 deletions src/layouts/auth/AuthLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import type { ReactNode } from 'react'
import { useEffect, useCallback } from 'react'
import CommonFooter from '@/layouts/common/CommonFooter'
import { User, signOut } from 'firebase/auth'

import { fetchQuery, graphql } from 'react-relay'

import { useRouter } from 'next/router'
import AuthHeader from './AuthHeader'
import { useRecoilState } from 'recoil'
import { defaultUser, userState } from '@/store/user'
import { auth } from '@/lib/firebase'
import { createEnvironment } from '@/lib/relayEnvironment'
import { AuthLayoutQuery } from '@/__generated__/AuthLayoutQuery.graphql'
import useI18nRouter from '@/hooks/useI18nRouter'

type Props = {
children: ReactNode
Expand All @@ -30,7 +28,7 @@ export const authLayoutQuery = graphql`
`

export default function AuthLayout({ children }: Props) {
const router = useRouter()
const { router, routerPush } = useI18nRouter()

const resetWindowScrollPosition = useCallback(() => {
const element = document.getElementById(mainContentId)
Expand Down Expand Up @@ -59,7 +57,7 @@ export default function AuthLayout({ children }: Props) {
const user = await fetchQuery<AuthLayoutQuery>(
createEnvironment(),
authLayoutQuery,
{}
{},
).toPromise()
if (user?.me?.id) {
setUser({
Expand All @@ -70,7 +68,7 @@ export default function AuthLayout({ children }: Props) {
iconUrl: user.me.iconUrl ?? '',
emailVerified: fbUser.emailVerified,
})
await router.push('/user/vertex-ai')
await routerPush('/user/vertex-ai')
} else {
setUser(defaultUser)
await signOut(auth)
Expand All @@ -79,7 +77,7 @@ export default function AuthLayout({ children }: Props) {
setUser(defaultUser)
}
},
[setUser, router]
[setUser, routerPush],
)

useEffect(() => {
Expand Down
24 changes: 12 additions & 12 deletions src/layouts/user/UserLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { ReactNode } from 'react'
import { useMemo, useCallback, useEffect, useState, Fragment } from 'react'
import { Transition, Dialog, Menu } from '@headlessui/react'
import { XMarkIcon, Bars3BottomLeftIcon } from '@heroicons/react/24/outline'
import { useRouter } from 'next/router'
import clsx from 'clsx'
import { userHeaderNav, userMenuNav } from '@/config/navs'
import { useTranslation } from 'next-i18next'
Expand All @@ -16,6 +15,7 @@ import Image from 'next/image'
import { fetchQuery, graphql } from 'react-relay'
import { UserLayoutQuery } from '@/__generated__/UserLayoutQuery.graphql'
import { createEnvironment } from '@/lib/relayEnvironment'
import useI18nRouter from '@/hooks/useI18nRouter'

type Props = {
children: ReactNode
Expand All @@ -34,7 +34,7 @@ export const userLayoutQuery = graphql`
`

export default function UserLayout({ children }: Props) {
const router = useRouter()
const { router, routerPush } = useI18nRouter()
const [sidebarOpen, setSidebarOpen] = useState(false)
const { t } = useTranslation()

Expand Down Expand Up @@ -70,7 +70,7 @@ export default function UserLayout({ children }: Props) {
const user = await fetchQuery<UserLayoutQuery>(
createEnvironment(),
userLayoutQuery,
{}
{},
).toPromise()
if (user?.me?.id) {
setUser({
Expand All @@ -84,14 +84,14 @@ export default function UserLayout({ children }: Props) {
} else {
setUser(defaultUser)
await signOut(auth)
await router.push('/auth/login')
await routerPush('/auth/login')
}
} else {
setUser(defaultUser)
await router.push('/auth/login')
await routerPush('/auth/login')
}
},
[setUser, router]
[setUser, routerPush],
)

useEffect(() => {
Expand Down Expand Up @@ -171,7 +171,7 @@ export default function UserLayout({ children }: Props) {
asPathWithoutLang === item.href
? 'bg-gray-50 text-gray-900 dark:bg-gray-900 dark:text-white'
: 'text-gray-700 hover:bg-gray-50 dark:text-gray-50 dark:hover:bg-gray-800',
'group flex items-center px-2 py-2 text-base font-medium'
'group flex items-center px-2 py-2 text-base font-medium',
)}
>
{item.icon && (
Expand All @@ -180,7 +180,7 @@ export default function UserLayout({ children }: Props) {
asPathWithoutLang === item.href
? 'text-gray-900 dark:text-white'
: 'text-gray-700 dark:text-gray-50',
'mr-4 h-6 w-6 flex-shrink-0'
'mr-4 h-6 w-6 flex-shrink-0',
)}
aria-hidden="true"
/>
Expand Down Expand Up @@ -212,7 +212,7 @@ export default function UserLayout({ children }: Props) {
asPathWithoutLang === item.href
? 'bg-gray-50 text-gray-900 dark:bg-gray-700 dark:text-white'
: 'text-gray-700 hover:bg-gray-50 dark:text-gray-50 dark:hover:bg-gray-800',
'group flex items-center px-2 py-2 text-sm font-medium'
'group flex items-center px-2 py-2 text-sm font-medium',
)}
>
{item.icon && (
Expand All @@ -221,7 +221,7 @@ export default function UserLayout({ children }: Props) {
asPathWithoutLang === item.href
? 'text-gray-900 dark:text-white'
: 'text-gray-700 dark:text-gray-50',
'mr-3 h-6 w-6 flex-shrink-0'
'mr-3 h-6 w-6 flex-shrink-0',
)}
aria-hidden="true"
/>
Expand Down Expand Up @@ -285,7 +285,7 @@ export default function UserLayout({ children }: Props) {
active
? 'bg-gray-50 text-gray-900 dark:bg-gray-700 dark:text-white'
: '',
'block px-4 py-2 text-sm text-gray-700 dark:text-gray-50'
'block px-4 py-2 text-sm text-gray-700 dark:text-gray-50',
)}
>
{t(item.name)}
Expand All @@ -306,7 +306,7 @@ export default function UserLayout({ children }: Props) {
active
? 'bg-gray-50 text-gray-900 dark:bg-gray-700 dark:text-white'
: '',
'block px-4 py-2 text-sm text-gray-700 hover:cursor-pointer dark:text-gray-50'
'block px-4 py-2 text-sm text-gray-700 hover:cursor-pointer dark:text-gray-50',
)}
>
{t('logout')}
Expand Down

0 comments on commit 35e4cb0

Please sign in to comment.