forked from vercel/commerce
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial commit related to getAllPages * Initial Changes * Making documentListName configurable * fixing dynamic page rendering and adding typescript code Co-authored-by: amolnadagonde <amol.nadagonde@kibocommerce.com> Co-authored-by: kibo-sushant <sushant.jadhav@blueconchtech.com>
- Loading branch information
1 parent
11c43e9
commit 4993708
Showing
7 changed files
with
121 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,46 @@ | ||
export type Page = { url: string } | ||
export type GetAllPagesResult = { pages: Page[] } | ||
import type { KiboCommerceConfig } from '../index' | ||
import type { OperationContext } from '@commerce/api/operations' | ||
import type { Page } from "../../types/page"; | ||
import type { KiboCommerceConfig, KiboCommerceProvider } from '../index' | ||
import { getAllPagesQuery } from '../queries/get-all-pages-query' | ||
|
||
export default function getAllPagesOperation() { | ||
function getAllPages({ | ||
export type GetAllPagesResult< | ||
T extends { pages: Page[] } = { pages: Page[] } | ||
> = T | ||
|
||
export default function getAllPagesOperation({ | ||
commerce, | ||
}: OperationContext<KiboCommerceProvider>) { | ||
|
||
async function getAllPages({ | ||
query = getAllPagesQuery, | ||
config, | ||
preview, | ||
variables, | ||
}: { | ||
url?: string | ||
config?: Partial<KiboCommerceConfig> | ||
variables?: any | ||
preview?: boolean | ||
}): Promise<GetAllPagesResult> { | ||
return Promise.resolve({ | ||
pages: [], | ||
}) | ||
query?: string | ||
} = {}): Promise<GetAllPagesResult> { | ||
const cfg = commerce.getConfig(config) | ||
variables = { | ||
documentListName: cfg.documentListName | ||
} | ||
const { data } = await cfg.fetch(query, { variables }); | ||
|
||
const pages = data.documentListDocuments.items.map((page: any) => { | ||
return { | ||
id: page.id, | ||
name: page.name.charAt(0).toUpperCase() + page.name.slice(1), | ||
url: page.properties.url, | ||
body: page.properties.body, | ||
is_visible: page.properties.is_visible, | ||
sort_order: page.properties.sort_order | ||
} | ||
}); | ||
|
||
return { pages } | ||
} | ||
|
||
return getAllPages | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,42 @@ | ||
export type Page = any | ||
export type GetPageResult = { page?: Page } | ||
import type { | ||
OperationContext, | ||
} from '@commerce/api/operations' | ||
import type { KiboCommerceConfig, KiboCommerceProvider } from '..' | ||
import { normalizePage } from '../../../bigcommerce/lib/normalize' | ||
import { getPageQuery } from '../queries/get-page-query' | ||
|
||
export type PageVariables = { | ||
id: number | ||
} | ||
export default function getPageOperation({ | ||
commerce, | ||
}: OperationContext<KiboCommerceProvider>) { | ||
async function getPage<T extends any>({ | ||
url, | ||
variables, | ||
config, | ||
preview, | ||
}: { | ||
url?: string | ||
variables: any | ||
config?: Partial<KiboCommerceConfig> | ||
preview?: boolean | ||
}): Promise<any> { | ||
// RecursivePartial forces the method to check for every prop in the data, which is | ||
// required in case there's a custom `url` | ||
const cfg = commerce.getConfig(config) | ||
const pageVariables = { documentListName: cfg.documentListName, filter: `id eq ${variables.id}`} | ||
|
||
export default function getPageOperation() { | ||
function getPage(): Promise<GetPageResult> { | ||
return Promise.resolve({}) | ||
const { data } = await cfg.fetch<any>(getPageQuery, {variables: pageVariables}) | ||
|
||
const firstPage = data.documentListDocuments.items?.[0].properties; | ||
const page = firstPage as any | ||
|
||
if (preview || page?.is_visible) { | ||
const normalizedPage = { page: normalizePage(page as any) } | ||
normalizedPage.page.name = url as string; | ||
|
||
return normalizedPage | ||
} | ||
return {} | ||
} | ||
|
||
return getPage | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const getAllPagesQuery = /* GraphQL */` | ||
query($documentListName: String!) { | ||
documentListDocuments(documentListName:$documentListName){ | ||
items { | ||
id | ||
name | ||
listFQN | ||
properties | ||
} | ||
} | ||
}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const getPageQuery = /* GraphQL */` | ||
query($documentListName: String!, $filter: String!) { | ||
documentListDocuments(documentListName: $documentListName, filter: $filter){ | ||
startIndex | ||
totalCount | ||
items { | ||
properties | ||
} | ||
} | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as Core from '@commerce/types/page' | ||
export * from '@commerce/types/page' | ||
|
||
export type Page = Core.Page | ||
|
||
export type PageTypes = { | ||
page: Page | ||
} | ||
|
||
export type GetAllPagesOperation = Core.GetAllPagesOperation<PageTypes> | ||
export type GetPageOperation = Core.GetPageOperation<PageTypes> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters