From 0fb30e0ac5a280a2d7e39438ceb9b6ee6415e44a Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Tue, 27 Dec 2022 17:16:21 -0800 Subject: [PATCH] Implement retrieveCookie retrieveCookie fetches and parses cookies. This util should help us further reduce places where we're extracting and parsing with cookies in the business logic --- components/HeaderMenu/index.tsx | 21 +++++++-------------- types/GranblueCookie.d.ts | 5 +++++ utils/retrieveCookies.tsx | 23 +++++++++++++++++++++++ 3 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 types/GranblueCookie.d.ts create mode 100644 utils/retrieveCookies.tsx diff --git a/components/HeaderMenu/index.tsx b/components/HeaderMenu/index.tsx index 354e897a..14be9367 100644 --- a/components/HeaderMenu/index.tsx +++ b/components/HeaderMenu/index.tsx @@ -1,7 +1,9 @@ import React, { useEffect, useState } from 'react' -import { getCookie, setCookie } from 'cookies-next' import { useRouter } from 'next/router' import { useTranslation } from 'next-i18next' +import { setCookie } from 'cookies-next' +import classNames from 'classnames' +import retrieveCookies from '~utils/retrieveCookies' import Link from 'next/link' import * as Switch from '@radix-ui/react-switch' @@ -14,7 +16,6 @@ import LoginModal from '~components/LoginModal' import SignupModal from '~components/SignupModal' import './index.scss' -import { accountState } from '~utils/accountState' interface Props { authenticated: boolean @@ -25,22 +26,14 @@ interface Props { } const HeaderMenu = (props: Props) => { + // Setup const router = useRouter() + const data: GranblueCookie | undefined = retrieveCookies() const { t } = useTranslation('common') - const accountCookie = getCookie('account') - const accountData: AccountCookie = accountCookie - ? JSON.parse(accountCookie as string) - : null // Refs const ref: React.RefObject = React.createRef() - const userCookie = getCookie('user') - const userData: UserCookie = userCookie - ? JSON.parse(userCookie as string) - : null - - const localeCookie = getCookie('NEXT_LOCALE') useEffect(() => { const handleClickOutside = (event: Event) => { if ( @@ -61,9 +54,9 @@ const HeaderMenu = (props: Props) => { const [checked, setChecked] = useState(false) useEffect(() => { - const locale = localeCookie + const locale = data?.locale setChecked(locale === 'ja' ? true : false) - }, [localeCookie]) + }, [data?.locale]) function handleCheckedChange(value: boolean) { const language = value ? 'ja' : 'en' diff --git a/types/GranblueCookie.d.ts b/types/GranblueCookie.d.ts new file mode 100644 index 00000000..eb0cca52 --- /dev/null +++ b/types/GranblueCookie.d.ts @@ -0,0 +1,5 @@ +interface GranblueCookie { + account: AccountCookie + user: UserCookie + locale: string +} diff --git a/utils/retrieveCookies.tsx b/utils/retrieveCookies.tsx new file mode 100644 index 00000000..3577625a --- /dev/null +++ b/utils/retrieveCookies.tsx @@ -0,0 +1,23 @@ +import { getCookies } from 'cookies-next' +import { NextApiRequest, NextApiResponse } from 'next' + +export default function retrieveCookies( + req?: NextApiRequest, + res?: NextApiResponse +): GranblueCookie | undefined { + const cookies = getCookies({ req, res }) + if (!cookies) return undefined + + const { + account: accountData, + user: userData, + NEXT_LOCALE: localeData, + } = cookies + if (!accountData || !userData) return undefined + + const account = JSON.parse(decodeURIComponent(accountData)) ?? undefined + const user = JSON.parse(decodeURIComponent(userData)) ?? undefined + const locale = localeData as string + + return { account, user, locale } +}