-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecreationGovAvailability.js
427 lines (367 loc) · 16.1 KB
/
recreationGovAvailability.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
Recreation.gov Campsite Availability
Fetches availabilty for a given campground and months, aggregates it and displays all availability dates in
those months.
Paste this code into the console after navigating to recreation.gov to build the app.
Sample availability endpoint (with start date specifier):
https://www.recreation.gov/api/camps/availability/campground/232487/month?start_date=2020-08-01T00%3A00%3A00.000Z
Sample campground endpoint:
https://www.recreation.gov/api/camps/campgrounds/250005
Sample campsites endpoint:
https://www.recreation.gov/api/camps/campsites/4522
Sample campground website link:
https://www.recreation.gov/camping/campgrounds/250005
*/
const fetchCampsite = (campsite) => fetch(`https://www.recreation.gov/api/camps/campsites/${campsite}`);
const fetchAvailability = (campground, year, monthNum) => fetch(`https://www.recreation.gov/api/camps/availability/campground/${campground}/month?start_date=${String(year).padStart(4,'20')}-${String(monthNum).padStart(2,'0')}-01T00%3A00%3A00.000Z`);
const fetchCampground = (campground) => fetch(`https://www.recreation.gov/api/camps/campgrounds/${campground}`);
const MONTHS_MAP = {
1: "January",
2: "February",
3: "March",
4: "April",
5: "May",
6: "June",
7: "July",
8: "August",
9: "September",
10: "October",
11: "November",
12: "December"
};
const DEFAULT_HEADER_BOX_STYLE_MIXIN = "border: 1px solid black; border-radius: 8px; text-align: center; background-color: #333; color: white;";
const DEFAULT_TITLE_BOX_STYLE = `${DEFAULT_HEADER_BOX_STYLE_MIXIN} padding: 6px;`;
const DEFAULT_H2_BOX_STYLE = `${DEFAULT_HEADER_BOX_STYLE_MIXIN} padding: 3px; margin: 10px 0px;`;
let DEFAULT_CAMPGROUND = 232487;
function setTitle(campgroundID) {
if (campgroundID) {
document.title = `Availability: ${campgroundID}`;
} else {
document.title = `Rec.gov Availability`;
}
}
function campgroundWebsiteLink(campgroundID) {
return `https://www.recreation.gov/camping/campgrounds/${campgroundID}`;
}
function createCampsitesAvailabilityTables(availabilityData) {
const RESERVED = "Reserved";
const AVAILABLE = "Available";
const campsitesAvailability = availabilityData.campsites;
// MAP: {
// [site name (e.g. 'A10')]: [availability table for that site]
// }
const campsiteSiteAvailabilityMap = Object.keys(campsitesAvailability).reduce((agg, campsiteID) => {
const csa = campsitesAvailability[campsiteID];
const { site, availabilities } = csa;
const availDates = Object.keys(availabilities).reduce((dates, dt) => {
const isAvail = availabilities[dt] === AVAILABLE;
if (isAvail) {
dates.push((new Date(dt)));
}
return dates;
}, []);
if (availDates.length > 0) {
const availabilityTbl = createAvailabilityTable(site, availDates);
agg[site] = availabilityTbl;
}
return agg;
}, {});
const availabilityTables = Object.keys(campsiteSiteAvailabilityMap).sort().map(site => {
const availabilityTbl = campsiteSiteAvailabilityMap[site];
return availabilityTbl;
});
return availabilityTables;
}
function createAvailabilityTable(site, availDates) {
// console.log(`Creating Table for ${site}`);
const tbl = document.createElement('table');
const thd = document.createElement('thead');
const thdRow = document.createElement('tr');
const thdCell = document.createElement('th');
thdCell.innerText = site;
thdRow.appendChild(thdCell);
thd.appendChild(thdRow);
tbl.appendChild(thd);
const tbod = document.createElement('tbody');
tbl.appendChild(tbod);
availDates.forEach(availDate => {
const dateString = availDate.toLocaleDateString();
const row = document.createElement('tr');
const cell = document.createElement('td');
cell.innerText = dateString;
row.appendChild(cell)
tbod.appendChild(row);
});
return tbl;
}
function fetchAvailabilityandAggregate(campgroundID, availabilityYear, availabilityMonths, availabilityCallback) {
Promise.all(availabilityMonths.map(availMonth => fetchAvailability(campgroundID, availabilityYear, availMonth)))
.then(responses => {
console.log(`Availability fetched`);
Promise.all(responses.map(resp => resp.json()))
.then(monthlyAvailabilities => {
const aggregatedAvailability = monthlyAvailabilities.reduce((agg, avail) => {
Object.keys(avail.campsites).forEach(campsiteID => {
const cs = avail.campsites[campsiteID];
const currentAvailabilities = agg.campsites[campsiteID] ? agg.campsites[campsiteID].availabilities : {};
agg.campsites[campsiteID] = {
...agg.campsites[campsiteID],
...cs,
availabilities: {
...currentAvailabilities,
...cs.availabilities
}
}
});
return agg;
}, { campsites: {} });
availabilityCallback(aggregatedAvailability);
})
})
.catch(err => console.log(err.message));
}
function setUpUI(defaultCampground) {
document.body.innerText = "";
// Setting global styles
const STYLE_RULES = `
* {
font-family: "Helvetica", Arial, sans-serif;
}
a {
color: #5af;
}
button {
background-color: #333;
color: white;
border-radius: 3px;
border-color: black;
padding: 6px;
font-size: 1em;
}
button:hover {
background-color: #666;
}
button:active {
color: #999;
}
input {
border: 1px solid #333;
border-radius: 3px;
margin: 6px 0px;
padding: 3px;
}
label {
margin: 6px 0px;
}
table {
border: 2px solid black;
margin: 5px;
display: inline;
}
thead {}
tbody {
border: 1px solid black;
}
th {
font-weight: bold;
}
th, td {
text-align: center;
vertical-align: middle;
}
`;
const style = document.createElement("style");
style.type = "text/css";
if (style.styleSheet) {
style.styleSheet.cssText = STYLE_RULES;
} else {
style.appendChild(document.createTextNode(STYLE_RULES));
}
document.getElementsByTagName("head")[0].appendChild(style);
// Creating title box
const titleBox = document.createElement('div');
titleBox.setAttribute("style", DEFAULT_TITLE_BOX_STYLE);
const titleElement = document.createElement('h1');
titleElement.setAttribute("style", "text-align: center; margin: 0 auto;");
const subTitleElement = document.createElement('h4');
const anchorTagHtml = `<a href="https://www.recreation.gov/search?inventory_type=camping" target="_blank">Recreation.gov</a>`;
subTitleElement.innerHTML = `Use this tool to see all available dates for selected months for a given campground ID from ${anchorTagHtml}`;
titleBox.appendChild(titleElement);
titleBox.appendChild(subTitleElement);
document.body.appendChild(titleBox);
setTitle();
titleElement.innerText = "Recreation.gov Campsite Availability";
const availabilityTablesSection = document.createElement('div');
const availabilityTablesSectionHeaderBox = document.createElement('div');
availabilityTablesSectionHeaderBox.setAttribute("style", DEFAULT_H2_BOX_STYLE);
const availabilityTablesSectionHeader = document.createElement("h2");
const availabilityTablesSectionAddress = document.createElement("h4");
const availabilityTablesSectionLink = document.createElement("a");
const availabilityTablesSectionEmail = document.createElement("h5");
const availabilityTablesSectionPhone = document.createElement("h5");
availabilityTablesSectionHeaderBox.appendChild(availabilityTablesSectionHeader);
availabilityTablesSectionHeaderBox.appendChild(availabilityTablesSectionLink);
availabilityTablesSectionHeaderBox.appendChild(availabilityTablesSectionAddress);
availabilityTablesSectionHeaderBox.appendChild(availabilityTablesSectionEmail);
availabilityTablesSectionHeaderBox.appendChild(availabilityTablesSectionPhone);
const availabilityTablesSectionData = document.createElement('div');
function availabilityCallback(aggregatedAvailability) {
const tables = createCampsitesAvailabilityTables(aggregatedAvailability);
if (tables.length > 0) {
availabilityTablesSectionData.innerText = "";
tables.forEach(tbl => {
availabilityTablesSection.appendChild(tbl);
});
} else {
availabilityTablesSectionData.innerText = "No availability found for these dates :(";
}
}
function selectionCallback(selectionData) {
const { campground, year, months } = selectionData;
availabilityTablesSectionLink.innerText = "";
availabilityTablesSectionAddress.innerText = "";
availabilityTablesSectionEmail.innerText = "";
availabilityTablesSectionPhone.innerText = "";
fetchCampground(campground)
.then(resp => resp.json())
.then(({ campground: campgroundData}) => {
if (!campgroundData) {
availabilityTablesSectionHeader.innerText = `CAMPGROUND ${campground} NOT FOUND`;
return;
}
const {
facility_name,
addresses,
facility_email: email,
facility_phone: phone
} = campgroundData;
availabilityTablesSectionHeader.innerText = `Availability: ${facility_name}`;
setTitle(facility_name);
availabilityTablesSectionLink.href = campgroundWebsiteLink(campground);
availabilityTablesSectionLink.innerText = 'Recreation.gov Page';
availabilityTablesSectionLink.target = "_blank";
if (addresses && addresses.length > 0) {
const { address1, city, state_code, postal_code } = addresses[0];
availabilityTablesSectionAddress.innerText = `${address1}, ${city}, ${state_code} ${postal_code}`;
}
if (email && email.length > 0) {
availabilityTablesSectionEmail.innerText = `Email: ${email}`;
}
if (phone && phone.length > 0) {
availabilityTablesSectionPhone.innerText = `Phone: ${phone}`;
}
})
.catch(err => console.log(err.message))
.catch(err => console.log(err.message));
setTitle(campground);
availabilityTablesSection.innerText = "";
availabilityTablesSection.appendChild(availabilityTablesSectionHeaderBox);
availabilityTablesSection.appendChild(availabilityTablesSectionData);
availabilityTablesSectionHeader.innerText = `Availability: Campground ${campground}`;
availabilityTablesSectionData.innerText = "Loading ...";
fetchAvailabilityandAggregate(campground, year, months, availabilityCallback);
}
const userInputSection = createUserInputSection(defaultCampground, selectionCallback);
document.body.appendChild(userInputSection);
document.body.appendChild(availabilityTablesSection);
}
function createUserInputSection(defaultCampground, selectionCallback) {
// selectionCallback takes object with: { year, campground, months }
const DEFAULT_BOX_MARGIN = "6px 0px";
// Default availability year set to current year
const defaultAvailabilityYear = (new Date()).getFullYear();
const campgroundInputBox = document.createElement('div');
campgroundInputBox.id = "campground-input-box";
campgroundInputBox.style.margin = DEFAULT_BOX_MARGIN;
campgroundInputBox.style.padding = "5px";
const campgroundLabel = document.createElement('label');
campgroundLabel.innerText = "Campground ID";
campgroundLabel.style.fontWeight = "bold";
const campgroundInput = document.createElement('input');
campgroundInput.value = defaultCampground;
campgroundInput.required = true;
campgroundInput.style.display = "block";
campgroundInputBox.appendChild(campgroundLabel);
campgroundInputBox.appendChild(campgroundInput);
const yearInputBox = document.createElement('div');
yearInputBox.id = "year-input-box";
yearInputBox.style.margin = DEFAULT_BOX_MARGIN;
yearInputBox.style.padding = "5px";
const yearLabel = document.createElement('label');
yearLabel.innerText = "Year";
yearLabel.style.fontWeight = "bold";
const yearInput = document.createElement('input');
yearInput.type = "number";
yearInput.required = true;
yearInput.value = defaultAvailabilityYear;
yearInput.style.display = "block";
yearInputBox.appendChild(yearLabel);
yearInputBox.appendChild(yearInput);
let selectedMonths = [];
function clickCheckBox(e) {
const { value, checked } = e.target;
const selectedMonthsSet = new Set(selectedMonths);
if (checked) {
selectedMonthsSet.add(value);
} else {
selectedMonthsSet.delete(value);
}
selectedMonths = Array.from(selectedMonthsSet);
console.log(`Selected Months: ${JSON.stringify(selectedMonths)}`);
}
const monthsBox = document.createElement('div');
monthsBox.id = "months-box";
monthsBox.style.margin = DEFAULT_BOX_MARGIN;
monthsBox.style.padding = "5px";
const monthsLabel = document.createElement('label');
monthsLabel.style.fontWeight = "bold";
monthsLabel.innerText = "Months";
const monthsRadioButtons = Object.keys(MONTHS_MAP).map(month => {
const monthName = MONTHS_MAP[month];
const cid = `checkbox-${monthName}`;
const d = document.createElement('div');
const checkBox = document.createElement('input');
checkBox.type = "checkbox";
checkBox.value = String(month);
checkBox.id = cid;
checkBox.name = cid;
checkBox.onclick = clickCheckBox;
const label = document.createElement('label');
label.for=cid;
label.innerText = monthName;
label.style.margin = "auto 6px";
d.appendChild(checkBox);
d.appendChild(label);
return d;
});
monthsBox.appendChild(monthsLabel);
monthsRadioButtons.forEach(mrb => monthsBox.appendChild(mrb));
function submit(e) {
e.preventDefault();
const campground = campgroundInput.value;
const availabilityYear = yearInput.value;
if (!(campground && campground.length > 0)) {
return alert(`Need to give a campground ID`);
}
if (!(selectedMonths.length > 0)) {
return alert(`Need to select at least one month for availability`);
}
selectionCallback({ campground, year: availabilityYear, months: selectedMonths });
}
const submitButton = document.createElement('button');
submitButton.type = "button";
submitButton.onclick = submit;
submitButton.innerText = "Submit";
const userInputSection = document.createElement('div');
const header = document.createElement('h2');
header.innerText = "Select Campground Availability";
document.createElement('div')
userInputSection.appendChild(header);
userInputSection.appendChild(campgroundInputBox);
userInputSection.appendChild(yearInputBox);
userInputSection.appendChild(monthsBox);
userInputSection.appendChild(submitButton);
return userInputSection;
}
// MAIN
setUpUI(DEFAULT_CAMPGROUND);