-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgatsby-node.js
73 lines (65 loc) · 2.2 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const path = require('path');
const { keyframes } = require('styled-components');
const { isArray } = require('util');
exports.createPages = async function({actions, graphql}) {
const {data} = await graphql(`
query ProuctTypeFilter {
allDatoCmsProduct {
nodes {
id
title
price
productType
slug
image {
filename
fluid(maxWidth: 200) {
sizes
src
}
}
fandoms
}
}
}
`)
let filteredObjects = {};
// filter all products into product type
data.allDatoCmsProduct.nodes.forEach( node => {
if (filteredObjects.hasOwnProperty(node.productType)) {
filteredObjects[node.productType].push(node)
} else {
filteredObjects[node.productType] = []
filteredObjects[node.productType].push(node)
}
})
let organizedObjects = {}
// organize all products and product types by sub fandoms
for (const productType in filteredObjects) {
organizedObjects[productType] = {}
// for each product within each product type
filteredObjects[productType].forEach( product => {
// if productType contains product.fandom property, push it to that fandom array
if (organizedObjects[productType].hasOwnProperty(product.fandoms)) {
organizedObjects[productType][product.fandoms].push(product)
} else {
organizedObjects[productType][product.fandoms] = []
organizedObjects[productType][product.fandoms].push(product)
}
})
}
// for each product type
for (const productType in organizedObjects) {
let lowerProductType = productType.toLowerCase();
let fandomList = Object.keys(organizedObjects[productType]);
// for each item in each product type
for (const fandom in organizedObjects[productType]) {
let lowerFandom = fandom.replace(/\s/g, '-').toLowerCase()
actions.createPage({
path: `/${lowerProductType}/${lowerFandom}`,
component: require.resolve(`./src/templates/productTypeFilter.jsx`),
context: { pageContext: organizedObjects[productType][fandom], productType: productType, fandomList: fandomList }
})
}
}
}