How to create custom hook like usePost
for any custom post type?
#779
-
Any suggestions for creating custom hooks for handling CPT? Or how to make my code work?
import React from 'react'
import { BookIdType, client } from "../../client"
import { Header, Book, Footer } from "../../components"
import { getArrayFields } from "gqty"
export default function BookPage(bookSlug: string) {
const { useQuery } = client
const book = useQuery()?.book({ id: bookSlug, idType: BookIdType.URI })
return (
<>
<Header title={useQuery()?.generalSettings?.title} />
<Book
title={book?.BookCustomFields?.title}
authors={book?.BookCustomFields?.authors?.map((author) => author.name).join(', ')}
category={book?.BookCustomFields?.category?.map((category) => category.name).join(', ')}
city={book?.BookCustomFields?.city?.name}
cover={book?.BookCustomFields?.cover?.mediaItemUrl}
file={book?.BookCustomFields?.file?.mediaItemUrl} // pdf
genres={book?.BookCustomFields?.genres?.map((genre) => genre.name).join(', ')}
isbn={book?.BookCustomFields?.isbn}
language={book?.BookCustomFields?.language?.map((language) => language.name).join(', ')}
pageCount={+book?.BookCustomFields?.pageCount}
publisher={book?.BookCustomFields?.publisher?.name}
publishedDate={book?.BookCustomFields?.publishedDate?.name}
volume={book?.BookCustomFields?.volume?.toString()}
description={book?.BookCustomFields?.description}
/>
<Footer />
</>
)
}
export async function getStaticProps({ params }) {
return {
props: {
bookSlug: params.bookSlug,
},
}
}
export async function getStaticPaths() {
const books = await client.client.inlineResolved(() => {
return getArrayFields(
client.client.query.books()?.nodes,
"slug"
)
})
const bookSlug = books.map((book) => {
return {
params: {
bookSlug: book?.slug
},
}
})
return {
paths: bookSlug,
fallback: false,
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
josephfusco
Feb 2, 2023
Replies: 2 comments 3 replies
-
Hey @Ahrary, when adding new post types be sure to run |
Beta Was this translation helpful? Give feedback.
1 reply
-
Hi guys. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @Ahrary, a newer version of Faust is available that might take care of this issue you are experiencing with single post pages!
A migration guide is available to help move folks away from the legacy version using GQTY in favor of the latest Faust that uses Apollo.