mirror of
https://github.com/vercel/commerce.git
synced 2025-07-27 04:01:23 +00:00
updates
This commit is contained in:
86
lib/module-data/BlogPostListing.ts
Normal file
86
lib/module-data/BlogPostListing.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
|
||||
const getCustomInitialProps = async ({
|
||||
agility,
|
||||
channelName,
|
||||
languageCode,
|
||||
}:any) => {
|
||||
// set up api
|
||||
const api = agility;
|
||||
|
||||
|
||||
|
||||
// get sitemap...
|
||||
let sitemap = await api.getSitemap({
|
||||
channelName: channelName,
|
||||
languageCode,
|
||||
});
|
||||
|
||||
// get posts...
|
||||
let rawPosts = await api.getContentList({
|
||||
referenceName: "blogposts",
|
||||
languageCode,
|
||||
take: 50,
|
||||
contentLinkDepth: 2,
|
||||
depth: 2
|
||||
});
|
||||
|
||||
// resolve dynamic urls
|
||||
const dynamicUrls: [] = resolvePostUrls(sitemap, rawPosts.items);
|
||||
const posts: [] = rawPosts.items.map((post: any) => {
|
||||
|
||||
const productJSON = post.fields.product
|
||||
const product = JSON.parse(productJSON)
|
||||
|
||||
const productName = product.name
|
||||
const productImageSrc = product.imageUrl
|
||||
|
||||
// date
|
||||
const date = new Date(post.fields.date).toLocaleDateString();
|
||||
|
||||
// url
|
||||
const url = dynamicUrls[post.contentID] || "#";
|
||||
|
||||
// post image src
|
||||
let imageSrc = post.fields.image?.url || null
|
||||
|
||||
// post image alt
|
||||
let imageAlt = post.fields.image?.label || null;
|
||||
|
||||
return {
|
||||
contentID: post.contentID,
|
||||
title: post.fields.title,
|
||||
date,
|
||||
url,
|
||||
productName,
|
||||
productImageSrc,
|
||||
imageSrc,
|
||||
imageAlt,
|
||||
};
|
||||
});
|
||||
|
||||
//sort newest first...
|
||||
posts.sort((a: any, b: any) => {
|
||||
return b.date.localeCompare(a.date);
|
||||
})
|
||||
|
||||
return {
|
||||
posts,
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
|
||||
// function to resole post urls
|
||||
const resolvePostUrls = function (sitemap: any, posts: any): any {
|
||||
let dynamicUrls: any = {};
|
||||
posts.forEach((post: any) => {
|
||||
Object.keys(sitemap).forEach((path) => {
|
||||
if (sitemap[path].contentID === post.contentID) {
|
||||
dynamicUrls[post.contentID] = path;
|
||||
}
|
||||
});
|
||||
});
|
||||
return dynamicUrls;
|
||||
};
|
||||
|
||||
export default { getCustomInitialProps }
|
17
lib/module-data/CartData.ts
Normal file
17
lib/module-data/CartData.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import commerce from '@lib/api/commerce'
|
||||
|
||||
const getCustomInitialProps = async function ({ }):Promise<{pages:any, categories: any}> {
|
||||
const languageCode = "en-us"
|
||||
const preview = false
|
||||
const config = { locale: languageCode, locales: [languageCode] }
|
||||
const pagesPromise = commerce.getAllPages({ config, preview })
|
||||
const siteInfoPromise = commerce.getSiteInfo({ config, preview })
|
||||
const { pages } = await pagesPromise
|
||||
const { categories } = await siteInfoPromise
|
||||
return {
|
||||
pages, categories
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default {getCustomInitialProps}
|
26
lib/module-data/ProductDetailsData.ts
Normal file
26
lib/module-data/ProductDetailsData.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import commerce from '@lib/api/commerce'
|
||||
|
||||
const getCustomInitialProps = async function ({ item, agility, languageCode, channelName, pageInSitemap, dynamicPageItem }: any) {
|
||||
//TODO: pass the locale and preview mode as props...
|
||||
|
||||
|
||||
const locale = "en-us"
|
||||
const preview = false
|
||||
|
||||
const config = { locale, locales: [locale] }
|
||||
const productsPromise = commerce.getAllProducts({
|
||||
variables: { first: 4 },
|
||||
config,
|
||||
preview,
|
||||
})
|
||||
|
||||
|
||||
const { products } = await productsPromise
|
||||
|
||||
return {
|
||||
products
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default { getCustomInitialProps }
|
28
lib/module-data/ProductListing.ts
Normal file
28
lib/module-data/ProductListing.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import commerce from '@lib/api/commerce'
|
||||
|
||||
const getCustomInitialProps = async function ({ item, agility, languageCode, channelName, pageInSitemap, dynamicPageItem }: any) {
|
||||
//TODO: pass the locale and preview mode as props...
|
||||
|
||||
const locale = "en-US"
|
||||
const preview = false
|
||||
|
||||
const numItems = parseInt(item.fields.numItems) || 10
|
||||
|
||||
const config = { locale, locales: [locale] }
|
||||
const productsPromise = commerce.getAllProducts({
|
||||
variables: { first: numItems },
|
||||
config,
|
||||
preview,
|
||||
// Saleor provider only
|
||||
...({ featured: true } as any),
|
||||
})
|
||||
|
||||
const { products } = await productsPromise
|
||||
|
||||
return {
|
||||
products
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default { getCustomInitialProps }
|
27
lib/module-data/ProductMarquee.ts
Normal file
27
lib/module-data/ProductMarquee.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import commerce from '@lib/api/commerce'
|
||||
|
||||
const getCustomInitialProps = async function ({ item, agility, languageCode, channelName, pageInSitemap, dynamicPageItem }: any) {
|
||||
//TODO: pass the locale and preview mode as props...
|
||||
|
||||
|
||||
const locale = "en-US"
|
||||
const preview = false
|
||||
|
||||
const config = { locale, locales: [locale] }
|
||||
const productsPromise = commerce.getAllProducts({
|
||||
variables: { first: 6 },
|
||||
config,
|
||||
preview,
|
||||
// Saleor provider only
|
||||
...({ featured: true } as any),
|
||||
})
|
||||
|
||||
const { products } = await productsPromise
|
||||
|
||||
return {
|
||||
products
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default { getCustomInitialProps }
|
25
lib/module-data/ProductSearchData.ts
Normal file
25
lib/module-data/ProductSearchData.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import commerce from '@lib/api/commerce'
|
||||
|
||||
const getCustomInitialProps = async ({ agility, channelName, languageCode }:any) => {
|
||||
|
||||
//TODO: pass the locale and preview mode as props...
|
||||
|
||||
const locale = "en-US"
|
||||
const preview = false
|
||||
const config = { locale, locales: [locale] }
|
||||
const pagesPromise = commerce.getAllPages({ config, preview })
|
||||
const siteInfoPromise = commerce.getSiteInfo({ config, preview })
|
||||
|
||||
const { categories, brands } = await siteInfoPromise
|
||||
return {
|
||||
categories,
|
||||
brands,
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
export default {
|
||||
getCustomInitialProps
|
||||
}
|
27
lib/module-data/index.ts
Normal file
27
lib/module-data/index.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
import CartData from "./CartData"
|
||||
import ProductListing from "./ProductListing"
|
||||
import ProductMarquee from "./ProductMarquee"
|
||||
import ProductSearchData from "./ProductSearchData"
|
||||
import ProductDetailsData from "./ProductDetailsData"
|
||||
import BlogPostListing from "./BlogPostListing"
|
||||
|
||||
const allModules:any =[
|
||||
|
||||
{ name: "ProductListing", init: ProductListing},
|
||||
{ name: "ProductMarquee", init: ProductMarquee},
|
||||
{ name: "ProductSearch", init: ProductSearchData},
|
||||
{ name: "Cart", init: CartData},
|
||||
{ name: "ProductDetails", init: ProductDetailsData},
|
||||
{ name: "BlogPostListing", init: BlogPostListing}
|
||||
]
|
||||
|
||||
/**
|
||||
* Find the data method for a module by module reference name.
|
||||
* @param moduleName
|
||||
*/
|
||||
export default (moduleName:string):any => {
|
||||
const obj = allModules.find((m: { name: string }) => m.name.toLowerCase() === moduleName.toLowerCase())
|
||||
if (!obj) return null
|
||||
return obj.init
|
||||
}
|
Reference in New Issue
Block a user