-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-parking-place.js
54 lines (50 loc) · 1.41 KB
/
remove-parking-place.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
import {centerCoords} from "./center.js";
import {createAndAttachMap, createMarker} from "./map.js";
let map = createAndAttachMap(centerCoords.longitude, centerCoords.latitude);
// render all the parkings that are already avaiable
const idInput = document.querySelector("#id");
const fetchPromise = fetch("/parkings/all");
const markers = {};
fetchPromise.then(response => {
return response.json();
}).then(parkings => {
for (let p of parkings) {
const marker = createMarker(
p,
true,
"This parking place will be removed",
() => {
idInput.value = p.ID;
},
() => {
idInput.value = "";
},
);
marker.addTo(map);
markers[p.ID] = marker;
}
})
// handle form submission
const form = document.querySelector("#remove-parking-place-form");
form.addEventListener("submit", async (e) => {
e.preventDefault();
fetch(`/parkings/`, {
method: "DELETE",
header: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
ID: Number(idInput.value),
}),
})
.catch(() => alert("failed to remove the parking"))
.then((result) => {
return result.json();
})
.then(parking => {
try {
markers[parking.ID].remove();
delete markers[parking.ID]
} catch {}
});
});