This commit is contained in:
Joel Varty
2021-06-21 15:55:31 -04:00
parent 7cc26439f4
commit 206d221f34
29 changed files with 827 additions and 449 deletions

View 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 }

View 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}

View 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 }

View 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 }

View 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 }

View 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
View 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
}