-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
151 lines (143 loc) · 4.13 KB
/
gatsby-node.js
File metadata and controls
151 lines (143 loc) · 4.13 KB
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
const path = require("path")
const { pageNameByLocation } = require("./node-src/helpers")
const {
homeQueryFields,
businessesQuery,
categoryQuery,
} = require("./node-src/queries")
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
// Get all businesses
const businessesPromise = graphql(businessesQuery).then(result => {
const businesses = result.data.comopedir.businesses.edges || []
// Create pages for each business
businesses.forEach(({ node = {} }) => {
if (node.slug) {
createPage({
path: node.slug,
component: path.resolve("./src/templates/Restaurant/index.js"),
context: {
data: node,
},
})
}
})
return businesses
})
// Get all categories
const categoriesPromise = graphql(categoryQuery).then(result =>
(result.data.comopedir.categories.edges || []).map(({ node }) => node)
)
const [businesses, categories] = await Promise.all([
businessesPromise,
categoriesPromise,
])
// Get all locations from the businesses
const locations = businesses.reduce((accum, { node = {} }) => {
const { addresses } = node
addresses.forEach((address = {}) => {
const { city, state } = address
if (!accum[state]) {
accum[state] = [city]
} else if (!accum[state].includes(city)) {
accum[state].push(city)
}
})
return accum
}, {})
// Get businesses for every category in every city in every state.
const businessFilteredPromise = Promise.all(
Object.keys(locations).map(state =>
Promise.all(
locations[state].map(city =>
Promise.all([
// Create root page for each city and state with all categories.
graphql(`
query {
comopedir {
businesses(city: "${city}", state: "${state}") {
${homeQueryFields}
}
}
}
`).then(result => {
const businessesFiltered =
(result.data.comopedir.businesses &&
result.data.comopedir.businesses.edges) ||
[]
return createPage(
getListingPageParams({
state,
city,
businesses: businessesFiltered,
locations,
categories,
})
)
}),
Promise.all(
categories.map(category =>
graphql(`
query {
comopedir {
businesses(city: "${city}", state: "${state}", category: "${category.id}") {
${homeQueryFields}
}
}
}
`).then(result => {
const businessesFiltered =
(result.data.comopedir.businesses &&
result.data.comopedir.businesses.edges) ||
[]
return createPage(
getListingPageParams({
state,
city,
category: category.slug,
businesses: businessesFiltered,
locations,
categories,
})
)
})
)
),
])
)
)
)
)
return Promise.all([
businessFilteredPromise,
createPage({
path: "/",
component: path.resolve("./src/templates/Listing/index.js"),
context: {
businesses: businesses.map(i => i.node),
locations,
categories: categories.map(i => i.slug),
},
}),
])
}
// Generate page params for createPage for HomePage
const getListingPageParams = ({
state,
city,
category = "",
businesses,
locations,
categories,
}) => ({
path: `${pageNameByLocation(state, city)}/${category}`,
component: path.resolve("./src/templates/Listing/index.js"),
context: {
businesses: businesses.map(i => i.node),
locations,
categories: categories.map(i => i.slug),
selectedCity: city,
selectedState: state,
selectedCategory: category,
},
})