forked from Ayoona22/WebWeavers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
front.js
68 lines (62 loc) · 2.25 KB
/
front.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
// Get the carpooling form and the myRequests and myRides lists
const form = document.getElementById('carpoolingForm');
const myRequestsList = document.getElementById('myRequests');
const myRidesList = document.getElementById('myRides');
// Add a submit event listener to the carpooling form
form.addEventListener('submit', (e) => {
e.preventDefault();
// Get the form data
const location = document.getElementById('location').value;
const destination = document.getElementById('destination').value;
const date = document.getElementById('date').value;
const time = document.getElementById('time').value;
// Send a POST request to the Django backend with the carpooling request details
fetch('/api/carpooling/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ location, destination, date, time })
})
.then(response => response.json())
.then(data => {
// Display a success message to the user
alert('Carpooling request submitted successfully!');
// Add the new carpooling request to the myRequests list
const li = document.createElement('li');
li.innerHTML = `
<strong>${data.location}</strong> to <strong>${data.destination}</strong> on ${data.date} at ${data.time}
`;
myRequestsList.appendChild(li);
})
.catch(error => {
// Display an error message to the user
alert('Error submitting carpooling request.');
});
});
// Add a click event listener to the myRequests list
myRequestsList.addEventListener('click', (e) => {
if (e.target.tagName === 'LI') {
// Send a DELETE request to the Django backend to delete the selected carpooling request
fetch(`/api/carpooling/${e.target.dataset.id}/`, {
method: 'DELETE'
})
.then(() => {
// Remove the selected carpooling request from the myRequests list
e.target.remove();
});
}
});
// Add a click event listener to the myRides list
myRidesList.addEventListener('click', (e) => {
if (e.target.tagName === 'LI') {
// Send a DELETE request to the Django backend to delete the selected ride
fetch(`/api/rides/${e.target.dataset.id}/`, {
method: 'DELETE'
})
.then(() => {
// Remove the selected ride from the myRides list
e.target.remove();
});
}
});