-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
131 lines (118 loc) · 4.18 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
const Promise = require('bluebird')
const path = require('path')
exports.createPages = ({ graphql, actions }) => {
const { createPage, createRedirect } = actions
return new Promise((resolve, reject) => {
resolve(
graphql(
`
{
allContentfulTeaReviewPost {
edges {
node {
teaName
teaSource
teaType
slug
}
}
}
allContentfulTeaBlogPost {
edges {
node {
title
slug
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
// redirect
createRedirect({ fromPath: 'tea-reviews/1', toPath: 'tea-reviews', isPermanent: true });
const teaTypes = ['Black Tea', 'Green Tea', 'Oolong Tea', 'Pu-Erh', 'White Tea', 'Herbal Tea']
const teaFilterSlugs = ['black-tea','green-tea','oolong-tea','pu-erh','white-tea','herbal-tea']
const posts = result.data.allContentfulTeaReviewPost.edges
const postsPerPage = 6
const numPages = Math.ceil(posts.length / postsPerPage)
// pagination for specific tea types
for (let i = 0; i < 6; i++ ) {
const curTeaType = teaTypes[i];
const curSlug = teaFilterSlugs[i];
const filteredPosts = posts.filter((e) => e.node.teaType === curTeaType);
const numOfPages = Math.ceil(filteredPosts.length / postsPerPage);
Array.from({ length: numOfPages }).forEach((_, i) => {
createPage({
path: i === 0 ? `/tea-reviews/${curSlug}` : `/tea-reviews/${curSlug}/${i + 1}`,
component: path.resolve("./src/templates/teaReviewList.js"),
context: {
limit: postsPerPage,
skip: i * postsPerPage,
numPages: numOfPages,
filter: [curTeaType],
currentPage: i + 1,
},
})
})
}
// pagination for list of all tea reviews
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path: i === 0 ? `/tea-reviews` : `/tea-reviews/${i + 1}`,
component: path.resolve("./src/templates/teaReviewList.js"),
context: {
limit: postsPerPage,
skip: i * postsPerPage,
filter: teaTypes,
numPages,
currentPage: i + 1,
},
})
})
// page for each tea review
posts.forEach((post, index) => {
createPage({
path: `/tea-reviews/${post.node.slug}/`,
component: path.resolve('./src/templates/teaReviewPost.js'),
context: {
slug: post.node.slug,
prevSlug: index === 0 ? null : posts[index - 1].node.slug,
nextSlug: index === (posts.length - 1) ? null : posts[index + 1].node.slug
},
})
})
const blogPosts = result.data.allContentfulTeaBlogPost.edges
const numBlogPages = Math.ceil(blogPosts.length / postsPerPage)
// page for each blog post
blogPosts.forEach((post, index) => {
createPage({
path: `/blog/${post.node.slug}/`,
component: path.resolve('./src/templates/teaBlogPost.js'),
context: {
slug: post.node.slug,
prevSlug: index === 0 ? null : posts[index - 1].node.slug,
nextSlug: index === (posts.length - 1) ? null : posts[index + 1].node.slug
},
})
})
// pagination for blog post
Array.from({ length: numBlogPages }).forEach((_, i) => {
createPage({
path: i === 0 ? `/blog` : `/blog/${i + 1}`,
component: path.resolve("./src/templates/teaBlogList.js"),
context: {
limit: postsPerPage,
skip: i * postsPerPage,
numPages: numBlogPages,
currentPage: i + 1,
},
})
})
})
)
})
}