forked from jamstack-cms/jamstack-ecommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.esm.js
More file actions
168 lines (147 loc) · 4.07 KB
/
gatsby-node.esm.js
File metadata and controls
168 lines (147 loc) · 4.07 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import getInventory from './providers/inventoryProvider.js'
import { slugify } from './utils/helpers'
const ItemView = require.resolve('./src/templates/ItemView')
const CategoryView = require.resolve('./src/templates/CategoryView')
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const inventory = await getInventory()
createPage({
path: 'all',
component: CategoryView,
context: {
content: inventory,
title: 'all',
type: "categoryPage"
},
})
const inventoryByCategory = inventory.reduce((acc, next) => {
const categories = next.categories
categories.forEach(c => {
if (acc[c]) {
acc[c].items.push(next)
} else {
acc[c] = {}
acc[c].items = []
acc[c].items.push(next)
}
})
return acc
}, {})
const categories = Object.keys(inventoryByCategory)
categories.map(async(category, index) => {
const previous = index === categories.length - 1 ? null : categories[index + 1].node
const next = index === 0 ? null : categories[index - 1]
createPage({
path: slugify(category),
component: CategoryView,
context: {
content: inventoryByCategory[category],
title: category,
type: "categoryPage",
previous,
next,
},
})
})
inventory.map(async(item, index) => {
const previous = index === inventory.length - 1 ? null : inventory[index + 1].node
const next = index === 0 ? null : inventory[index - 1]
createPage({
path: slugify(item.name),
component: ItemView,
context: {
content: item,
title: item.name,
type: "itemPage",
previous,
next,
},
})
})
}
exports.sourceNodes = async ({ actions, createNodeId, createContentDigest }) => {
const { createNode } = actions
const inventory = await getInventory()
/* create nav info for categories */
const categoryNames = inventory.reduce((acc, next) => {
next.categories.forEach(c => {
if (!acc.includes(c)) acc.push(c)
})
return acc
}, [])
const navData = {
key: 'nav-info',
data: categoryNames
}
const navNodeContent = JSON.stringify(navData)
const navNodeMeta = {
id: createNodeId(`my-data-${navData.key}`),
parent: null,
children: [],
internal: {
type: `NavInfo`,
mediaType: `json`,
content: navNodeContent,
contentDigest: createContentDigest(navData)
}
}
const navNode = Object.assign({}, navData, navNodeMeta)
createNode(navNode)
/* create category info for home page */
const inventoryByCategory = inventory.reduce((acc, next) => {
const categories = next.categories
categories.forEach(c => {
const index = acc.findIndex(item => item.name === c)
if (index !== -1) {
const item = acc[index]
item.itemCount = item.itemCount + 1
acc[index] = item
} else {
const item = {
name: c,
image: next.image,
itemCount: 1
}
acc.push(item)
}
})
return acc
}, [])
const catData = {
key: 'category-info',
data: inventoryByCategory
}
const catNodeContent = JSON.stringify(catData)
const catNodeMeta = {
id: createNodeId(`my-data-${catData.key}`),
parent: null,
children: [],
internal: {
type: `CategoryInfo`,
mediaType: `json`,
content: catNodeContent,
contentDigest: createContentDigest(catData)
}
}
const catNode = Object.assign({}, catData, catNodeMeta)
createNode(catNode)
/* all inventory */
const inventoryData = {
key: 'all-inventory',
data: inventory
}
const inventoryNodeContent = JSON.stringify(inventoryData)
const inventoryNodeMeta = {
id: createNodeId(`my-data-${inventoryData.key}`),
parent: null,
children: [],
internal: {
type: `InventoryInfo`,
mediaType: `json`,
content: inventoryNodeContent,
contentDigest: createContentDigest(inventoryData)
}
}
const inventoryNode = Object.assign({}, inventoryData, inventoryNodeMeta)
createNode(inventoryNode)
}