CEC-5432: expand bulk-action to add, remove, and move vehicles between fleets (#484)

* battery indicator

* optimistic response remove from fleet

* update api to use params

* expand add to fleet to also remove

* typo

* update to post request

* CEC-5432: remove unused deps

* update mocks for delete
This commit is contained in:
Tristan Timblin
2023-11-17 10:44:56 -08:00
committed by GitHub
parent 94ba59ea77
commit f4652b5de7
15 changed files with 239 additions and 189 deletions

View File

@@ -239,26 +239,39 @@ export const FleetProvider = ({ children }) => {
}
};
const deleteFleetVehicle = async (name, vehicle, token) => {
const deleteFleetVehicles = async (name, vins, token) => {
if (!Array.isArray(vins)) {
throw new Error(`VINs are required`);
}
try {
setBusy(true);
validateFleetName(name);
validateVIN(vehicle.vin);
for (let i = 0; i < vins.length; i++) {
validateVIN(vins[0]);
}
const result = await api.deleteFleetVehicle(name, vehicle, token);
if (result.error) {
const result = await api.deleteFleetVehicles(name, vins, token);
if (result?.error) {
throw new Error(`Delete fleet vehicle error. ${result.message}`);
}
const index = fleetVehicles.findIndex(element => element === vehicle.vin);
if (index >= 0) fleetVehicles.splice(index, 1);
return result;
removeFleetVehiclesLocal(vins);
} finally {
setBusy(false);
}
};
const removeFleetVehiclesLocal = (vins = []) => {
setFleetVehicles((fleetVehicles) => {
return fleetVehicles.filter((element) => !vins.includes(element.vin));
});
setTotalFleetVehicles((totalFleetVehicles) => {
return totalFleetVehicles - vins.length;
});
}
const getFleetCANFilters = async (name, search, token) => {
try {
setBusy(true);
@@ -358,7 +371,8 @@ export const FleetProvider = ({ children }) => {
watchFleetVehicles,
getFleetVehicles,
addFleetVehicles,
deleteFleetVehicle,
deleteFleetVehicles,
removeFleetVehiclesLocal,
fleetCANFilters,
totalFleetCANFilters,