2021-12-16 23:05:31 +00:00
|
|
|
import { useRouter } from 'next/router'
|
2022-10-25 21:35:32 +00:00
|
|
|
import { Form, Select } from './form'
|
2023-07-23 15:08:43 +00:00
|
|
|
import { ITEM_SORTS, USER_SORTS, WHENS } from '../lib/constants'
|
2023-02-03 19:10:18 +00:00
|
|
|
|
2023-06-12 22:15:20 +00:00
|
|
|
export default function TopHeader ({ sub, cat }) {
|
2021-12-16 23:05:31 +00:00
|
|
|
const router = useRouter()
|
2022-10-25 21:35:32 +00:00
|
|
|
|
|
|
|
const top = async values => {
|
2023-02-03 19:10:18 +00:00
|
|
|
const { what, when, ...query } = values
|
|
|
|
|
2023-02-09 18:41:28 +00:00
|
|
|
if (what === 'cowboys') {
|
|
|
|
await router.push({
|
|
|
|
pathname: `/top/${what}`
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-12 22:15:20 +00:00
|
|
|
const prefix = sub ? `/~${sub}` : ''
|
|
|
|
|
2023-07-23 15:08:43 +00:00
|
|
|
if (typeof query.by !== 'undefined') {
|
|
|
|
if (query.by === '' ||
|
2023-07-26 00:45:35 +00:00
|
|
|
(what === 'stackers' && (query.by === 'stacked' || !USER_SORTS.includes(query.by))) ||
|
|
|
|
(what !== 'stackers' && (query.by === 'votes' || !ITEM_SORTS.includes(query.by)))) {
|
2023-07-23 15:08:43 +00:00
|
|
|
delete query.by
|
2023-02-03 19:10:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 21:35:32 +00:00
|
|
|
await router.push({
|
2023-06-12 22:15:20 +00:00
|
|
|
pathname: `${prefix}/top/${what}/${when || 'day'}`,
|
2023-02-03 19:10:18 +00:00
|
|
|
query
|
2022-10-25 21:35:32 +00:00
|
|
|
})
|
|
|
|
}
|
2021-12-16 23:05:31 +00:00
|
|
|
|
2023-07-25 20:29:45 +00:00
|
|
|
const what = cat
|
2023-08-06 18:13:32 +00:00
|
|
|
const by = router.query.by || (what === 'stackers' ? 'stacked' : 'votes')
|
2023-07-25 20:29:45 +00:00
|
|
|
const when = router.query.when || ''
|
|
|
|
|
2021-12-16 23:05:31 +00:00
|
|
|
return (
|
2022-10-25 21:35:32 +00:00
|
|
|
<div className='d-flex'>
|
|
|
|
<Form
|
2023-07-24 18:35:05 +00:00
|
|
|
className='me-auto'
|
2023-07-25 20:29:45 +00:00
|
|
|
initial={{ what, by, when }}
|
2022-10-25 21:35:32 +00:00
|
|
|
onSubmit={top}
|
|
|
|
>
|
2023-07-24 18:35:05 +00:00
|
|
|
<div className='text-muted fw-bold my-3 d-flex align-items-center'>
|
2022-10-25 21:35:32 +00:00
|
|
|
top
|
|
|
|
<Select
|
|
|
|
groupClassName='mx-2 mb-0'
|
|
|
|
onChange={(formik, e) => top({ ...formik?.values, what: e.target.value })}
|
|
|
|
name='what'
|
|
|
|
size='sm'
|
2023-07-25 20:29:45 +00:00
|
|
|
overrideValue={what}
|
2023-07-09 17:37:12 +00:00
|
|
|
items={router?.query?.sub ? ['posts', 'comments'] : ['posts', 'comments', 'stackers', 'cowboys']}
|
2022-10-25 21:35:32 +00:00
|
|
|
/>
|
2023-02-09 18:41:28 +00:00
|
|
|
{cat !== 'cowboys' &&
|
|
|
|
<>
|
|
|
|
by
|
|
|
|
<Select
|
|
|
|
groupClassName='mx-2 mb-0'
|
2023-07-23 15:08:43 +00:00
|
|
|
onChange={(formik, e) => top({ ...formik?.values, by: e.target.value })}
|
|
|
|
name='by'
|
2023-02-09 18:41:28 +00:00
|
|
|
size='sm'
|
2023-07-25 20:29:45 +00:00
|
|
|
overrideValue={by}
|
2023-07-09 17:37:12 +00:00
|
|
|
items={cat === 'stackers' ? USER_SORTS : ITEM_SORTS}
|
2023-02-09 18:41:28 +00:00
|
|
|
/>
|
|
|
|
for
|
|
|
|
<Select
|
2023-07-24 18:35:05 +00:00
|
|
|
groupClassName='mb-0 ms-2'
|
2023-02-09 18:41:28 +00:00
|
|
|
onChange={(formik, e) => top({ ...formik?.values, when: e.target.value })}
|
|
|
|
name='when'
|
|
|
|
size='sm'
|
2023-07-25 20:29:45 +00:00
|
|
|
overrideValue={when}
|
2023-07-23 15:08:43 +00:00
|
|
|
items={WHENS}
|
2023-02-09 18:41:28 +00:00
|
|
|
/>
|
|
|
|
</>}
|
|
|
|
|
2022-10-25 21:35:32 +00:00
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</div>
|
2021-12-16 23:05:31 +00:00
|
|
|
)
|
|
|
|
}
|