Update cookie usage in TopHeader and HeaderMenu
This commit is contained in:
parent
0c1fc9c1a1
commit
19d8fa4d9b
2 changed files with 251 additions and 232 deletions
|
|
@ -1,129 +1,141 @@
|
||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from "react"
|
||||||
import { useCookies } from 'react-cookie'
|
import { getCookie, setCookie } from "cookies-next"
|
||||||
import Router, { useRouter } from 'next/router'
|
import { useRouter } from "next/router"
|
||||||
import { useTranslation } from 'next-i18next'
|
import { useTranslation } from "next-i18next"
|
||||||
|
|
||||||
import Link from 'next/link'
|
import Link from "next/link"
|
||||||
import * as Switch from '@radix-ui/react-switch'
|
import * as Switch from "@radix-ui/react-switch"
|
||||||
|
|
||||||
import AboutModal from '~components/AboutModal'
|
import AboutModal from "~components/AboutModal"
|
||||||
import AccountModal from '~components/AccountModal'
|
import AccountModal from "~components/AccountModal"
|
||||||
import LoginModal from '~components/LoginModal'
|
import LoginModal from "~components/LoginModal"
|
||||||
import SignupModal from '~components/SignupModal'
|
import SignupModal from "~components/SignupModal"
|
||||||
|
|
||||||
import './index.scss'
|
import "./index.scss"
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
authenticated: boolean,
|
authenticated: boolean
|
||||||
username?: string,
|
username?: string
|
||||||
logout?: () => void
|
logout?: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
const HeaderMenu = (props: Props) => {
|
const HeaderMenu = (props: Props) => {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const { t } = useTranslation('common')
|
const { t } = useTranslation("common")
|
||||||
|
|
||||||
const [accountCookies] = useCookies(['account'])
|
|
||||||
const [userCookies] = useCookies(['user'])
|
|
||||||
const [cookies, setCookies] = useCookies()
|
|
||||||
|
|
||||||
const [checked, setChecked] = useState(false)
|
const accountCookie = getCookie("account")
|
||||||
|
const accountData: AccountCookie = accountCookie
|
||||||
|
? JSON.parse(accountCookie as string)
|
||||||
|
: null
|
||||||
|
|
||||||
useEffect(() => {
|
const userCookie = getCookie("user")
|
||||||
const locale = cookies['NEXT_LOCALE']
|
const userData: UserCookie = userCookie
|
||||||
setChecked((locale === 'ja') ? true : false)
|
? JSON.parse(userCookie as string)
|
||||||
}, [cookies])
|
: null
|
||||||
|
|
||||||
function handleCheckedChange(value: boolean) {
|
const localeCookie = getCookie("NEXT_LOCALE")
|
||||||
const language = (value) ? 'ja' : 'en'
|
|
||||||
setCookies('NEXT_LOCALE', language, { path: '/'})
|
|
||||||
router.push(router.asPath, undefined, { locale: language })
|
|
||||||
}
|
|
||||||
|
|
||||||
function authItems() {
|
const [checked, setChecked] = useState(false)
|
||||||
return (
|
|
||||||
<nav>
|
|
||||||
<ul className="Menu auth">
|
|
||||||
<div className="MenuGroup">
|
|
||||||
<li className="MenuItem profile">
|
|
||||||
<Link href={`/${accountCookies.account.username}` || ''} passHref>
|
|
||||||
<div>
|
|
||||||
<span>{accountCookies.account.username}</span>
|
|
||||||
<img
|
|
||||||
alt={userCookies.user.picture}
|
|
||||||
className={`profile ${userCookies.user.element}`}
|
|
||||||
srcSet={`/profile/${userCookies.user.picture}.png,
|
|
||||||
/profile/${userCookies.user.picture}@2x.png 2x`}
|
|
||||||
src={`/profile/${userCookies.user.picture}.png`}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</Link>
|
|
||||||
</li>
|
|
||||||
<li className="MenuItem">
|
|
||||||
<Link href={`/saved` || ''}>{t('menu.saved')}</Link>
|
|
||||||
</li>
|
|
||||||
</div>
|
|
||||||
<div className="MenuGroup">
|
|
||||||
<li className="MenuItem">
|
|
||||||
<Link href='/teams'>{t('menu.teams')}</Link>
|
|
||||||
</li>
|
|
||||||
|
|
||||||
<li className="MenuItem disabled">
|
useEffect(() => {
|
||||||
<div>
|
const locale = localeCookie
|
||||||
<span>{t('menu.guides')}</span>
|
setChecked(locale === "ja" ? true : false)
|
||||||
<i className="tag">{t('coming_soon')}</i>
|
}, [localeCookie])
|
||||||
</div>
|
|
||||||
</li>
|
|
||||||
</div>
|
|
||||||
<div className="MenuGroup">
|
|
||||||
<AboutModal />
|
|
||||||
<AccountModal />
|
|
||||||
<li className="MenuItem" onClick={props.logout}>
|
|
||||||
<span>{t('menu.logout')}</span>
|
|
||||||
</li>
|
|
||||||
</div>
|
|
||||||
</ul>
|
|
||||||
</nav>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
function unauthItems() {
|
function handleCheckedChange(value: boolean) {
|
||||||
return (
|
const language = value ? "ja" : "en"
|
||||||
<ul className="Menu unauth">
|
setCookie("NEXT_LOCALE", language, { path: "/" })
|
||||||
<div className="MenuGroup">
|
router.push(router.asPath, undefined, { locale: language })
|
||||||
<li className="MenuItem language">
|
}
|
||||||
<span>{t('menu.language')}</span>
|
|
||||||
<Switch.Root className="Switch" onCheckedChange={handleCheckedChange} checked={checked}>
|
function authItems() {
|
||||||
<Switch.Thumb className="Thumb" />
|
return (
|
||||||
<span className="left">JP</span>
|
<nav>
|
||||||
<span className="right">EN</span>
|
<ul className="Menu auth">
|
||||||
</Switch.Root>
|
<div className="MenuGroup">
|
||||||
</li>
|
<li className="MenuItem profile">
|
||||||
|
<Link href={`/${accountData.username}` || ""} passHref>
|
||||||
|
<div>
|
||||||
|
<span>{accountData.username}</span>
|
||||||
|
<img
|
||||||
|
alt={userData.picture}
|
||||||
|
className={`profile ${userData.element}`}
|
||||||
|
srcSet={`/profile/${userData.picture}.png,
|
||||||
|
/profile/${userData.picture}@2x.png 2x`}
|
||||||
|
src={`/profile/${userData.picture}.png`}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="MenuGroup">
|
</Link>
|
||||||
<li className="MenuItem">
|
</li>
|
||||||
<Link href='/teams'>{t('menu.teams')}</Link>
|
<li className="MenuItem">
|
||||||
</li>
|
<Link href={`/saved` || ""}>{t("menu.saved")}</Link>
|
||||||
|
</li>
|
||||||
|
</div>
|
||||||
|
<div className="MenuGroup">
|
||||||
|
<li className="MenuItem">
|
||||||
|
<Link href="/teams">{t("menu.teams")}</Link>
|
||||||
|
</li>
|
||||||
|
|
||||||
<li className="MenuItem disabled">
|
<li className="MenuItem disabled">
|
||||||
<div>
|
<div>
|
||||||
<span>{t('menu.guides')}</span>
|
<span>{t("menu.guides")}</span>
|
||||||
<i className="tag">{t('coming_soon')}</i>
|
<i className="tag">{t("coming_soon")}</i>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
</div>
|
</div>
|
||||||
<div className="MenuGroup">
|
<div className="MenuGroup">
|
||||||
<AboutModal />
|
<AboutModal />
|
||||||
</div>
|
<AccountModal />
|
||||||
<div className="MenuGroup">
|
<li className="MenuItem" onClick={props.logout}>
|
||||||
<LoginModal />
|
<span>{t("menu.logout")}</span>
|
||||||
<SignupModal />
|
</li>
|
||||||
</div>
|
</div>
|
||||||
</ul>
|
</ul>
|
||||||
)
|
</nav>
|
||||||
}
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return (props.authenticated) ? authItems() : unauthItems()
|
function unauthItems() {
|
||||||
|
return (
|
||||||
|
<ul className="Menu unauth">
|
||||||
|
<div className="MenuGroup">
|
||||||
|
<li className="MenuItem language">
|
||||||
|
<span>{t("menu.language")}</span>
|
||||||
|
<Switch.Root
|
||||||
|
className="Switch"
|
||||||
|
onCheckedChange={handleCheckedChange}
|
||||||
|
checked={checked}
|
||||||
|
>
|
||||||
|
<Switch.Thumb className="Thumb" />
|
||||||
|
<span className="left">JP</span>
|
||||||
|
<span className="right">EN</span>
|
||||||
|
</Switch.Root>
|
||||||
|
</li>
|
||||||
|
</div>
|
||||||
|
<div className="MenuGroup">
|
||||||
|
<li className="MenuItem">
|
||||||
|
<Link href="/teams">{t("menu.teams")}</Link>
|
||||||
|
</li>
|
||||||
|
|
||||||
|
<li className="MenuItem disabled">
|
||||||
|
<div>
|
||||||
|
<span>{t("menu.guides")}</span>
|
||||||
|
<i className="tag">{t("coming_soon")}</i>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
</div>
|
||||||
|
<div className="MenuGroup">
|
||||||
|
<AboutModal />
|
||||||
|
</div>
|
||||||
|
<div className="MenuGroup">
|
||||||
|
<LoginModal />
|
||||||
|
<SignupModal />
|
||||||
|
</div>
|
||||||
|
</ul>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return props.authenticated ? authItems() : unauthItems()
|
||||||
}
|
}
|
||||||
|
|
||||||
export default HeaderMenu
|
export default HeaderMenu
|
||||||
|
|
|
||||||
|
|
@ -1,147 +1,154 @@
|
||||||
import React, { useEffect } from 'react'
|
import React from "react"
|
||||||
import { useSnapshot } from 'valtio'
|
import { useSnapshot } from "valtio"
|
||||||
import { useCookies } from 'react-cookie'
|
import { getCookie, deleteCookie } from "cookies-next"
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from "next/router"
|
||||||
import { useTranslation } from 'next-i18next'
|
import { useTranslation } from "next-i18next"
|
||||||
|
|
||||||
import clonedeep from 'lodash.clonedeep'
|
import clonedeep from "lodash.clonedeep"
|
||||||
|
|
||||||
import api from '~utils/api'
|
import api from "~utils/api"
|
||||||
import { accountState, initialAccountState } from '~utils/accountState'
|
import { accountState, initialAccountState } from "~utils/accountState"
|
||||||
import { appState, initialAppState } from '~utils/appState'
|
import { appState, initialAppState } from "~utils/appState"
|
||||||
|
|
||||||
import Header from '~components/Header'
|
import Header from "~components/Header"
|
||||||
import Button from '~components/Button'
|
import Button from "~components/Button"
|
||||||
import HeaderMenu from '~components/HeaderMenu'
|
import HeaderMenu from "~components/HeaderMenu"
|
||||||
|
|
||||||
const TopHeader = () => {
|
const TopHeader = () => {
|
||||||
const { t } = useTranslation('common')
|
const { t } = useTranslation("common")
|
||||||
|
|
||||||
// Cookies
|
// Cookies
|
||||||
const [accountCookies, setAccountCookie, removeAccountCookie] = useCookies(['account'])
|
const accountCookie = getCookie("account")
|
||||||
const [userCookies, setUserCookies, removeUserCookie] = useCookies(['user'])
|
const userCookie = getCookie("user")
|
||||||
|
|
||||||
const headers = (accountCookies.account != null) ? {
|
|
||||||
'Authorization': `Bearer ${accountCookies.account.access_token}`
|
|
||||||
} : {}
|
|
||||||
|
|
||||||
const { account } = useSnapshot(accountState)
|
const headers = {}
|
||||||
const { party } = useSnapshot(appState)
|
// accountCookies.account != null
|
||||||
const router = useRouter()
|
// ? {
|
||||||
|
// Authorization: `Bearer ${accountCookies.account.access_token}`,
|
||||||
|
// }
|
||||||
|
// : {}
|
||||||
|
|
||||||
function copyToClipboard() {
|
const { account } = useSnapshot(accountState)
|
||||||
const el = document.createElement('input')
|
const { party } = useSnapshot(appState)
|
||||||
el.value = window.location.href
|
const router = useRouter()
|
||||||
el.id = 'url-input'
|
|
||||||
document.body.appendChild(el)
|
|
||||||
|
|
||||||
el.select()
|
function copyToClipboard() {
|
||||||
document.execCommand('copy')
|
const el = document.createElement("input")
|
||||||
el.remove()
|
el.value = window.location.href
|
||||||
}
|
el.id = "url-input"
|
||||||
|
document.body.appendChild(el)
|
||||||
|
|
||||||
function newParty() {
|
el.select()
|
||||||
// Push the root URL
|
document.execCommand("copy")
|
||||||
router.push('/')
|
el.remove()
|
||||||
|
}
|
||||||
|
|
||||||
// Clean state
|
function newParty() {
|
||||||
const resetState = clonedeep(initialAppState)
|
// Push the root URL
|
||||||
Object.keys(resetState).forEach((key) => {
|
router.push("/")
|
||||||
appState[key] = resetState[key]
|
|
||||||
})
|
|
||||||
|
|
||||||
// Set party to be editable
|
// Clean state
|
||||||
appState.party.editable = true
|
const resetState = clonedeep(initialAppState)
|
||||||
}
|
Object.keys(resetState).forEach((key) => {
|
||||||
|
appState[key] = resetState[key]
|
||||||
|
})
|
||||||
|
|
||||||
function logout() {
|
// Set party to be editable
|
||||||
removeAccountCookie('account')
|
appState.party.editable = true
|
||||||
removeUserCookie('user')
|
}
|
||||||
|
|
||||||
// Clean state
|
function logout() {
|
||||||
const resetState = clonedeep(initialAccountState)
|
deleteCookie("account")
|
||||||
Object.keys(resetState).forEach((key) => {
|
deleteCookie("user")
|
||||||
if (key !== 'language')
|
|
||||||
accountState[key] = resetState[key]
|
|
||||||
})
|
|
||||||
|
|
||||||
if (router.route != '/new')
|
|
||||||
appState.party.editable = false
|
|
||||||
|
|
||||||
router.push('/')
|
// Clean state
|
||||||
return false
|
const resetState = clonedeep(initialAccountState)
|
||||||
}
|
Object.keys(resetState).forEach((key) => {
|
||||||
|
if (key !== "language") accountState[key] = resetState[key]
|
||||||
|
})
|
||||||
|
|
||||||
function toggleFavorite() {
|
if (router.route != "/new") appState.party.editable = false
|
||||||
if (party.favorited)
|
|
||||||
unsaveFavorite()
|
|
||||||
else
|
|
||||||
saveFavorite()
|
|
||||||
}
|
|
||||||
|
|
||||||
function saveFavorite() {
|
router.push("/")
|
||||||
if (party.id)
|
return false
|
||||||
api.saveTeam({ id: party.id, params: headers })
|
}
|
||||||
.then((response) => {
|
|
||||||
if (response.status == 201)
|
|
||||||
appState.party.favorited = true
|
|
||||||
})
|
|
||||||
else
|
|
||||||
console.error("Failed to save team: No party ID")
|
|
||||||
}
|
|
||||||
|
|
||||||
function unsaveFavorite() {
|
function toggleFavorite() {
|
||||||
if (party.id)
|
if (party.favorited) unsaveFavorite()
|
||||||
api.unsaveTeam({ id: party.id, params: headers })
|
else saveFavorite()
|
||||||
.then((response) => {
|
}
|
||||||
if (response.status == 200)
|
|
||||||
appState.party.favorited = false
|
|
||||||
})
|
|
||||||
else
|
|
||||||
console.error("Failed to unsave team: No party ID")
|
|
||||||
}
|
|
||||||
|
|
||||||
const leftNav = () => {
|
function saveFavorite() {
|
||||||
return (
|
if (party.id)
|
||||||
<div className="dropdown">
|
api.saveTeam({ id: party.id, params: headers }).then((response) => {
|
||||||
<Button icon="menu">{t('buttons.menu')}</Button>
|
if (response.status == 201) appState.party.favorited = true
|
||||||
{ (account.user) ?
|
})
|
||||||
<HeaderMenu authenticated={account.authorized} username={account.user.username} logout={logout} /> :
|
else console.error("Failed to save team: No party ID")
|
||||||
<HeaderMenu authenticated={account.authorized} />
|
}
|
||||||
}
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const saveButton = () => {
|
function unsaveFavorite() {
|
||||||
if (party.favorited)
|
if (party.id)
|
||||||
return (<Button icon="save" active={true} onClick={toggleFavorite}>Saved</Button>)
|
api.unsaveTeam({ id: party.id, params: headers }).then((response) => {
|
||||||
else
|
if (response.status == 200) appState.party.favorited = false
|
||||||
return (<Button icon="save" onClick={toggleFavorite}>Save</Button>)
|
})
|
||||||
}
|
else console.error("Failed to unsave team: No party ID")
|
||||||
|
}
|
||||||
|
|
||||||
const rightNav = () => {
|
const leftNav = () => {
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
{ (router.route === '/p/[party]' && account.user && (!party.user || party.user.id !== account.user.id)) ?
|
|
||||||
saveButton() : ''
|
|
||||||
}
|
|
||||||
{ (router.route === '/p/[party]') ?
|
|
||||||
<Button icon="link" onClick={copyToClipboard}>{t('buttons.copy')}</Button> : ''
|
|
||||||
}
|
|
||||||
<Button icon="new" onClick={newParty}>{t('buttons.new')}</Button>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Header
|
<div className="dropdown">
|
||||||
position="top"
|
<Button icon="menu">{t("buttons.menu")}</Button>
|
||||||
left={ leftNav() }
|
{account.user ? (
|
||||||
right={ rightNav() }
|
<HeaderMenu
|
||||||
/>
|
authenticated={account.authorized}
|
||||||
|
username={account.user.username}
|
||||||
|
logout={logout}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<HeaderMenu authenticated={account.authorized} />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const saveButton = () => {
|
||||||
|
if (party.favorited)
|
||||||
|
return (
|
||||||
|
<Button icon="save" active={true} onClick={toggleFavorite}>
|
||||||
|
Saved
|
||||||
|
</Button>
|
||||||
|
)
|
||||||
|
else
|
||||||
|
return (
|
||||||
|
<Button icon="save" onClick={toggleFavorite}>
|
||||||
|
Save
|
||||||
|
</Button>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const rightNav = () => {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
{router.route === "/p/[party]" &&
|
||||||
|
account.user &&
|
||||||
|
(!party.user || party.user.id !== account.user.id)
|
||||||
|
? saveButton()
|
||||||
|
: ""}
|
||||||
|
{router.route === "/p/[party]" ? (
|
||||||
|
<Button icon="link" onClick={copyToClipboard}>
|
||||||
|
{t("buttons.copy")}
|
||||||
|
</Button>
|
||||||
|
) : (
|
||||||
|
""
|
||||||
|
)}
|
||||||
|
<Button icon="new" onClick={newParty}>
|
||||||
|
{t("buttons.new")}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return <Header position="top" left={leftNav()} right={rightNav()} />
|
||||||
}
|
}
|
||||||
|
|
||||||
export default TopHeader
|
export default TopHeader
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue