This repository has been archived by the owner on Jan 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
138 lines (120 loc) · 3.31 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
const path = require('path')
const kebabCase = require('lodash/kebabCase')
const { refreshData } = require('./src/api/refreshData')
const { getTopActive } = require('./src/api/getTopActive')
const { getTopBest } = require('./src/api/getTopBest')
const { getWorldwide } = require('./src/api/getWorldwide')
const { getAllCountries } = require('./src/api/getAllCountries')
const { createGQLNode } = require('./src/api/createGQLNode')
const { percentageChange } = require('./src/utils')
exports.onPreInit = async () => {
await refreshData()
}
exports.sourceNodes = async ({
actions,
createNodeId,
createContentDigest
}) => {
const { createNode } = actions
const context = {
createNode,
createNodeId,
createContentDigest
}
const topActive = {
label: 'activeTop20',
name: 'ActiveTop20',
payload: { ...await getTopActive() }
}
const topBest = {
label: 'bestTop20',
name: 'BestTop20',
payload: { ...await getTopBest() }
}
createGQLNode(context, topActive)
createGQLNode(context, topBest)
const worldData = await getWorldwide()
const last = worldData.stats[worldData.stats.length - 30]
const first = worldData.stats[worldData.stats.length - 1]
const monthToDate = {
label: 'monthToDate',
name: 'MonthToDate',
payload: {
monthAgo: last,
today: first,
change: {
active: percentageChange(first.active, last.active),
cases: percentageChange(first.cases, last.cases),
deaths: percentageChange(first.deaths, last.deaths),
recovered: percentageChange(first.recovered, last.recovered)
}
}
}
createGQLNode(context, monthToDate)
const allCountries = await getAllCountries()
const weekToDateChange = {
label: 'weekToDateChange',
name: 'WeekToDateChange',
payload: {
stats: allCountries.map(country => {
const range = country.stats.slice(-7)
return {
name: country.name,
change: {
...range.reduce((acc, current, idx) => idx !== 0 && ({
[idx]: {
active: current.active - acc.active,
cases: current.cases - acc.cases,
deaths: current.deaths - acc.deaths,
recovered: current.recovered - acc.recovered,
}
}))
}
}
})
}
}
createGQLNode(context, weekToDateChange)
}
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
return graphql(`
{
countries: allMongodbCovidCountries {
edges {
node {
name
mongodb_id
}
}
}
}`
).then(result => {
// Create pages for country.
result.data.countries.edges.forEach(({ node }) => {
const name = kebabCase(node.name.trim().toLowerCase())
const { mongodb_id: id } = node
createPage({
path: name,
component: path.resolve('./src/templates/page.js'),
context: { id },
})
})
}).catch(err => {
reporter.panicOnBuild(err)
})
}
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
if (stage === 'build-html') {
actions.setWebpackConfig({
module: {
rules: [
{
test: /react-apexcharts/,
use: loaders.null(),
},
],
},
})
}
}