-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
77 lines (64 loc) · 2.4 KB
/
app.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
import express from 'express';
import { AVAILABLE_LOCATIONS } from './data/available-locations.js';
import renderLocationsPage from './views/index.js';
import renderLocation from './views/components/location.js';
const app = express();
const INTERESTING_LOCATIONS = [];
app.use(express.static('public'));
app.use(express.urlencoded({ extended: false }));
// Receives GET request and renders locations page
// with updated lists of available and interesting locations
app.get('/', (req, res) => {
// An array containing all locations NOT in INTERESTING_LOCATIONS
const availableLocations = AVAILABLE_LOCATIONS.filter(
location => !INTERESTING_LOCATIONS.includes(location)
);
res.send(renderLocationsPage(availableLocations, INTERESTING_LOCATIONS));
});
// Receives POST request with locationId and
// pushes it to INTERESTING_LOCATIONS
app.post('/places', (req, res) => {
const locationId = req.body.locationId;
// Returns location with matching id
const location = AVAILABLE_LOCATIONS.find(loc => loc.id === locationId);
INTERESTING_LOCATIONS.push(location);
const availableLocations = AVAILABLE_LOCATIONS.filter(
location => !INTERESTING_LOCATIONS.includes(location)
);
// Returns location item, which HTMX will target to the "My Dream Locations" section
res.send(`
${renderLocation(location, false)}
<ul
id="available-locations"
class="locations"
hx-swap-oob="true">
${availableLocations.map(location => renderLocation(location)).join('')}
</ul>
`);
});
// Receives DELETE request with locationId and
// removes it from INTERESTING_LOCATIONS
app.delete('/places/:id', (req, res) => {
const locationId = req.params.id;
const locationIndex = INTERESTING_LOCATIONS.findIndex(
loc => loc.id === locationId
);
// Removes location from INTERESTING_LOCATIONS
INTERESTING_LOCATIONS.splice(locationIndex, 1);
// Updates available locations after removing above location
// from INTERESTING_LOCATIONS
const availableLocations = AVAILABLE_LOCATIONS.filter(
location => !INTERESTING_LOCATIONS.includes(location)
);
// Note that hx-swap-oob means the button that sent the DELETE
// request is still receiving an empty response.
res.send(`
<ul
id="available-locations"
class="locations"
hx-swap-oob="true">
${availableLocations.map(location => renderLocation(location)).join('')}
</ul>
`);
});
app.listen(3000);