2021-09-23 17:42:00 +00:00
|
|
|
import Layout from '../../components/layout'
|
2021-09-30 15:46:58 +00:00
|
|
|
import { useQuery } from '@apollo/client'
|
2021-09-23 17:42:00 +00:00
|
|
|
import UserHeader from '../../components/user-header'
|
|
|
|
import Seo from '../../components/seo'
|
|
|
|
import Items from '../../components/items'
|
2021-09-30 15:46:58 +00:00
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { USER_FULL } from '../../fragments/users'
|
|
|
|
import { getServerSideProps as headerProps } from './index'
|
|
|
|
import getSSRApolloClient from '../../api/ssrApollo'
|
|
|
|
import { MORE_ITEMS } from '../../fragments/items'
|
2021-09-23 17:42:00 +00:00
|
|
|
|
2021-09-30 15:46:58 +00:00
|
|
|
export async function getServerSideProps ({ req, params: { username } }) {
|
|
|
|
const { notFound, props } = await headerProps({ req, params: { username } })
|
2021-09-23 17:42:00 +00:00
|
|
|
|
2021-09-30 15:46:58 +00:00
|
|
|
if (notFound) {
|
2021-09-23 17:42:00 +00:00
|
|
|
return {
|
2021-09-30 15:46:58 +00:00
|
|
|
notFound
|
2021-09-23 17:42:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 15:46:58 +00:00
|
|
|
const { user } = props
|
|
|
|
const client = await getSSRApolloClient(req)
|
|
|
|
const { data } = await client.query({
|
|
|
|
query: MORE_ITEMS,
|
|
|
|
variables: { sort: 'user', userId: user.id }
|
|
|
|
})
|
|
|
|
|
|
|
|
let items, cursor
|
|
|
|
if (data) {
|
|
|
|
({ moreItems: { items, cursor } } = data)
|
|
|
|
}
|
|
|
|
|
2021-09-23 17:42:00 +00:00
|
|
|
return {
|
|
|
|
props: {
|
2021-09-30 15:46:58 +00:00
|
|
|
...props,
|
|
|
|
items,
|
|
|
|
cursor
|
2021-09-23 17:42:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 15:46:58 +00:00
|
|
|
export default function UserPosts ({ user, items, cursor }) {
|
|
|
|
const router = useRouter()
|
|
|
|
|
|
|
|
const { data } = useQuery(
|
|
|
|
USER_FULL(user.name), {
|
|
|
|
fetchPolicy: router.query.cache ? 'cache-first' : undefined
|
|
|
|
})
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
({ user } = data)
|
|
|
|
}
|
|
|
|
|
2021-09-23 17:42:00 +00:00
|
|
|
return (
|
|
|
|
<Layout noSeo>
|
|
|
|
<Seo user={user} />
|
|
|
|
<UserHeader user={user} />
|
2021-09-30 15:46:58 +00:00
|
|
|
<Items
|
|
|
|
items={items} cursor={cursor}
|
|
|
|
variables={{ sort: 'user', userId: user.id }}
|
|
|
|
/>
|
2021-09-23 17:42:00 +00:00
|
|
|
</Layout>
|
|
|
|
)
|
|
|
|
}
|