feat: get-blog-detail and relevant blogs

This commit is contained in:
Quangnhankie
2021-10-20 10:11:07 +07:00
parent abea83cdd6
commit 7ea734d0ad
16 changed files with 459 additions and 98 deletions

View File

@@ -1,18 +1,21 @@
import type { CommerceAPIConfig } from '@commerce/api'
import { CommerceAPI, getCommerceApi as commerceApi } from '@commerce/api'
import getAllFacets from './operations/get-all-facets'
import getAllBlogs from './operations/get-all-blogs'
import getAllCollections from './operations/get-all-collection'
import getAllFacets from './operations/get-all-facets'
import getAllPages from './operations/get-all-pages'
import getAllProductPaths from './operations/get-all-product-paths'
import getAllProducts from './operations/get-all-products'
import getBlogDetail from './operations/get-blog-detail'
import getCustomerWishlist from './operations/get-customer-wishlist'
import getFeaturedBlog from './operations/get-featured-blog'
import getPage from './operations/get-page'
import getProduct from './operations/get-product'
import getSiteInfo from './operations/get-site-info'
import getAllBlogPaths from './operations/get-all-blog-paths'
import getRelevantBlogs from './operations/get-relevant-blogs'
import login from './operations/login'
import fetchGraphqlApi from './utils/fetch-graphql-api'
import getAllBlogs from './operations/get-all-blogs'
import getFeaturedBlog from './operations/get-featured-blog'
export interface VendureConfig extends CommerceAPIConfig {}
@@ -46,7 +49,10 @@ const operations = {
getAllFacets,
getAllCollections,
getAllBlogs,
getFeaturedBlog
getFeaturedBlog,
getBlogDetail,
getAllBlogPaths,
getRelevantBlogs
}
export const provider = { config, operations }

View File

@@ -0,0 +1,53 @@
import { BlogList } from './../../schema.d';
import { OperationContext,OperationOptions } from '@commerce/api/operations';
import { BigcommerceConfig } from '../../../bigcommerce/api';
import type { GetAllBlogPathsQuery,BlogTranslation } from '../../schema';
import { getAllBlogPathsQuery } from '../../utils/queries/get-all-blog-paths-query';
import { Provider } from '../index';
import { GetAllBlogPathsOperation } from './../../../commerce/types/blogs';
export type GetAllBlogPathsResult = {
blogs: Array<{ node: { path: string } }>
}
export default function getAllBlogPathsOperation({
commerce,
}: OperationContext<Provider>) {
async function getAllBlogPaths<
T extends GetAllBlogPathsOperation
>(opts?: {
variables?: T['variables']
config?: BigcommerceConfig
}): Promise<T['data']>
async function getAllBlogPaths<T extends GetAllBlogPathsOperation>(
opts: {
variables?: T['variables']
config?: BigcommerceConfig
} & OperationOptions
): Promise<T['data']>
async function getAllBlogPaths<T extends GetAllBlogPathsOperation>({
query = getAllBlogPathsQuery,
variables,
config: cfg,
}: {
query?: string
variables?: T['variables']
config?: BigcommerceConfig
} = {}): Promise<T['data']> {
const config = commerce.getConfig(cfg)
const { data } = await config.fetch<GetAllBlogPathsQuery>(query, {
variables,
})
const blogs = data.blogs.items;
return {
blogs: blogs?.map(val=>val.translations.map((p:BlogTranslation) => ({ path: `/${p.slug}` })))
}
}
return getAllBlogPaths
}

View File

@@ -0,0 +1,55 @@
import { OperationContext } from '@commerce/api/operations'
import { Provider, VendureConfig } from '..'
import { GetBlogQuery,BlogList } from '../../schema'
import { getBlogDetailQuery } from '../../utils/queries/get-blog-detail'
export type BlogVariables = {
slug?: string,
}
export default function getBlogDetailOperation({
commerce,
}: OperationContext<Provider>) {
async function getBlogDetail(opts?: {
variables?: BlogVariables
config?: Partial<VendureConfig>
preview?: boolean
}): Promise<{ blogDetail: BlogList}>
async function getBlogDetail({
query = getBlogDetailQuery,
variables: { ...vars } = {},
config: cfg,
}: {
query?: string
variables?: BlogVariables
config?: Partial<VendureConfig>
preview?: boolean
} = {}): Promise<{ blogDetail: BlogList | any }> {
const config = commerce.getConfig(cfg)
const variables = {
slug: vars.slug
}
const { data } = await config.fetch<GetBlogQuery>(query, {
variables,
})
return {
blogDetail: {
id:data?.blog?.id,
title: data?.blog?.translations[0].title,
imageSrc: data?.blog?.featuredAsset?.preview ?? null,
slug: data?.blog?.translations[0]?.slug,
description: data?.blog?.translations[0]?.description,
isPublish: data?.blog?.isPublish,
isFeatured: data?.blog?.isFeatured,
authorName: data?.blog?.authorName,
authorAvatarAsset : data?.blog?.authorAvatarAsset?.preview,
createdAt: data?.blog?.createdAt,
relevantProducts: data?.blog?.relevantProducts.map(val=>val.id)
}
}
}
return getBlogDetail
}

View File

@@ -0,0 +1,54 @@
import { OperationContext } from '@commerce/api/operations'
import { Provider, VendureConfig } from '..'
import { BlogList,GetRelevantBlogsQuery } from '../../schema'
import { getRelevantBlogsQuery } from '../../utils/queries/get-relevant-blogs'
export type BlogVariables = {
productId?: number,
}
export default function getRelevantBlogsOperation({
commerce,
}: OperationContext<Provider>) {
async function getRelevantBlogs(opts?: {
variables?: BlogVariables
config?: Partial<VendureConfig>
preview?: boolean
}): Promise<{ relevantBlogs: GetRelevantBlogsQuery[]}>
async function getRelevantBlogs({
query = getRelevantBlogsQuery,
variables: { ...vars } = {},
config: cfg,
}: {
query?: string
variables?: BlogVariables
config?: Partial<VendureConfig>
preview?: boolean
} = {}): Promise<{ relevantBlogs: GetRelevantBlogsQuery[] | any[] }> {
const config = commerce.getConfig(cfg)
const variables = {
productId: vars.productId,
}
const { data } = await config.fetch<GetRelevantBlogsQuery>(query, {
variables,
})
return {
relevantBlogs: data?.relevantBlogs?.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,
createdAt: val.createdAt
})),
}
}
return getRelevantBlogs
}