-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgatsby-node.js
208 lines (190 loc) · 7.3 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require('path')
const { createOpenGraphImage } = require('gatsby-plugin-open-graph-images')
const createPageDataStruct = require('./gatsbyNodeHelper/index')
const houseLegislators = require('./src/data/house_legislators.json')
const senateLegislators = require('./src/data/senate_legislators.json')
const legislationData = require('./src/data/legislation.json')
const { getLegislatorUrlParams, isHouseRep, isSenator } = require('./src/utilities')
const makePage = ({ chamber, pageData, createPage, legislatorId }) => {
const ogImageFilename = (
getLegislatorUrlParams(pageData.legislator) +
'-' +
pageData.legislator.district
)
.toLowerCase()
.replace(/ /g, '-')
.replace(/[.,']/g, '')
// The next two lines remove diacritics, which createOpenGraphImage can't handle
// Source: stackoverflow.com/questions/990904
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
const context = {
id: legislatorId,
chamber,
pageData,
ogImage: createOpenGraphImage(createPage, {
path: `og-images/legislator/${ogImageFilename}.png`,
component: path.resolve(`src/components/legislator/ogImage.js`),
waitCondition: 'networkidle0',
size: {
width: 630,
height: 315,
},
context: {
chamber,
pageData,
},
}),
}
createPage({
path: `/legislator/${getLegislatorUrlParams(pageData.legislator)}`,
component: require.resolve(`./src/components/legislator/index.js`),
context,
})
}
//TODO - other sessions
const sponsorshipsSessionNumber = 194
const votesSessionOrdinal = '193rd'
const sponsorshipsSessionYear = 2025
const votesSessionYear = 2023
// create individual legislator pages
exports.createPages = async function ({ actions, graphql }) {
const { createPage } = actions
// sponsorships
const getLegislatorById = (id) => {
const legislator =
houseLegislators.find((legislator) => legislator.id === id) ??
senateLegislators?.find((legislator) => legislator.id === id)
if (legislator === undefined) {
throw new Error(
`A legislator with ID ${id} is included in the sponsorships table, but that ID was not found among the ` +
`current legislators provided by Open States. Possibly a typo in the ID, or a retired legislator.`
)
}
const votes =
legislationData[votesSessionYear]?.houseVotes?.find((vote) => vote.id === id) ??
legislationData[votesSessionYear]?.senateVotes?.find((vote) => vote.id === id)
return {
...legislator,
...votes,
}
}
const sponsoredBills = Object.entries(legislationData[sponsorshipsSessionYear].sponsoredBills)
const sponsoredBillTemplate = path.resolve(`./src/components/sponsorships/sponsorships.js`)
const sponsors = legislationData[sponsorshipsSessionYear].sponsorship.map((sponsorshipData) => {
const legislatorData = getLegislatorById(sponsorshipData.id)
return {
sponsorshipData: { ...sponsorshipData.data, ...sponsorshipData.score },
...legislatorData,
}
})
const validateBillStatuses = (billData) => {
const validStatusPairs = [
['Passed', 'Passed'],
['Not Yet', 'Not Yet'],
['Passed', 'Not Yet'],
['Not Yet', 'Passed'],
['In Conference Committee', 'In Conference Committee'],
['Enacted', 'Enacted'],
]
if (!validStatusPairs.some(([houseStatus, senateStatus]) => {
return houseStatus === billData.houseStatus && senateStatus === billData.senateStatus
})) {
throw new Error(
`Invalid statuses for bill ${billData.houseBillNumber} / ${billData.senateBillNumber}: ` +
`house status "${billData.houseStatus}", senate status "${billData.senateStatus}"`)
}
}
const billNamesToConsolidatedBillsMap = new Map()
sponsoredBills.forEach((sponsoredBill) => {
const [billNumber, billData] = sponsoredBill
const name = billData.shorthand_title.toLowerCase().trim()
const chamber = Array.from(billNumber)[0] == 'H' ? 'house' : 'senate'
if (billNamesToConsolidatedBillsMap.has(name)) {
// Found a paired bill
const pairedBill = billNamesToConsolidatedBillsMap.get(name)
pairedBill[`${chamber}BillNumber`] = billNumber
pairedBill[`${chamber}Status`] = billData.status
pairedBill[`${chamber}LeadSponsors`] = billData.sponsors.split('&').map(sponsor => sponsor.trim())
billData['houseBillNumber'] = pairedBill['houseBillNumber']
billData['senateBillNumber'] = pairedBill['senateBillNumber']
billData['houseStatus'] = pairedBill['houseStatus']
billData['senateStatus'] = pairedBill['senateStatus']
billData['senateLeadSponsors'] = pairedBill['senateLeadSponsors']
billData['houseLeadSponsors'] = pairedBill['houseLeadSponsors']
billData.sponsors = billData.senateLeadSponsors.concat(billData.houseLeadSponsors).join(', ')
validateBillStatuses(pairedBill)
} else {
// First occurrence of this name
billData[`${chamber}BillNumber`] = billNumber
billData[`${chamber}Status`] = billData.status
billData[`${chamber}LeadSponsors`] = billData.sponsors.split('&').map(sponsor => sponsor.trim())
billNamesToConsolidatedBillsMap.set(name, billData)
}
})
const consolidatedBills = Array.from(billNamesToConsolidatedBillsMap.values());
consolidatedBills.forEach((billData) => {
let otherBillNames = ''
const sortedSponsors = sponsors
.filter((sponsor) => {
if (!sponsor.id) return false
for (const bills in sponsor.sponsorshipData) {
if (bills.includes(billData.bill_number) && sponsor.sponsorshipData[bills]) {
otherBillNames = bills
return true
}
}
return false
})
.map((sponsor) => {
return {
...sponsor,
name: [sponsor.familyName, sponsor.givenName].join(', '),
}
})
const createPageData = {
component: sponsoredBillTemplate,
context: {
billData: { ...billData, otherBillNames },
sponsors: sortedSponsors,
houseSponsors: sortedSponsors.filter(isHouseRep),
senateSponsors: sortedSponsors.filter(isSenator),
votesSessionOrdinal,
sponsorshipsSessionNumber,
},
}
if (billData.houseBillNumber !== undefined) {
const createPageHouseBillData = { ...createPageData, path: `/sponsorships/${billData.houseBillNumber}` }
createPage(createPageHouseBillData)
}
if (billData.senateBillNumber !== undefined) {
const createPageSenateBillData = { ...createPageData, path: `/sponsorships/${billData.senateBillNumber}` }
createPage(createPageSenateBillData)
}
})
const allSponsoredBillsTemplate = path.resolve(`./src/components/sponsorships/index.js`)
createPage({
path: `/sponsorships/all-bills`,
component: allSponsoredBillsTemplate,
context: {
consolidatedBills,
},
})
let legislatorsList = [
{ chamber: 'senate', legislators: senateLegislators },
{ chamber: 'house', legislators: houseLegislators },
].map(({ chamber, legislators }) => {
legislators.forEach(({ id: legislatorId }) => {
const pageData = createPageDataStruct({
chamber,
legislatorId,
})
makePage({ chamber, pageData, createPage, legislatorId })
})
})
}