-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
189 lines (176 loc) · 4.33 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
const path = require('path');
const KEYWORDS = [
'teach kids code',
'teach your kids to code',
'how to teach coding kids',
'coding games for kids',
'computer coding for kids',
'coding for beginners',
'how to teach coding',
'teaching kids to code',
];
const createPagesMain = (actions, template, toys, categories, materials) => {
actions.createPage({
path: `/`,
component: template,
context: {
keywords: [...KEYWORDS],
data: {
toys,
categories,
materials,
query: {
category: null,
material: null,
age: null,
},
},
},
});
};
const createPagesCategory = (
actions,
template,
toys,
categories,
materials
) => {
categories.forEach(({ value }) => {
actions.createPage({
path: `/teach-kids-code-with-${value.replace(/\s/g, '-').toLowerCase()}`,
component: template,
context: {
keywords: [`teach kids code with ${value.toLowerCase()}`, ...KEYWORDS],
data: {
toys,
categories,
materials,
query: {
category: value,
material: null,
age: null,
},
},
},
});
});
};
const createPagesMaterial = (
actions,
template,
toys,
categories,
materials
) => {
materials.forEach(({ value }) => {
actions.createPage({
path: `/teach-kids-code-with-toys-made-from-${value
.replace(/\s/g, '-')
.toLowerCase()}`,
component: template,
context: {
keywords: [
`teach kids code with toys made from ${value.toLowerCase()}`,
...KEYWORDS,
],
data: {
toys,
categories,
materials,
query: {
category: null,
material: value,
age: null,
},
},
},
});
});
};
const createPagesAge = (actions, template, toys, categories, materials) => {
new Array(22)
.fill(0)
.map((v, i) => v + i)
.forEach(value => {
actions.createPage({
path: `/teach-kids-code-from-age-${value}-years`,
component: template,
context: {
keywords: [`teach kids code from age ${value} years`, ...KEYWORDS],
data: {
toys,
categories,
materials,
query: {
category: null,
material: null,
age: value,
},
},
},
});
});
};
exports.createPages = ({ graphql, actions }) => {
return new Promise((resolve, reject) => {
resolve(
graphql(
`
{
allContentfulToy {
edges {
node {
id
name
url
imgSrcOne
imgSrcTwo
categories
materials
minAge
maxAge
priceDecimal
comment
}
}
}
}
`
).then(result => {
if (result.errors) {
reject(result.errors);
}
const template = path.resolve(`src/templates/index.js`);
const toys = result.data.allContentfulToy.edges.map(edge => edge.node);
const categories = toys.reduce((result, { categories }) => {
categories.forEach(category => {
if (!result.map(v => v.key).includes(category)) {
result.push({
key: category,
value: category,
text: category,
});
}
});
return result;
}, []);
const materials = toys.reduce((result, { materials }) => {
materials.forEach(material => {
if (!result.map(v => v.key).includes(material)) {
result.push({
key: material,
value: material,
text: material,
});
}
});
return result;
}, []);
createPagesMain(actions, template, toys, categories, materials);
createPagesCategory(actions, template, toys, categories, materials);
createPagesMaterial(actions, template, toys, categories, materials);
createPagesAge(actions, template, toys, categories, materials);
})
);
});
};