-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
143 lines (130 loc) · 3.42 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
const React = require("react")
const path = require("path")
const slugify = require("slugify")
const { paginate } = require("gatsby-awesome-pagination")
const {
getParentRecursively,
getChildrenRecursively,
getChildren,
} = require("./src/utils")
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
const productIndex = path.resolve(`./src/templates/productIndex.js`)
const productView = path.resolve(`./src/templates/productView.js`)
const tagView = path.resolve(`./src/templates/tagView.js`)
const categoryView = path.resolve(`./src/templates/categoryView.js`)
const result = await graphql(`
{
products: allProductsJson {
edges {
node {
_id
slug
parent_id
}
}
}
tags: allProductsJson {
group(field: tags) {
fieldValue
totalCount
edges {
node {
_id
}
}
}
}
categories: allCategoriesJson {
edges {
node {
_id
name
parent_id
}
}
}
}
`)
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
const products = result.data.products.edges
const _categories = result.data.categories.edges
const categories = _categories.map((i) => i.node)
const tags = result.data.tags.group
console.log("products:" + products.length)
console.log("categories:" + categories.length)
paginate({
createPage,
items: products,
component: productIndex,
itemsPerPage: 50,
// itemsPerFirstPage: 3,
pathPrefix: "/",
})
products.forEach(({ node }) => {
createPage({
path: node.slug,
component: productView,
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
_id: node._id,
allSupercategories: getParentRecursively(categories, node).reverse(),
},
})
})
for (const i of categories) {
const allSupercategories = getParentRecursively(categories, i).reverse()
const allSubcategories = getChildrenRecursively(categories, i)
const allSubcategoriesIds = [i._id, ...allSubcategories.map((i) => i._id)]
const subcategories = getChildren(allSubcategories, i._id)
const result = await graphql(
`
query($allSubcategoriesIds: [String]) {
allProductsJson(filter: { parent_id: { in: $allSubcategoriesIds } }) {
edges {
node {
_id
}
}
}
}
`,
{ allSubcategoriesIds }
)
if (result.errors) {
reporter.panicOnBuild(`2 Error while running GraphQL query.`)
return
}
const products = result.data.allProductsJson.edges
paginate({
createPage,
items: products,
component: categoryView,
itemsPerPage: 50,
context: {
category: i,
allSupercategories,
subcategories,
allSubcategoriesIds,
productsCount: products.length,
},
pathPrefix: `/${slugify(i.name.toLowerCase())}`,
})
}
tags.forEach((tag) => {
paginate({
createPage,
items: tag.edges,
component: tagView,
itemsPerPage: 50,
context: {
tag: tag.fieldValue,
},
pathPrefix: `/tags/${slugify(tag.fieldValue.toLowerCase())}`,
})
})
}