Skip to content
This repository has been archived by the owner on Apr 26, 2022. It is now read-only.

Commit

Permalink
feat: Collection nodes (#12)
Browse files Browse the repository at this point in the history
* feat: Build nodes for collection resources

* feat: Add products field to collection nodes

Link to existing product nodes so we can use  with main image

* feat: Add collections to product nodes
  • Loading branch information
ynnoj authored Feb 27, 2019
1 parent 3b095c8 commit 572ea0d
Showing 1 changed file with 83 additions and 24 deletions.
107 changes: 83 additions & 24 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,68 @@ exports.sourceNodes = async (

const moltin = new createClient({ client_id })

const processProduct = ({ product, main_images, categories }) => {
const processProduct = ({
product,
main_images,
categories,
collections
}) => {
const nodeContent = JSON.stringify(product)

let categoriesArray
let collectionsArray
let mainImageHref

if (product.relationships.main_image) {
const {
link: { href }
} = main_images.find(
main_image => main_image.id === product.relationships.main_image.data.id
)
if (product.relationships) {
if (product.relationships.main_image) {
const {
link: { href }
} = main_images.find(
main_image =>
main_image.id === product.relationships.main_image.data.id
)

mainImageHref = href
}
mainImageHref = href
}

if (product.relationships.categories) {
categoriesArray = product.relationships.categories.data.map(
relationship => {
const category = categories.find(
category => category.id === relationship.id
)
if (product.relationships.categories) {
categoriesArray = product.relationships.categories.data.map(
relationship => {
const category = categories.find(
category => category.id === relationship.id
)

if (category) {
return {
...category
}
}
}
)
}

if (category) {
return {
...category
if (product.relationships.collections) {
collectionsArray = product.relationships.collections.data.map(
relationship => {
const collection = collections.find(
collection => collection.id === relationship.id
)

if (collection) {
return {
...collection
}
}
}
}
)
)
}
}

const nodeData = {
...product,
id: product.id,
categories: categoriesArray,
collections: collectionsArray,
mainImageHref,
parent: null,
children: [],
Expand Down Expand Up @@ -76,7 +102,26 @@ exports.sourceNodes = async (
return nodeData
}

const processCollection = ({ collection }) => {
const nodeContent = JSON.stringify(collection)

const nodeData = {
...collection,
id: collection.id,
parent: null,
children: [],
internal: {
type: `MoltinCollection`,
content: nodeContent,
contentDigest: createContentDigest(collection)
}
}

return nodeData
}

const { data: categories } = await moltin.get('categories')
const { data: collections } = await moltin.get('collections')
const {
data: products,
included: { main_images }
Expand All @@ -88,13 +133,27 @@ exports.sourceNodes = async (
)
}

const createProducts = async ({ products, main_images, categories }) => {
const createCollections = async ({ collections }) => {
collections.forEach(async collection =>
createNode(await processCollection({ collection }))
)
}

const createProducts = async ({
products,
main_images,
categories,
collections
}) => {
products.forEach(async product =>
createNode(await processProduct({ product, main_images, categories }))
createNode(
await processProduct({ product, main_images, categories, collections })
)
)
}

await createProducts({ products, main_images, categories })
await createProducts({ products, main_images, categories, collections })
await createCollections({ collections })
await createCategories({ categories })
}

Expand Down Expand Up @@ -129,7 +188,7 @@ exports.onCreateNode = async ({
}

if (
node.internal.type === `MoltinCategory` &&
[`MoltinCategory`, `MoltinCollection`].includes(node.internal.type) &&
node.relationships &&
node.relationships.products
) {
Expand Down

0 comments on commit 572ea0d

Please sign in to comment.