Changed to query page by id

This commit is contained in:
cond0r 2021-02-26 09:12:44 +02:00
parent 1c7ac7d795
commit 590bff576c
5 changed files with 21 additions and 19 deletions

View File

@ -8,7 +8,6 @@ import {
} from '../const' } from '../const'
if (!API_URL) { if (!API_URL) {
console.log(process.env)
throw new Error( throw new Error(
`The environment variable NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN is missing and it's required to access your store` `The environment variable NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN is missing and it's required to access your store`
) )

View File

@ -25,12 +25,14 @@ const getAllPages = async (options?: {
}): Promise<ReturnType> => { }): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options ?? {} let { config, variables = { first: 250 } } = options ?? {}
config = getConfig(config) config = getConfig(config)
const { locale = 'en-US' } = config
const { data } = await config.fetch(getAllPagesQuery, { variables }) const { data } = await config.fetch(getAllPagesQuery, { variables })
const pages = data.pages?.edges?.map( const pages = data.pages?.edges?.map(
({ node: { title: name, handle, ...node } }: PageEdge) => ({ ({ node: { title: name, handle, ...node } }: PageEdge) => ({
...node, ...node,
url: `/${handle}`, url: `/${locale}/${handle}`,
name, name,
}) })
) )

View File

@ -2,34 +2,33 @@ import { getConfig, ShopifyConfig } from '../api'
import getPageQuery from '../utils/queries/get-page-query' import getPageQuery from '../utils/queries/get-page-query'
import { Page } from './get-all-pages' import { Page } from './get-all-pages'
type Variables = { type PageVariables = {
slug: string id: string
} }
type ReturnType = { export type GetPageResult<T extends { page?: any } = { page?: Page }> = T
page: Page
}
const getPage = async (options: { const getPage = async (options: {
variables: Variables variables: PageVariables
config: ShopifyConfig config: ShopifyConfig
preview?: boolean preview?: boolean
}): Promise<ReturnType> => { }): Promise<GetPageResult> => {
let { config, variables } = options ?? {} let { config, variables } = options ?? {}
config = getConfig(config) config = getConfig(config)
const { locale = 'en-US' } = config
const { data } = await config.fetch(getPageQuery, { const { data } = await config.fetch(getPageQuery, {
variables, variables,
}) })
const page = data.node
const { pageByHandle: page } = data
return { return {
page: page page: page
? { ? {
...page, ...page,
name: page.title, name: page.title,
url: page?.handle, url: `/${locale}/${page.handle}`,
} }
: null, : null,
} }

View File

@ -23,6 +23,7 @@ export type SearchProductsData = {
products: Product[] products: Product[]
found: boolean found: boolean
} }
export const handler: SWRHook< export const handler: SWRHook<
SearchProductsData, SearchProductsData,
SearchProductsInput, SearchProductsInput,

View File

@ -1,12 +1,13 @@
export const getPageQuery = /* GraphQL */ ` export const getPageQuery = /* GraphQL */ `
query getPageBySlug($slug: String!) { query($id: ID!) {
pageByHandle(handle: $slug) { node(id: $id) {
id id
title ... on Page {
handle title
body handle
bodySummary body
url bodySummary
}
} }
} }
` `