-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-cars.js
77 lines (64 loc) · 2.33 KB
/
add-cars.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
const carForm = document.getElementById('car-form');
const availableCarsDiv = document.getElementById('available-cars');
// Fetch cars from the JSON server
async function fetchCars() {
const response = await fetch('http://localhost:3000/add-cars');
const data = await response.json();
displayCars(data);
}
// Display cars in the UI
function displayCars(cars) {
availableCarsDiv.innerHTML = ''; // Clear existing cars
cars.forEach(car => {
const carItem = document.createElement('div');
carItem.className = 'car-item';
carItem.innerHTML = `
<img src="${car.image}" alt="${car.make} ${car.model}">
<h3>${car.make} ${car.model} (${car.year})</h3>
<p>Price: $${car.price} per day</p>
<button class="delete-button" data-id="${car.id}">Delete</button>
`;
availableCarsDiv.appendChild(carItem);
});
// Add event listeners to delete buttons
const deleteButtons = document.querySelectorAll('.delete-button');
deleteButtons.forEach(button => {
button.addEventListener('click', async () => {
const carId = button.getAttribute('data-id');
await deleteCar(carId);
});
});
}
// Handle car form submission
carForm.addEventListener('submit', async (event) => {
event.preventDefault(); // Prevent form submission
const make = document.getElementById('make').value;
const model = document.getElementById('model').value;
const year = document.getElementById('year').value;
const price = document.getElementById('price').value;
const image = document.getElementById('image').value;
const newCar = { make, model, year: Number(year), price: Number(price), image };
// Send POST request to add a new car
await fetch('http://localhost:3000/add-cars', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(newCar)
});
// Reset the form
carForm.reset();
// Fetch the updated car list
fetchCars();
});
// Function to delete a car
async function deleteCar(carId) {
// Send DELETE request to remove the car
await fetch(`http://localhost:3000/add-cars/${carId}`, {
method: 'DELETE'
});
// Fetch the updated car list
fetchCars();
}
// Fetch cars on page load
fetchCars();