mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 12:24:18 +00:00
feat: get-all-recipes
This commit is contained in:
@@ -16,6 +16,9 @@ import type {
|
||||
GetBlogDetailOperation,
|
||||
GetRelevantBlogsOperation
|
||||
} from '../types/blogs'
|
||||
import type {
|
||||
GetAllRecipesOperation
|
||||
} from '../types/recipes'
|
||||
|
||||
import type { APIProvider, CommerceAPI } from '.'
|
||||
import { GetAllCollectionsOperation } from '@commerce/types/collection';
|
||||
@@ -39,7 +42,8 @@ export const OPERATIONS = [
|
||||
'getFeaturedBlog',
|
||||
'getAllBlogPaths',
|
||||
'getBlogDetail',
|
||||
'getRelevantBlogs'
|
||||
'getRelevantBlogs',
|
||||
'getAllRecipes'
|
||||
] as const
|
||||
|
||||
export const defaultOperations = OPERATIONS.reduce((ops, k) => {
|
||||
@@ -188,6 +192,22 @@ export type Operations<P extends APIProvider> = {
|
||||
): Promise<T['data']>
|
||||
}
|
||||
|
||||
getAllRecipes: {
|
||||
<T extends GetAllRecipesOperation>(opts: {
|
||||
variables?: T['variables']
|
||||
config?: P['config']
|
||||
preview?: boolean
|
||||
}): Promise<T['data']>
|
||||
|
||||
<T extends GetAllRecipesOperation>(
|
||||
opts: {
|
||||
variables?: T['variables']
|
||||
config?: P['config']
|
||||
preview?: boolean
|
||||
} & OperationOptions
|
||||
): Promise<T['data']>
|
||||
}
|
||||
|
||||
getRelevantBlogs: {
|
||||
<T extends GetRelevantBlogsOperation>(opts: {
|
||||
variables?: T['variables']
|
||||
|
32
framework/commerce/types/recipes.ts
Normal file
32
framework/commerce/types/recipes.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { Asset, BlogTranslation, Maybe, Product } from './../../vendure/schema.d';
|
||||
|
||||
export type RecipeList = Node &{
|
||||
id: string
|
||||
featuredAsset?: Maybe<Asset>
|
||||
isPublish:Boolean
|
||||
translations: Array<BlogTranslation>
|
||||
authorName: string
|
||||
authorAvatarAsset:Array<Asset>
|
||||
relevantProducts: Product
|
||||
link:String
|
||||
minutes:Number
|
||||
people:Number
|
||||
}
|
||||
export type RecipesType = {
|
||||
items: RecipeList
|
||||
totalItems: number
|
||||
}
|
||||
|
||||
export enum SortType {
|
||||
ASC = 'ASC',
|
||||
DESC = 'DESC',
|
||||
}
|
||||
export type GetAllRecipesOperation<T extends RecipesType = RecipesType> = {
|
||||
data: { items: T['items'][], totalItems: number }
|
||||
variables: {
|
||||
take?: number
|
||||
sort?: {
|
||||
id: SortType
|
||||
}
|
||||
}
|
||||
}
|
68
framework/vendure/api/operations/get-all-recipe.ts
Normal file
68
framework/vendure/api/operations/get-all-recipe.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { OperationContext } from '@commerce/api/operations'
|
||||
import { Provider, VendureConfig } from '..'
|
||||
import { GetAllRecipesQuery,BlogList } from '../../schema'
|
||||
import { getAllBlogsQuery } from '../../utils/queries/get-all-blog-query'
|
||||
|
||||
export type BlogVariables = {
|
||||
excludeBlogIds?: string[],
|
||||
take?: number,
|
||||
skip?:number,
|
||||
filter?:{
|
||||
isFeatured?:{
|
||||
eq?:Boolean
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
export default function getAllRecipesOperation({
|
||||
commerce,
|
||||
}: OperationContext<Provider>) {
|
||||
async function getAllRecipes(opts?: {
|
||||
variables?: BlogVariables
|
||||
config?: Partial<VendureConfig>
|
||||
preview?: boolean
|
||||
}): Promise<{ recipes: GetAllRecipesQuery[],totalItems:number }>
|
||||
|
||||
async function getAllRecipes({
|
||||
query = getAllBlogsQuery,
|
||||
variables: { ...vars } = {},
|
||||
config: cfg,
|
||||
}: {
|
||||
query?: string
|
||||
variables?: BlogVariables
|
||||
config?: Partial<VendureConfig>
|
||||
preview?: boolean
|
||||
} = {}): Promise<{ recipes: GetAllRecipesQuery[] | any[] ,totalItems?:number }> {
|
||||
|
||||
const config = commerce.getConfig(cfg)
|
||||
const variables = {
|
||||
excludeBlogIds: vars.excludeBlogIds,
|
||||
options: {
|
||||
take: vars.take,
|
||||
filter: {
|
||||
isFeatured: vars.filter?.isFeatured
|
||||
}
|
||||
},
|
||||
}
|
||||
const { data } = await config.fetch<GetAllRecipesQuery>(query, {
|
||||
variables,
|
||||
})
|
||||
return {
|
||||
recipes: data?.blogs?.items?.map((val:BlogList)=>({
|
||||
id: val.id,
|
||||
title: val.translations[0]?.title,
|
||||
imageSrc: val.featuredAsset?.preview ?? null,
|
||||
slug: val.translations[0]?.slug,
|
||||
description: val.translations[0]?.description,
|
||||
isPublish: val.isPublish,
|
||||
isFeatured: val.isFeatured,
|
||||
authorName: val.authorName,
|
||||
authorAvatarAsset : val.authorAvatarAsset?.preview ?? null,
|
||||
createdAt: val.createdAt
|
||||
})),
|
||||
totalItems: data?.blogs?.totalItems || null
|
||||
}
|
||||
}
|
||||
|
||||
return getAllRecipes
|
||||
}
|
41
framework/vendure/schema.d.ts
vendored
41
framework/vendure/schema.d.ts
vendored
@@ -2384,6 +2384,37 @@ export type BlogList = Node &{
|
||||
isFeatured: Boolean
|
||||
}
|
||||
|
||||
export type RecipeList = Node &{
|
||||
id: ID!
|
||||
createdAt: DateTime!
|
||||
updatedAt: DateTime!
|
||||
featuredAsset?: Maybe<Asset>
|
||||
isPublish:Boolean
|
||||
translations: Array<RecipeTranslation>
|
||||
authorName: Scalars['String']
|
||||
authorAvatarAsset:Asset
|
||||
relevantProducts: Product[]
|
||||
link:String
|
||||
minutes:Number
|
||||
people:Number
|
||||
}
|
||||
|
||||
|
||||
export type RecipeTranslation = {
|
||||
__typename?: 'BlogTranslation'
|
||||
id: Scalars['ID']
|
||||
createdAt: Scalars['DateTime']
|
||||
updatedAt: Scalars['DateTime']
|
||||
languageCode: LanguageCode
|
||||
title: Scalars['String']
|
||||
slug: Scalars['String']
|
||||
description: Scalars['String']
|
||||
content: Scalars['String']
|
||||
Ingredients:Scalars['String']
|
||||
Preparation:Scalars['String']
|
||||
}
|
||||
|
||||
|
||||
export type GetBlogQuery = { __typename?: 'Query' } & {
|
||||
blog?: Maybe<
|
||||
{ __typename?: 'Blog' } & BlogList
|
||||
@@ -2391,8 +2422,6 @@ export type GetBlogQuery = { __typename?: 'Query' } & {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
export type BlogTranslation = {
|
||||
__typename?: 'BlogTranslation'
|
||||
id: Scalars['ID']
|
||||
@@ -2412,6 +2441,14 @@ export type GetAllBlogsQuery = PaginatedList & {
|
||||
}
|
||||
}
|
||||
|
||||
export type GetAllRecipesQuery = PaginatedList & {
|
||||
recipes: { __typename?: 'RecipeList' } & {
|
||||
items: Array<{ __typename?: 'Recipe' } & RecipeList!>,
|
||||
'totalItems'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export type GetRelevantBlogsQuery = PaginatedList & {
|
||||
relevantBlogs: { __typename?: 'BlogList' } & {
|
||||
items: Array<{ __typename?: 'Blog' } & BlogList!>,
|
||||
|
@@ -1,8 +1,11 @@
|
||||
import { Layout } from 'src/components/common';
|
||||
import RecipeListBanner from 'src/components/modules/recipes-list/RecipeListBanner/RecipeListBanner';
|
||||
import RecipesList from 'src/components/modules/recipes-list/RecipesList/RecipesList';
|
||||
|
||||
|
||||
import { GetStaticPropsContext } from 'next';
|
||||
import { PromiseWithKey } from 'src/utils/types.utils';
|
||||
import { DEFAULT_BLOG_PAGE_SIZE } from "src/utils/constanst.utils";
|
||||
import commerce from '@lib/api/commerce';
|
||||
import { getAllPromies } from 'src/utils/funtion.utils';
|
||||
export default function RecipeListPage() {
|
||||
return (
|
||||
<>
|
||||
@@ -12,4 +15,50 @@ export default function RecipeListPage() {
|
||||
)
|
||||
}
|
||||
|
||||
export async function getStaticProps({
|
||||
preview,
|
||||
locale,
|
||||
locales,
|
||||
}: GetStaticPropsContext) {
|
||||
|
||||
const config = { locale, locales }
|
||||
let promisesWithKey = [] as PromiseWithKey[]
|
||||
let props = {} as any;
|
||||
|
||||
|
||||
const blogsPromise = commerce.getAllBlogs({
|
||||
variables: {
|
||||
excludeBlogIds: [],
|
||||
take: DEFAULT_BLOG_PAGE_SIZE,
|
||||
filter: {
|
||||
isFeatured: {
|
||||
eq:false
|
||||
}
|
||||
}
|
||||
},
|
||||
config,
|
||||
preview,
|
||||
})
|
||||
promisesWithKey.push({ key: 'blogsResult', promise: blogsPromise })
|
||||
|
||||
|
||||
try {
|
||||
const promises = getAllPromies(promisesWithKey)
|
||||
const rs = await Promise.all(promises)
|
||||
|
||||
promisesWithKey.map((item, index) => {
|
||||
props[item.key] = item.keyResult ? rs[index][item.keyResult] : rs[index]
|
||||
return null
|
||||
})
|
||||
console.log(props);
|
||||
return {
|
||||
props,
|
||||
revalidate: 60
|
||||
}
|
||||
} catch (err) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
RecipeListPage.Layout = Layout
|
||||
|
Reference in New Issue
Block a user