-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.esm.js
195 lines (178 loc) · 4.43 KB
/
gatsby-node.esm.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import getInventory from "./providers/inventoryProvider.js"
import { slugify } from "./utils/helpers"
import { getHeader } from "./providers/inventoryHelper"
const { createRemoteFileNode } = require("gatsby-source-filesystem")
const ItemView = require.resolve("./src/templates/ItemView")
const CategoryView = require.resolve("./src/templates/CategoryView")
exports.createSchemaCustomization = ({ actions: { createTypes } }) => {
createTypes(`
type InventoryInfo implements Node {
cover: productImage
}
type productImage @dontInfer {
url: File @link(by: "url")
}
`)
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const { data } = await graphql(`
query {
allInventoryInfo {
nodes {
id
price
name
subtitle
authors
description
categories
ean
weight
width
height
thickness
edition
publicationDate
format
keywords
brand
cover {
url {
childImageSharp {
gatsbyImageData(
layout: CONSTRAINED
placeholder: BLURRED
formats: [AUTO, WEBP]
)
}
}
}
}
}
}
`)
const inventory = data.allInventoryInfo.nodes
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: {
category: 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: {
ean: item.ean,
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 inventoryByCategory_temp = inventory.reduce((acc, next) => {
const categories = next.categories
categories.forEach((c) => {
c = c.toLowerCase()
if (acc[c]) {
acc[c] += 1
} else {
acc[c] = 1
}
})
return acc
}, {})
// Get only top 5 categories by item count
const sorted_categories = Object.keys(inventoryByCategory_temp).sort(
function (a, b) {
return inventoryByCategory_temp[b] - inventoryByCategory_temp[a]
}
)
const categoryNames = sorted_categories.slice(0, 5)
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)
inventory.forEach((product) => {
createNode({
...product,
id: createNodeId(`product-${product.ean}`),
internal: {
type: `InventoryInfo`,
mediaType: `json`,
contentDigest: createContentDigest(product),
},
})
})
}
exports.onCreateNode = async ({
node,
actions: { createNode },
store,
cache,
createNodeId,
}) => {
if (node.internal.type === "InventoryInfo") {
node.cover = await createRemoteFileNode({
url: node.image,
parentNodeId: node.id,
httpHeaders: getHeader("cover"),
createNode,
createNodeId,
cache,
store,
})
}
}