Skip to content

Commit

Permalink
rename to /books. update function name add custom field support
Browse files Browse the repository at this point in the history
  • Loading branch information
gregrickaby committed Oct 23, 2023
1 parent ad02080 commit be72a11
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions app/book/[slug]/page.tsx → app/books/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import getAllBooks from '@/lib/api/queries/getAllBooks'
import getBookBySlug from '@/lib/api/queries/getBookBySlug'
import {Metadata} from 'next'
import Link from 'next/link'
import {notFound} from 'next/navigation'

export const dynamicParams = true
Expand Down Expand Up @@ -55,11 +56,11 @@ export async function generateMetadata({
*
* @see https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts#pages
*/
export default async function Page({params}: {params: {slug: string}}) {
// Fetch a single page from WordPress.
export default async function Book({params}: {params: {slug: string}}) {
// Fetch a single book from WordPress.
const book = await getBookBySlug(params.slug)

// No post? Bail...
// No book? Bail...
if (!book) {
notFound()
}
Expand All @@ -69,6 +70,9 @@ export default async function Page({params}: {params: {slug: string}}) {
<article className="w-full">
<h1 dangerouslySetInnerHTML={{__html: book.title}} />
<div dangerouslySetInnerHTML={{__html: book.content}} />
<Link className="button" href={book.bookFields.affiliateUrl}>
View on Amazon
</Link>
</article>
</main>
)
Expand Down
10 changes: 7 additions & 3 deletions lib/api/queries/getBookBySlug.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import {fetchGraphQL} from '@/lib/functions'
import {Page} from '@/lib/types'
import {Book} from '@/lib/types'

/**
* Fetch a book by slug.
*/
export default async function getBookBySlug(slug: string) {
const query = `
query GetBookBySlug($slug: ID = "URI") {
book(idType: URI, id: $slug) {
book(idType: SLUG, id: $slug) {
bookFields {
affiliateUrl
isbn
}
databaseId
content(format: RENDERED)
title(format: RENDERED)
Expand Down Expand Up @@ -38,5 +42,5 @@ export default async function getBookBySlug(slug: string) {

const response = await fetchGraphQL(query, variables)

return response.data.book as Page
return response.data.book as Book
}

0 comments on commit be72a11

Please sign in to comment.