Skip to content

Commit

Permalink
feat: add wp page support
Browse files Browse the repository at this point in the history
  • Loading branch information
pooriaset committed May 4, 2024
1 parent a2702d6 commit 465e68a
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/app/[locale]/(main)/pages/[...slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { getClient } from '@/graphql/clients/serverSideClient';
import { GET_PAGE } from '@/graphql/queries/pages';
import { GetPageQuery } from '@/graphql/types/graphql';
import { Locale } from '@/navigation';
import { Container, Typography } from '@mui/material';
import { notFound } from 'next/navigation';
import React, { FC } from 'react';

type IPage = NonNullable<GetPageQuery['pages']>['edges'][number]['node'];

const getPage = async ({ slug }: { slug: string }): Promise<IPage | null> => {
if (!slug) {
return null;
}

const { data } = await getClient().query<GetPageQuery>({
query: GET_PAGE,
variables: {
slug,
},
});
return data?.pages?.edges?.[0].node || null;
};

interface PageProps {
params: { locale: Locale; slug: string[] };
searchParams: any[];
}

export async function generateMetadata(props: PageProps) {
const slug = props.params.slug?.[0];

const page = await getPage({ slug });

return {
title: page?.title,
};
}

const page: FC<PageProps> = async ({ params }) => {
const slug = params.slug?.[0];

const page = await getPage({ slug });

if (!page?.content) {
notFound();
}

return (
<Container maxWidth="xl" sx={{ mt: 10 }}>
<Typography
component="h1"
variant="h5"
sx={{
fontWeight: 'bold',
}}
>
{page.title}
</Typography>

<Typography
component="div"
sx={{
textAlign: 'justify',
lineHeight: 2,
}}
dangerouslySetInnerHTML={{
__html: page.content,
}}
/>
</Container>
);
};

export default page;
14 changes: 14 additions & 0 deletions src/graphql/queries/pages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { gql } from '@apollo/client';

export const GET_PAGE = gql`
query GetPage($slug: String) {
pages(where: { name: $slug, status: PUBLISH }) {
edges {
node {
title
content
}
}
}
}
`;
5 changes: 5 additions & 0 deletions src/graphql/types/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/
const documents = {
"\n query Categories {\n productCategories {\n nodes {\n id: databaseId\n name\n parentId: parentDatabaseId\n }\n }\n }\n": types.CategoriesDocument,
"\n query GetMainCategories {\n productCategories(where: { parent: null, orderby: TERM_ORDER }) {\n edges {\n node {\n id: databaseId\n name\n image {\n id: databaseId\n sourceUrl\n }\n }\n }\n }\n }\n": types.GetMainCategoriesDocument,
"\n query GetPage($slug: String) {\n pages(where: { name: $slug }) {\n edges {\n node {\n title\n content\n }\n }\n }\n }\n": types.GetPageDocument,
"\n query GetAllProducts(\n $stockStatus: [StockStatusEnum]\n $orderBy: [ProductsOrderbyInput]\n $categoryIdIn: [Int]\n $q: String\n $first: Int\n ) {\n products(\n first: $first\n where: {\n stockStatus: $stockStatus\n orderby: $orderBy\n categoryIdIn: $categoryIdIn\n search: $q\n }\n ) {\n pageInfo {\n total\n hasNextPage\n hasPreviousPage\n }\n nodes {\n __typename\n ... on VariableProduct {\n databaseId\n name\n onSale\n type\n averageRating\n slug\n image {\n sourceUrl\n }\n price\n regularPrice\n salePrice\n stockStatus\n }\n }\n }\n }\n": types.GetAllProductsDocument,
"\n query GetHomePageSliders {\n sliderCategories(where: { slug: \"homepage\" }) {\n nodes {\n sliders {\n edges {\n node {\n id: databaseId\n title\n url\n featuredImage {\n node {\n id: databaseId\n url: sourceUrl\n }\n }\n }\n }\n }\n }\n }\n }\n": types.GetHomePageSlidersDocument,
};
Expand All @@ -41,6 +42,10 @@ export function graphql(source: "\n query Categories {\n productCategories {
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
export function graphql(source: "\n query GetMainCategories {\n productCategories(where: { parent: null, orderby: TERM_ORDER }) {\n edges {\n node {\n id: databaseId\n name\n image {\n id: databaseId\n sourceUrl\n }\n }\n }\n }\n }\n"): (typeof documents)["\n query GetMainCategories {\n productCategories(where: { parent: null, orderby: TERM_ORDER }) {\n edges {\n node {\n id: databaseId\n name\n image {\n id: databaseId\n sourceUrl\n }\n }\n }\n }\n }\n"];
/**
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
export function graphql(source: "\n query GetPage($slug: String) {\n pages(where: { name: $slug }) {\n edges {\n node {\n title\n content\n }\n }\n }\n }\n"): (typeof documents)["\n query GetPage($slug: String) {\n pages(where: { name: $slug }) {\n edges {\n node {\n title\n content\n }\n }\n }\n }\n"];
/**
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/graphql/types/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25457,6 +25457,13 @@ export type GetMainCategoriesQueryVariables = Exact<{ [key: string]: never; }>;

export type GetMainCategoriesQuery = { __typename?: 'RootQuery', productCategories?: { __typename?: 'RootQueryToProductCategoryConnection', edges: Array<{ __typename?: 'RootQueryToProductCategoryConnectionEdge', node: { __typename?: 'ProductCategory', name?: string | null, id: number, image?: { __typename?: 'MediaItem', sourceUrl?: string | null, id: number } | null } }> } | null };

export type GetPageQueryVariables = Exact<{
slug?: InputMaybe<Scalars['String']['input']>;
}>;


export type GetPageQuery = { __typename?: 'RootQuery', pages?: { __typename?: 'RootQueryToPageConnection', edges: Array<{ __typename?: 'RootQueryToPageConnectionEdge', node: { __typename?: 'Page', title?: string | null, content?: string | null } }> } | null };

export type GetAllProductsQueryVariables = Exact<{
stockStatus?: InputMaybe<Array<InputMaybe<StockStatusEnum>> | InputMaybe<StockStatusEnum>>;
orderBy?: InputMaybe<Array<InputMaybe<ProductsOrderbyInput>> | InputMaybe<ProductsOrderbyInput>>;
Expand All @@ -25476,5 +25483,6 @@ export type GetHomePageSlidersQuery = { __typename?: 'RootQuery', sliderCategori

export const CategoriesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Categories"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"productCategories"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"nodes"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"id"},"name":{"kind":"Name","value":"databaseId"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","alias":{"kind":"Name","value":"parentId"},"name":{"kind":"Name","value":"parentDatabaseId"}}]}}]}}]}}]} as unknown as DocumentNode<CategoriesQuery, CategoriesQueryVariables>;
export const GetMainCategoriesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetMainCategories"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"productCategories"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"where"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"parent"},"value":{"kind":"NullValue"}},{"kind":"ObjectField","name":{"kind":"Name","value":"orderby"},"value":{"kind":"EnumValue","value":"TERM_ORDER"}}]}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"edges"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"node"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"id"},"name":{"kind":"Name","value":"databaseId"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"image"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"id"},"name":{"kind":"Name","value":"databaseId"}},{"kind":"Field","name":{"kind":"Name","value":"sourceUrl"}}]}}]}}]}}]}}]}}]} as unknown as DocumentNode<GetMainCategoriesQuery, GetMainCategoriesQueryVariables>;
export const GetPageDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetPage"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"slug"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"pages"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"where"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"name"},"value":{"kind":"Variable","name":{"kind":"Name","value":"slug"}}}]}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"edges"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"node"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"title"}},{"kind":"Field","name":{"kind":"Name","value":"content"}}]}}]}}]}}]}}]} as unknown as DocumentNode<GetPageQuery, GetPageQueryVariables>;
export const GetAllProductsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetAllProducts"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"stockStatus"}},"type":{"kind":"ListType","type":{"kind":"NamedType","name":{"kind":"Name","value":"StockStatusEnum"}}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"orderBy"}},"type":{"kind":"ListType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ProductsOrderbyInput"}}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"categoryIdIn"}},"type":{"kind":"ListType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Int"}}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"q"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"first"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"Int"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"products"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"first"},"value":{"kind":"Variable","name":{"kind":"Name","value":"first"}}},{"kind":"Argument","name":{"kind":"Name","value":"where"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"stockStatus"},"value":{"kind":"Variable","name":{"kind":"Name","value":"stockStatus"}}},{"kind":"ObjectField","name":{"kind":"Name","value":"orderby"},"value":{"kind":"Variable","name":{"kind":"Name","value":"orderBy"}}},{"kind":"ObjectField","name":{"kind":"Name","value":"categoryIdIn"},"value":{"kind":"Variable","name":{"kind":"Name","value":"categoryIdIn"}}},{"kind":"ObjectField","name":{"kind":"Name","value":"search"},"value":{"kind":"Variable","name":{"kind":"Name","value":"q"}}}]}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"pageInfo"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"total"}},{"kind":"Field","name":{"kind":"Name","value":"hasNextPage"}},{"kind":"Field","name":{"kind":"Name","value":"hasPreviousPage"}}]}},{"kind":"Field","name":{"kind":"Name","value":"nodes"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"__typename"}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"VariableProduct"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"databaseId"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"onSale"}},{"kind":"Field","name":{"kind":"Name","value":"type"}},{"kind":"Field","name":{"kind":"Name","value":"averageRating"}},{"kind":"Field","name":{"kind":"Name","value":"slug"}},{"kind":"Field","name":{"kind":"Name","value":"image"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"sourceUrl"}}]}},{"kind":"Field","name":{"kind":"Name","value":"price"}},{"kind":"Field","name":{"kind":"Name","value":"regularPrice"}},{"kind":"Field","name":{"kind":"Name","value":"salePrice"}},{"kind":"Field","name":{"kind":"Name","value":"stockStatus"}}]}}]}}]}}]}}]} as unknown as DocumentNode<GetAllProductsQuery, GetAllProductsQueryVariables>;
export const GetHomePageSlidersDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetHomePageSliders"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"sliderCategories"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"where"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"slug"},"value":{"kind":"StringValue","value":"homepage","block":false}}]}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"nodes"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"sliders"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"edges"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"node"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"id"},"name":{"kind":"Name","value":"databaseId"}},{"kind":"Field","name":{"kind":"Name","value":"title"}},{"kind":"Field","name":{"kind":"Name","value":"url"}},{"kind":"Field","name":{"kind":"Name","value":"featuredImage"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"node"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"id"},"name":{"kind":"Name","value":"databaseId"}},{"kind":"Field","alias":{"kind":"Name","value":"url"},"name":{"kind":"Name","value":"sourceUrl"}}]}}]}}]}}]}}]}}]}}]}}]}}]} as unknown as DocumentNode<GetHomePageSlidersQuery, GetHomePageSlidersQueryVariables>;

0 comments on commit 465e68a

Please sign in to comment.