fix closure stacker bug

This commit is contained in:
keyan 2023-08-30 19:03:05 -05:00
parent b3eb1fbd96
commit 68e9dfd69c
3 changed files with 19 additions and 12 deletions

View File

@ -61,15 +61,16 @@ export default async function getSSRApolloClient ({ req, res, me = null }) {
* @param opts.notFound function that tests data to determine if 404 * @param opts.notFound function that tests data to determine if 404
* @param opts.authRequired boolean that determines if auth is required * @param opts.authRequired boolean that determines if auth is required
*/ */
export function getGetServerSideProps ({ query, variables, notFound, authRequired }) { export function getGetServerSideProps (
{ query: queryOrFunc, variables: varsOrFunc, notFound, authRequired }) {
return async function ({ req, res, query: params }) { return async function ({ req, res, query: params }) {
const { nodata, ...realParams } = params const { nodata, ...realParams } = params
// we want to use client-side cache // we want to use client-side cache
if (nodata) return { props: { } } if (nodata) return { props: { } }
variables = typeof variables === 'function' ? variables(realParams) : variables const variables = typeof varsOrFunc === 'function' ? varsOrFunc(realParams) : varsOrFunc
const vars = { ...realParams, ...variables } const vars = { ...realParams, ...variables }
query = typeof query === 'function' ? query(vars) : query const query = typeof queryOrFunc === 'function' ? queryOrFunc(vars) : queryOrFunc
const client = await getSSRApolloClient({ req, res }) const client = await getSSRApolloClient({ req, res })

View File

@ -195,6 +195,14 @@ export const USER_FULL = gql`
} }
}` }`
export const USER = gql`
${USER_FIELDS}
query User($name: String!) {
user(name: $name) {
...UserFields
}
}`
export const USER_WITH_ITEMS = gql` export const USER_WITH_ITEMS = gql`
${USER_FIELDS} ${USER_FIELDS}
${ITEM_FIELDS} ${ITEM_FIELDS}

View File

@ -1,7 +1,7 @@
import { getGetServerSideProps } from '../../api/ssrApollo' import { getGetServerSideProps } from '../../api/ssrApollo'
import Items from '../../components/items' import Items from '../../components/items'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import { USER_WITH_ITEMS } from '../../fragments/users' import { USER, USER_WITH_ITEMS } from '../../fragments/users'
import { useQuery } from '@apollo/client' import { useQuery } from '@apollo/client'
import { COMMENT_TYPE_QUERY, ITEM_SORTS, ITEM_TYPES, WHENS } from '../../lib/constants' import { COMMENT_TYPE_QUERY, ITEM_SORTS, ITEM_TYPES, WHENS } from '../../lib/constants'
import PageLoading from '../../components/page-loading' import PageLoading from '../../components/page-loading'
@ -9,12 +9,11 @@ import { UserLayout } from '.'
import { Form, Select } from '../../components/form' import { Form, Select } from '../../components/form'
const staticVariables = { sort: 'user' } const staticVariables = { sort: 'user' }
const variablesFunc = vars => const variablesFunc = vars => ({
({ includeComments: COMMENT_TYPE_QUERY.includes(vars.type),
includeComments: COMMENT_TYPE_QUERY.includes(vars.type), ...staticVariables,
...staticVariables, ...vars
...vars })
})
export const getServerSideProps = getGetServerSideProps( export const getServerSideProps = getGetServerSideProps(
{ query: USER_WITH_ITEMS, variables: variablesFunc, notFound: data => !data.user }) { query: USER_WITH_ITEMS, variables: variablesFunc, notFound: data => !data.user })
@ -22,7 +21,7 @@ export default function UserItems ({ ssrData }) {
const router = useRouter() const router = useRouter()
const variables = variablesFunc(router.query) const variables = variablesFunc(router.query)
const { data } = useQuery(USER_WITH_ITEMS, { variables }) const { data } = useQuery(USER, { variables })
if (!data && !ssrData) return <PageLoading /> if (!data && !ssrData) return <PageLoading />
const { user } = data || ssrData const { user } = data || ssrData
@ -43,7 +42,6 @@ export default function UserItems ({ ssrData }) {
function UserItemsHeader ({ type, name }) { function UserItemsHeader ({ type, name }) {
const router = useRouter() const router = useRouter()
async function select (values) { async function select (values) {
let { type, ...query } = values let { type, ...query } = values
if (!type || type === 'all' || !ITEM_TYPES('user').includes(type)) type = 'all' if (!type || type === 'all' || !ITEM_TYPES('user').includes(type)) type = 'all'