From ae916ecb9735f305178c3804cdc4ba651b5bc88b Mon Sep 17 00:00:00 2001 From: keyan Date: Mon, 18 Apr 2022 11:40:08 -0500 Subject: [PATCH] add notFound function to SSR HOF, 404 inactive jobs --- api/ssrApollo.js | 4 ++-- pages/[name]/index.js | 3 ++- pages/items/[id]/edit.js | 3 ++- pages/items/[id]/index.js | 3 ++- pages/~/[sub]/index.js | 3 ++- pages/~/[sub]/post.js | 3 ++- pages/~/[sub]/recent.js | 3 ++- pages/~/[sub]/search.js | 3 ++- 8 files changed, 16 insertions(+), 9 deletions(-) diff --git a/api/ssrApollo.js b/api/ssrApollo.js index 84e99971..3f6b7cc2 100644 --- a/api/ssrApollo.js +++ b/api/ssrApollo.js @@ -31,7 +31,7 @@ export default async function getSSRApolloClient (req, me = null) { }) } -export function getGetServerSideProps (query, variables = null, foundField, requireVar) { +export function getGetServerSideProps (query, variables = null, notFoundFunc, requireVar) { return async function ({ req, query: params }) { const client = await getSSRApolloClient(req) const vars = { ...params, ...variables } @@ -47,7 +47,7 @@ export function getGetServerSideProps (query, variables = null, foundField, requ variables: vars }) - if (error || !data || (foundField && !data[foundField])) { + if (error || !data || (notFoundFunc && notFoundFunc(data))) { return { notFound: true } diff --git a/pages/[name]/index.js b/pages/[name]/index.js index e9538fb3..4ef4db05 100644 --- a/pages/[name]/index.js +++ b/pages/[name]/index.js @@ -15,7 +15,8 @@ import { USER_FULL } from '../../fragments/users' import { ITEM_FIELDS } from '../../fragments/items' import { getGetServerSideProps } from '../../api/ssrApollo' -export const getServerSideProps = getGetServerSideProps(USER_FULL, null, 'user') +export const getServerSideProps = getGetServerSideProps(USER_FULL, null, + data => !data.user) const BioSchema = Yup.object({ bio: Yup.string().required('required').trim() diff --git a/pages/items/[id]/edit.js b/pages/items/[id]/edit.js index dc8199d6..0faa67c0 100644 --- a/pages/items/[id]/edit.js +++ b/pages/items/[id]/edit.js @@ -5,7 +5,8 @@ import { LinkForm } from '../../../components/link-form' import LayoutCenter from '../../../components/layout-center' import JobForm from '../../../components/job-form' -export const getServerSideProps = getGetServerSideProps(ITEM, null, 'item') +export const getServerSideProps = getGetServerSideProps(ITEM, null, + data => !data.item) export default function PostEdit ({ data: { item } }) { const editThreshold = new Date(item.createdAt).getTime() + 10 * 60000 diff --git a/pages/items/[id]/index.js b/pages/items/[id]/index.js index f4935595..8b387321 100644 --- a/pages/items/[id]/index.js +++ b/pages/items/[id]/index.js @@ -5,7 +5,8 @@ import ItemFull from '../../../components/item-full' import { getGetServerSideProps } from '../../../api/ssrApollo' import { useQuery } from '@apollo/client' -export const getServerSideProps = getGetServerSideProps(ITEM_FULL, null, 'item') +export const getServerSideProps = getGetServerSideProps(ITEM_FULL, null, + data => !data.item || data.item.status !== 'ACTIVE') export default function AnItem ({ data: { item } }) { const { data } = useQuery(ITEM_FULL, { diff --git a/pages/~/[sub]/index.js b/pages/~/[sub]/index.js index c62f78b1..664c37e1 100644 --- a/pages/~/[sub]/index.js +++ b/pages/~/[sub]/index.js @@ -3,7 +3,8 @@ import Items from '../../../components/items' import Layout from '../../../components/layout' import { SUB_ITEMS } from '../../../fragments/subs' -export const getServerSideProps = getGetServerSideProps(SUB_ITEMS, null, 'sub') +export const getServerSideProps = getGetServerSideProps(SUB_ITEMS, null, + data => !data.sub) export default function Sub ({ data: { sub: { name }, items: { items, cursor } } }) { return ( diff --git a/pages/~/[sub]/post.js b/pages/~/[sub]/post.js index 6312bcde..bf403e13 100644 --- a/pages/~/[sub]/post.js +++ b/pages/~/[sub]/post.js @@ -3,7 +3,8 @@ import { SUB } from '../../../fragments/subs' import LayoutCenter from '../../../components/layout-center' import JobForm from '../../../components/job-form' -export const getServerSideProps = getGetServerSideProps(SUB, null, 'sub') +export const getServerSideProps = getGetServerSideProps(SUB, null, + data => !data.sub) // need to recent list items export default function Post ({ data: { sub } }) { diff --git a/pages/~/[sub]/recent.js b/pages/~/[sub]/recent.js index 29051935..3bccdb43 100644 --- a/pages/~/[sub]/recent.js +++ b/pages/~/[sub]/recent.js @@ -4,7 +4,8 @@ import Layout from '../../../components/layout' import { SUB_ITEMS } from '../../../fragments/subs' const variables = { sort: 'recent' } -export const getServerSideProps = getGetServerSideProps(SUB_ITEMS, variables, 'sub') +export const getServerSideProps = getGetServerSideProps(SUB_ITEMS, variables, + data => !data.sub) // need to recent list items export default function Sub ({ data: { sub: { name }, items: { items, cursor } } }) { diff --git a/pages/~/[sub]/search.js b/pages/~/[sub]/search.js index 8a09ff2f..418c9b8e 100644 --- a/pages/~/[sub]/search.js +++ b/pages/~/[sub]/search.js @@ -5,7 +5,8 @@ import { useRouter } from 'next/router' import { SeoSearch } from '../../../components/seo' import { SUB_SEARCH } from '../../../fragments/subs' -export const getServerSideProps = getGetServerSideProps(SUB_SEARCH, null, 'sub', 'q') +export const getServerSideProps = getGetServerSideProps(SUB_SEARCH, null, + data => !data.sub, 'q') export default function Index ({ data: { sub: { name }, search: { items, cursor } } }) { const router = useRouter()