-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_bill_data.js
61 lines (54 loc) · 1.88 KB
/
load_bill_data.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
'use strict';
var page = 'http://prochoicetexas.org/?page_id=3782&preview=true';
var productionPage = 'http://prochoicetexas.org/bill-tracker-2/';
var apiKey = 'AIzaSyARsKyuI7u_mrrHf5_VVW2PAXVJgbl9L5U';
var spreadsheetId = '1fO-vfsIkNuUnfFFiKtnp0pydHmsaLTFt1VgSvguDiAs';
var range = 'A:I';
var baseUrl = 'https://sheets.googleapis.com/v4/spreadsheets/';
function callUrl(sheetId, range) {
return baseUrl + sheetId + '/values/' + range + '/?key=' + apiKey;
}
function mapRowToJson(headerRow, row) {
var shapedRow = {};
for (var i = 0; i < headerRow.length; i++) {
shapedRow[headerRow[i]] = row[i];
}
return shapedRow;
}
function mapToCard(bill) {
// console.log(bill);
return `<div class="card ${bill['Urgent'] ? 'urgent' : ''}">
<div class="row text-center top">
${bill['Bill ID']}
</div>
<div class="row middle text-center">
<div class="col-sm-12">Description: ${bill['Title'].substring(1,100)}...</div>
</div>
<div class="row bottom">
<div class="col-sm-12">
${bill['Last Status']} at ${bill['Last Updated']}
</div>
</div>
</div>`;
}
jQuery(document).ready(function($) {
if (window.location.href === page || window.location.href === productionPage) {
var url = callUrl(spreadsheetId, range);
jQuery.ajax({
url: url,
success: function(data) {
// Turn it all into JSON
var headerRow = data.values[0];
var jsonResult = data.values.slice(1).map(function(row) {
return mapRowToJson(headerRow, row);
});
// Clear out the div and replace it with results of sheet
jQuery('.bill-tracker-content').empty();
jsonResult.forEach(function(bill) {
// Do something with bill
jQuery('.bill-tracker-content').append(mapToCard(bill));
});
}
});
}
});