-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
319 lines (283 loc) · 7.07 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
const WooCommerceRestApi = require("@woocommerce/woocommerce-rest-api").default;
const { createRemoteFileNode } = require("gatsby-source-filesystem")
require("dotenv").config({
path: `.env.${process.env.NODE_ENV}`,
})
const api = new WooCommerceRestApi({
url: `${process.env.GATSBY_WORDPRESS_URL}/`,
consumerKey: process.env.GATSBY_WOO_KEY,
consumerSecret: process.env.GATSBY_WOO_SECRET,
version: 'wc/v3'
});
exports.sourceNodes = async (
{ actions: { createNode }, createNodeId, createContentDigest, store, cache }) => {
async function get_products() {
const totalProducts = [];
let page = 1
while (true) {
const options = { per_page: 100, status: 'publish' };
if (page !== 1) {
options.page = page
}
const products = await api.get("products", options);
totalProducts.push(...products.data);
page++
if (products.data.length != 100) {
break;
}
}
return totalProducts;
}
let products = await get_products()
let categories = await api.get("products/categories")
const processProduct = async (product, args) => {
product.images = await Promise.all(product.images.map(async image => {
let fileNode
try {
fileNode = await createRemoteFileNode({
url: image.src,
...args
})
} catch (e) {
console.log("e", e)
}
if (fileNode) {
image.localFile___NODE = fileNode.id
return image
}
}))
const nodeId = createNodeId(`wc-product-${product.id}`)
const nodeContent = JSON.stringify(product)
return Object.assign({}, product, {
id: nodeId,
databaseId: product.id,
parent: null,
children: [],
internal: {
type: `wcProduct`,
content: nodeContent,
contentDigest: createContentDigest(product)
}
})
}
const processCategory = async (category, args) => {
if (!Array.isArray(category.image)) {
let fileNode
try {
fileNode = await createRemoteFileNode({
url: category.image.src,
...args
})
} catch (e) {
console.log("e", e)
}
if (fileNode) {
category.image.localFile___NODE = fileNode.id
}
}
if (Array.isArray(category.acf.gallery)) {
category.acf.gallery = await Promise.all(category?.acf?.gallery?.map(async ({ image }) => {
let fileNode
try {
fileNode = await createRemoteFileNode({
url: image.url,
...args
})
} catch (e) {
console.log("e", e)
}
if (fileNode) {
image.localFile___NODE = fileNode.id
return image
}
}))
} else {
category.acf.gallery = []
}
const nodeId = createNodeId(`wc-category-${category.id}`)
const nodeContent = JSON.stringify(category)
return Object.assign({}, category, {
id: nodeId,
databaseId: category.id,
parent: null,
children: [],
internal: {
type: `wcCategory`,
content: nodeContent,
contentDigest: createContentDigest(category)
}
})
}
await asyncForEach(products, async (product) => {
const productNode = await processProduct(product, { store, cache, createNode, createNodeId })
createNode(productNode)
})
await asyncForEach(categories.data, async (category) => {
const categoryNode = await processCategory(category, { store, cache, createNode, createNodeId })
createNode(categoryNode)
})
};
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}
exports.createPages = async ({ actions: { createPage, createRedirect }, graphql }) => {
const { data: { allWcProduct } } = await graphql(`
{
allWcProduct {
nodes {
name
slug
id
databaseId
categories {
slug
name
}
}
}
}
`);
allWcProduct.nodes.map(el => {
if (!el.categories[0]) return null
createPage({
path: `/sklep/${el.categories[0].slug}/${el.slug}/`,
component: el.categories[0].slug !== 'wystawy'
? require.resolve("./src/templates/product-page.js")
: require.resolve("./src/templates/exhibition-page.js"),
context: {
itemId: el.databaseId,
id: el.id,
title: el.name,
url: `/sklep/${el.categories[0].slug}/${el.slug}/`,
breadCrumbs: [
{
item: 'Sklep',
url: '/sklep/'
},
{
item: el.categories[0].name,
url: `/sklep/${el.categories[0].slug}/`
},
{
item: el.name,
url: `/sklep/${el.categories[0].slug}/${el.slug}/`
}
]
},
});
});
const { data: { allWcCategory } } = await graphql(`
{
allWcCategory {
nodes {
name
slug
id
count
}
}
}
`);
allWcCategory.nodes.map(el => {
createPage({
path: `/sklep/${el.slug}/`,
component: require.resolve(
"./src/templates/category-page.js"
),
context: {
id: el.id,
title: el.name,
slug: el.slug,
url: `/sklep/${el.slug}/`,
breadCrumbs: [
{
item: 'Sklep',
url: '/sklep/'
},
{
item: el.name,
url: `/sklep/${el.slug}/`
}
]
},
});
// if (el.count < 1) {
// createRedirect({
// fromPath: `/sklep/modele/${el.slug}/`,
// toPath: '/sklep/',
// force: true
// })
// }
});
const kolekcjeData = await graphql(`
{
allWpKolekcje {
edges {
node {
id
slug
}
}
}
}
`);
kolekcjeData.data.allWpKolekcje.edges.map(({ node }) => {
createPage({
path: `/kolekcje-modeli/${node.slug}/`,
component: require.resolve(
"./src/components/Templates/CollectionTemplate/CollectionTemplate.js"
),
context: {
kolekcjaId: node.id,
},
});
});
const wystawyData = await graphql(`
{
allWpWystawa {
edges {
node {
slug
id
}
}
}
}
`);
wystawyData.data.allWpWystawa.edges.map(({ node }) => {
createPage({
path: `/wystawy/${node.slug}/`,
component: require.resolve(
"./src/components/Templates/TemplateExhibitions/TemplateExhibitions.js"
),
context: {
wystawaId: node.id,
},
});
});
const artykulData = await graphql(`
{
allWpArtykul {
edges {
node {
id
slug
}
}
}
}
`);
artykulData.data.allWpArtykul.edges.map(({ node }) => {
createPage({
path: `/artykuly/${node.slug}/`,
component: require.resolve(
"./src/components/Templates/ArticlesTemplate/ArticlesTemplate.js"
),
context: {
articleId: node.id,
},
});
});
};