-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
189 lines (155 loc) · 6.61 KB
/
Copy pathscripts.js
File metadata and controls
189 lines (155 loc) · 6.61 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Function to create a card
function createCard(area, vehicleData, driverData) {
const card = document.createElement("div");
card.classList.add("card");
// First section: Area Name
const idSection = document.createElement("div");
idSection.classList.add("card-section");
const idText = document.createElement("div");
idText.classList.add("card-id"); // Add this class for identifying area name
idText.textContent = `Area: ${area.name}`; // Display area name
idSection.appendChild(idText);
card.appendChild(idSection);
// Second section: Select boxes
const dropdownSection = document.createElement("div");
dropdownSection.classList.add("card-section");
const dropdownContainer = document.createElement("div");
dropdownContainer.classList.add("dropdown-container");
// Create the first dropdown and populate it with vehicle IDs
const dropdown1 = createDropdown("Vehicle", vehicleData.map(vehicle => vehicle.id));
dropdown1.classList.add("vehicle-dropdown"); // Add this class for identifying vehicle dropdown
dropdownContainer.appendChild(dropdown1);
// Create the second dropdown and populate it with driver names
const dropdown2 = createDropdown("Driver", driverData.map(driver => driver.name));
dropdown2.classList.add("driver-dropdown"); // Add this class for identifying driver dropdown
dropdownContainer.appendChild(dropdown2);
dropdownSection.appendChild(dropdownContainer);
card.appendChild(dropdownSection);
// Third section: Submit button
const submitSection = document.createElement("div");
submitSection.classList.add("card-section");
const cardSubmitButton = document.createElement("button");
cardSubmitButton.textContent = "Submit";
cardSubmitButton.classList.add("submit-btn");
cardSubmitButton.disabled = true; // Disable the button by default
// Attach a click event listener to the submit button
cardSubmitButton.addEventListener("click", () => {
// Implement the logic to send data to the assigned-work API here
const requestData = {
areaId: area.areaId || area.name, // Use areaId if available, otherwise use name
vehicleId: dropdown1.value,
driverId: getDriverIdByName(driverData, dropdown2.value),
};
fetch("https://garbage-collect-backend.onrender.com/assign-work", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestData),
})
.then(response => response.json())
.then(data => {
console.log("Response from assigned-work API:", data);
})
.catch(error => console.error("Error submitting data:", error));
// Disable selected options on other cards
disableSelectedOptions(area.areaId || area.name, dropdown1.value, dropdown2.value);
// Disable submit button and dropdowns for the clicked card
cardSubmitButton.disabled = true;
dropdown1.disabled = true;
dropdown2.disabled = true;
// Change text and style for the clicked card's submit button
cardSubmitButton.textContent = "Submitted";
cardSubmitButton.style.backgroundColor = "white";
cardSubmitButton.style.color = "green";
});
submitSection.appendChild(cardSubmitButton);
card.appendChild(submitSection);
// Event listener for dropdown1
dropdown1.addEventListener("change", () => {
updateSubmitButtonStatus();
});
// Event listener for dropdown2
dropdown2.addEventListener("change", () => {
updateSubmitButtonStatus();
});
// Function to update the submit button status
function updateSubmitButtonStatus() {
cardSubmitButton.disabled = !(dropdown1.value && dropdown2.value);
}
return card;
}
// Function to create a dropdown
function createDropdown(label, options) {
const dropdown = document.createElement("select");
const labelElement = document.createElement("label");
labelElement.textContent = label;
labelElement.setAttribute("for", label.toLowerCase().replace(/\s/g, "-"));
dropdown.id = label.toLowerCase().replace(/\s/g, "-");
// Add an initial blank option
const blankOption = document.createElement("option");
blankOption.value = ""; // Set value as appropriate
blankOption.textContent = ""; // Set text content as appropriate
dropdown.appendChild(blankOption);
options.forEach(option => {
const optionElement = document.createElement("option");
optionElement.value = option;
optionElement.textContent = option;
dropdown.appendChild(optionElement);
});
return dropdown;
}
// Function to get driver ID by name
function getDriverIdByName(driverData, driverName) {
const selectedDriver = driverData.find(driver => driver.name === driverName);
return selectedDriver ? selectedDriver.driverId : '';
}
// Function to disable selected options on other cards
function disableSelectedOptions(area, selectedVehicle, selectedDriver) {
const cards = document.querySelectorAll('.card');
cards.forEach(card => {
// Skip the card that was just submitted
if (card.querySelector('.card-id').textContent === `Area: ${area}`) {
return;
}
const dropdown1 = card.querySelector('.vehicle-dropdown');
const dropdown2 = card.querySelector('.driver-dropdown');
// Disable selected vehicle and driver options on other cards
disableOption(dropdown1, selectedVehicle);
disableOption(dropdown2, selectedDriver);
});
}
// Function to disable a specific option in a dropdown
function disableOption(dropdown, optionValue) {
const options = dropdown.options;
for (let i = 0; i < options.length; i++) {
if (options[i].value === optionValue) {
options[i].disabled = true;
break;
}
}
}
// Fetch driver data from the API
fetch("https://garbage-collect-backend.onrender.com/get-driver")
.then(response => response.json())
.then(driverData => {
// Fetch vehicle data from the API
fetch("https://garbage-collect-backend.onrender.com/get-vehicle")
.then(response => response.json())
.then(vehicleData => {
// Fetch area data from the API
fetch("https://garbage-collect-backend.onrender.com/get-all-areas")
.then(response => response.json())
.then(areaData => {
const mainSection = document.getElementById("main-section");
// Iterate through the area data and create cards
areaData.forEach(area => {
const card = createCard(area, vehicleData, driverData);
mainSection.appendChild(card);
});
})
.catch(error => console.error("Error fetching area data:", error));
})
.catch(error => console.error("Error fetching vehicle data:", error));
})
.catch(error => console.error("Error fetching driver data:", error));