import { getGetServerSideProps } from '@/api/ssrApollo' import Items from '@/components/items' import { useRouter } from 'next/router' import { USER, USER_WITH_ITEMS } from '@/fragments/users' import { useQuery } from '@apollo/client' import { COMMENT_TYPE_QUERY, ITEM_SORTS, ITEM_TYPES_USER, WHENS } from '@/lib/constants' import PageLoading from '@/components/page-loading' import { UserLayout } from '.' import { Form, Select, DatePicker } from '@/components/form' import { whenToFrom } from '@/lib/time' const staticVariables = { sort: 'user' } const variablesFunc = vars => ({ includeComments: COMMENT_TYPE_QUERY.includes(vars.type), ...staticVariables, ...vars }) export const getServerSideProps = getGetServerSideProps( { query: USER_WITH_ITEMS, variables: variablesFunc, notFound: data => !data.user }) export default function UserItems ({ ssrData }) { const router = useRouter() const variables = variablesFunc(router.query) const { data } = useQuery(USER, { variables }) if (!data && !ssrData) return const { user } = data || ssrData return (
) } function UserItemsHeader ({ type, name }) { const router = useRouter() async function select (values) { let { type, ...query } = values if (!type || type === 'all' || !ITEM_TYPES_USER.includes(type)) type = 'all' 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 if (query.when !== 'custom') { delete query.from; delete query.to } if (query.from && !query.to) return await router.push({ pathname: `/${name}/${type}`, query }) } type ||= router.query.type || 'all' const by = router.query.by || 'recent' const when = router.query.when || 'forever' return (
select({ ...formik?.values, by: e.target.value })} /> for