2024-03-20 00:37:31 +00:00
|
|
|
import { getGetServerSideProps } from '@/api/ssrApollo'
|
|
|
|
import Items from '@/components/items'
|
2023-07-23 15:08:43 +00:00
|
|
|
import { useRouter } from 'next/router'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { USER, USER_WITH_ITEMS } from '@/fragments/users'
|
2023-07-23 15:08:43 +00:00
|
|
|
import { useQuery } from '@apollo/client'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { COMMENT_TYPE_QUERY, ITEM_SORTS, ITEM_TYPES_USER, WHENS } from '@/lib/constants'
|
|
|
|
import PageLoading from '@/components/page-loading'
|
2023-07-23 15:08:43 +00:00
|
|
|
import { UserLayout } from '.'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { Form, Select, DatePicker } from '@/components/form'
|
|
|
|
import { whenToFrom } from '@/lib/time'
|
2023-07-23 15:08:43 +00:00
|
|
|
|
|
|
|
const staticVariables = { sort: 'user' }
|
2023-08-31 00:03:05 +00:00
|
|
|
const variablesFunc = vars => ({
|
|
|
|
includeComments: COMMENT_TYPE_QUERY.includes(vars.type),
|
|
|
|
...staticVariables,
|
|
|
|
...vars
|
|
|
|
})
|
2023-08-28 17:52:15 +00:00
|
|
|
export const getServerSideProps = getGetServerSideProps(
|
|
|
|
{ query: USER_WITH_ITEMS, variables: variablesFunc, notFound: data => !data.user })
|
2023-07-23 15:08:43 +00:00
|
|
|
|
|
|
|
export default function UserItems ({ ssrData }) {
|
|
|
|
const router = useRouter()
|
|
|
|
const variables = variablesFunc(router.query)
|
|
|
|
|
2023-08-31 00:03:05 +00:00
|
|
|
const { data } = useQuery(USER, { variables })
|
2023-07-23 15:08:43 +00:00
|
|
|
if (!data && !ssrData) return <PageLoading />
|
|
|
|
|
|
|
|
const { user } = data || ssrData
|
|
|
|
|
|
|
|
return (
|
|
|
|
<UserLayout user={user}>
|
|
|
|
<div className='mt-2'>
|
|
|
|
<UserItemsHeader type={variables.type} name={user.name} />
|
|
|
|
<Items
|
|
|
|
ssrData={ssrData}
|
|
|
|
variables={variables}
|
|
|
|
query={USER_WITH_ITEMS}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</UserLayout>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function UserItemsHeader ({ type, name }) {
|
|
|
|
const router = useRouter()
|
|
|
|
async function select (values) {
|
|
|
|
let { type, ...query } = values
|
2023-11-21 23:32:22 +00:00
|
|
|
if (!type || type === 'all' || !ITEM_TYPES_USER.includes(type)) type = 'all'
|
2023-07-25 20:29:45 +00:00
|
|
|
if (!query.by || query.by === 'recent' || !ITEM_SORTS.includes(query.by)) delete query.by
|
|
|
|
if (!query.when || query.when === 'forever' || !WHENS.includes(query.when) || query.when === 'forever') delete query.when
|
2023-11-09 00:15:36 +00:00
|
|
|
if (query.when !== 'custom') { delete query.from; delete query.to }
|
|
|
|
if (query.from && !query.to) return
|
2023-07-23 15:08:43 +00:00
|
|
|
|
|
|
|
await router.push({
|
|
|
|
pathname: `/${name}/${type}`,
|
|
|
|
query
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-25 20:29:45 +00:00
|
|
|
type ||= router.query.type || 'all'
|
|
|
|
const by = router.query.by || 'recent'
|
|
|
|
const when = router.query.when || 'forever'
|
|
|
|
|
2023-07-23 15:08:43 +00:00
|
|
|
return (
|
|
|
|
<Form
|
2023-11-09 00:15:36 +00:00
|
|
|
initial={{ type, by, when, from: '', to: '' }}
|
2023-07-23 15:08:43 +00:00
|
|
|
onSubmit={select}
|
|
|
|
>
|
2023-11-09 00:15:36 +00:00
|
|
|
<div className='text-muted fw-bold mt-0 mb-3 d-flex justify-content-start align-items-center flex-wrap'>
|
|
|
|
<div className='text-muted fw-bold mt-0 mb-2 d-flex justify-content-start align-items-center'>
|
|
|
|
<Select
|
|
|
|
groupClassName='mb-0 me-2'
|
|
|
|
className='w-auto'
|
|
|
|
name='type'
|
|
|
|
size='sm'
|
|
|
|
overrideValue={type}
|
2023-11-21 23:32:22 +00:00
|
|
|
items={ITEM_TYPES_USER}
|
2023-11-09 00:15:36 +00:00
|
|
|
onChange={(formik, e) => select({ ...formik?.values, type: e.target.value })}
|
|
|
|
/>
|
|
|
|
by
|
|
|
|
<Select
|
|
|
|
groupClassName='mb-0 mx-2'
|
|
|
|
className='w-auto'
|
|
|
|
name='by'
|
|
|
|
size='sm'
|
|
|
|
overrideValue={by}
|
|
|
|
items={['recent', ...ITEM_SORTS]}
|
|
|
|
onChange={(formik, e) => select({ ...formik?.values, by: e.target.value })}
|
|
|
|
/>
|
|
|
|
for
|
|
|
|
<Select
|
|
|
|
groupClassName='mb-0 mx-2'
|
|
|
|
className='w-auto'
|
|
|
|
name='when'
|
|
|
|
size='sm'
|
|
|
|
items={WHENS}
|
|
|
|
overrideValue={when}
|
|
|
|
onChange={(formik, e) => {
|
2023-11-14 16:23:44 +00:00
|
|
|
const range = e.target.value === 'custom' ? { from: whenToFrom(when), to: Date.now() } : {}
|
2023-11-09 00:15:36 +00:00
|
|
|
select({ ...formik?.values, when: e.target.value, ...range })
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{when === 'custom' &&
|
|
|
|
<DatePicker
|
|
|
|
fromName='from' toName='to'
|
2024-05-12 01:32:44 +00:00
|
|
|
className='p-0 px-2'
|
2023-11-09 00:15:36 +00:00
|
|
|
onChange={(formik, [from, to], e) => {
|
2023-11-14 16:23:44 +00:00
|
|
|
select({ ...formik?.values, from: from.getTime(), to: to.getTime() })
|
2023-11-09 00:15:36 +00:00
|
|
|
}}
|
|
|
|
from={router.query.from}
|
|
|
|
to={router.query.to}
|
|
|
|
when={when}
|
|
|
|
/>}
|
2023-07-23 15:08:43 +00:00
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
)
|
|
|
|
}
|