hensei-web/components/BottomHeader/index.tsx
2022-02-23 16:43:08 -08:00

103 lines
3.4 KiB
TypeScript

import React from 'react'
import { useRouter } from 'next/router'
import { useCookies } from 'react-cookie'
import { useSnapshot } from 'valtio'
import clonedeep from 'lodash.clonedeep'
import * as AlertDialog from '@radix-ui/react-alert-dialog'
import Header from '~components/Header'
import Button from '~components/Button'
import api from '~utils/api'
import { accountState } from '~utils/accountState'
import { appState, initialAppState } from '~utils/appState'
import { ButtonType } from '~utils/enums'
import CrossIcon from '~public/icons/Cross.svg'
const BottomHeader = () => {
const account = useSnapshot(accountState)
const app = useSnapshot(appState)
const router = useRouter()
// Cookies
const [cookies] = useCookies(['user'])
const headers = (cookies.user != null) ? {
headers: {
'Authorization': `Bearer ${cookies.user.access_token}`
}
} : {}
function deleteTeam(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) {
if (appState.party.editable && appState.party.id) {
api.endpoints.parties.destroy(appState.party.id, headers)
.then(() => {
// Push to route
router.push('/')
// Clean state
const resetState = clonedeep(initialAppState)
Object.keys(resetState).forEach((key) => {
appState[key] = resetState[key]
})
// Set party to be editable
appState.party.editable = true
})
.catch((error) => {
console.error(error)
})
}
}
const leftNav = () => {
return (
<Button icon="edit" click={() => {}}>Add more info</Button>
)
}
const rightNav = () => {
if (app.party.editable && router.route === '/p/[party]') {
return (
<AlertDialog.Root>
<AlertDialog.Trigger className="Button destructive">
<span className='icon'>
<CrossIcon />
</span>
<span className="text">Delete team</span>
</AlertDialog.Trigger>
<AlertDialog.Portal>
<AlertDialog.Overlay className="Overlay" />
<AlertDialog.Content className="Dialog">
<AlertDialog.Title className="DialogTitle">
Delete team
</AlertDialog.Title>
<AlertDialog.Description className="DialogDescription">
Are you sure you want to permanently delete this team?
</AlertDialog.Description>
<div className="actions">
<AlertDialog.Cancel className="Button modal">Nevermind</AlertDialog.Cancel>
<AlertDialog.Action className="Button modal destructive" onClick={(e) => deleteTeam(e)}>Yes, delete</AlertDialog.Action>
</div>
</AlertDialog.Content>
</AlertDialog.Portal>
</AlertDialog.Root>
)
} else {
return (<div />)
}
}
return (
<Header
position="bottom"
left={ leftNav() }
right={ rightNav() }
/>
)
}
export default BottomHeader