Skip to content

Commit

Permalink
Merge pull request #100 from Pooja3Bhattrai/Bhattrai
Browse files Browse the repository at this point in the history
Updated to show existing driver
  • Loading branch information
mansiruhil13 authored Oct 3, 2024
2 parents e7b0996 + 8b0c0b8 commit 73be175
Showing 1 changed file with 58 additions and 29 deletions.
87 changes: 58 additions & 29 deletions manage.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,39 +151,68 @@ <h2 id="ambulanceFormTitle">Add Ambulance</h2>
});

// Handle Driver Form Submission
driverForm.addEventListener('submit', (e) => {
e.preventDefault();
const driverName = document.getElementById('driverName').value.trim();
const driverStatus = document.getElementById('driverStatusSelect').value;
const driverContact = document.getElementById('driverContact').value;
const driverLocation = document.getElementById('driverLocation').value.trim();
const driverIndex = document.getElementById('driverIndex').value;
// Handle Driver Form Submission
driverForm.addEventListener('submit', (e) => {
e.preventDefault();
const driverName = document.getElementById('driverName').value.trim();
const driverStatus = document.getElementById('driverStatusSelect').value;
const driverContact = document.getElementById('driverContact').value;
const driverLocation = document.getElementById('driverLocation').value.trim();
const driverIndex = document.getElementById('driverIndex').value;

let drivers = JSON.parse(localStorage.getItem('drivers')) || [];
let drivers = JSON.parse(localStorage.getItem('drivers')) || [];

if (driverIndex === '') {
// Add New Driver
drivers.push({
name: driverName,
status: driverStatus,
contact: driverContact,
location: driverLocation
});
alert(`Driver ${driverName} has been added.`);
} else {
// Edit Existing Driver
drivers[driverIndex] = {
name: driverName,
status: driverStatus,
contact: driverContact,
location: driverLocation
};
alert(`Driver ${driverName} has been updated.`);
}
// Function to check if driver already exists
const driverExists = (name, contact, location) => {
return drivers.some(driver =>
driver.name.toLowerCase() === name.toLowerCase() &&
driver.contact === contact &&
driver.location.toLowerCase() === location.toLowerCase()
);
};

localStorage.setItem('drivers', JSON.stringify(drivers));
hideSection(manageDriverSection);
if (driverIndex === '') {
// Check for duplicate when adding a new driver
if (driverExists(driverName, driverContact, driverLocation)) {
alert(`A driver with the name "${driverName}", contact "${driverContact}", and location "${driverLocation}" already exists.`);
return; // Exit the function without adding the driver
}
// Add New Driver
drivers.push({
name: driverName,
status: driverStatus,
contact: driverContact,
location: driverLocation
});
alert(`Driver ${driverName} has been added.`);
} else {
// Edit Existing Driver
const originalDriver = drivers[driverIndex];
if (
originalDriver.name !== driverName ||
originalDriver.contact !== driverContact ||
originalDriver.location !== driverLocation
) {
// Check for duplicate only if name, contact, or location has changed
if (driverExists(driverName, driverContact, driverLocation)) {
alert(`A driver with the name "${driverName}", contact "${driverContact}", and location "${driverLocation}" already exists.`);
return; // Exit the function without updating the driver
}
}
drivers[driverIndex] = {
name: driverName,
status: driverStatus,
contact: driverContact,
location: driverLocation
};
alert(`Driver ${driverName} has been updated.`);
}

localStorage.setItem('drivers', JSON.stringify(drivers));
hideSection(manageDriverSection);
});



// Handle Ambulance Form Submission
ambulanceForm.addEventListener('submit', (e) => {
Expand Down

0 comments on commit 73be175

Please sign in to comment.