CEC-371 Update car ECUs display (#78)

* Clean up className styles
Update car status page to show update and ECUs

* Add update ecu version button
Show all ECUs on car status page
Only show car ecus for search
This commit is contained in:
John Wu
2021-08-18 09:14:13 -07:00
committed by GitHub
parent 3e66959521
commit d1815e2ff9
20 changed files with 1169 additions and 299 deletions

View File

@@ -33,21 +33,19 @@ export const VehicleProvider = ({ children }) => {
const [models, setModels] = useState([]);
const [years, setYears] = useState([]);
const getVehicles = async (search, token) => {
const addConnections = async (cars, token) => {
try {
setBusy(true);
const result = await api.getVehicles(search, token);
const vins = cars.map((car) => car.vin);
const result = await api.getConnections(vins, token);
if (result.error) {
setVehicles([]);
throw new Error(`Get vehicles error. ${result.message}`);
throw new Error(`Add connections error. ${result.message}`);
}
await addConnections(result.data, token);
setVehicles(result.data);
if (result.total) {
setTotalVehicles(result.total);
}
} finally {
setBusy(false);
cars.forEach((car) => {
car.connected = result[car.vin] || false;
});
} catch (e) {
logger.error(e.stack);
}
};
@@ -63,6 +61,41 @@ export const VehicleProvider = ({ children }) => {
}
};
const getConnections = async (vins, token) => {
try {
setBusy(true);
const result = await api.getConnections(vins, token);
if (result.error)
throw new Error(`Get connections error. ${result.message}`);
return result;
} finally {
setBusy(false);
}
};
const getECUs = async (search, token) => {
try {
setBusy(true);
const result = await api.getECUs(search, token);
if (result.error) throw new Error(`Get ECUs error. ${result.message}`);
return result;
} finally {
setBusy(false);
}
};
const getLocations = async (token) => {
try {
setBusy(true);
const result = await api.getLocations(token);
if (result.error)
throw new Error(`Get locations error. ${result.message}`);
return result;
} finally {
setBusy(false);
}
};
const getModels = async (token) => {
try {
setBusy(true);
@@ -74,6 +107,35 @@ export const VehicleProvider = ({ children }) => {
}
};
const getState = async (token, vin) => {
try {
setBusy(true);
const result = await api.getState(token, vin);
if (result.error) throw new Error(`Get state error. ${result.message}`);
return result;
} finally {
setBusy(false);
}
};
const getVehicles = async (search, token) => {
try {
setBusy(true);
const result = await api.getVehicles(search, token);
if (result.error) {
setVehicles([]);
throw new Error(`Get vehicles error. ${result.message}`);
}
await addConnections(result.data, token);
setVehicles(result.data);
if (result.total) {
setTotalVehicles(result.total);
}
} finally {
setBusy(false);
}
};
const getYears = async (token) => {
try {
setBusy(true);
@@ -97,74 +159,23 @@ export const VehicleProvider = ({ children }) => {
}
};
const addConnections = async (cars, token) => {
try {
const vins = cars.map((car) => car.vin);
const result = await api.getConnections(vins, token);
if (result.error) {
throw new Error(`Add connections error. ${result.message}`);
}
cars.forEach((car) => {
car.connected = result[car.vin] || false;
});
} catch (e) {
logger.error(e.stack);
}
};
const getConnections = async (vins, token) => {
try {
setBusy(true);
const result = await api.getConnections(vins, token);
if (result.error)
throw new Error(`Get connections error. ${result.message}`);
return result;
} finally {
setBusy(false);
}
};
const getLocations = async (token) => {
try {
setBusy(true);
const result = await api.getLocations(token);
if (result.error)
throw new Error(`Get locations error. ${result.message}`);
return result;
} finally {
setBusy(false);
}
};
const getState = async (token, vin) => {
try {
setBusy(true);
const result = await api.getState(token, vin);
if (result.error)
throw new Error(`Get state error. ${result.message}`);
return result;
} finally {
setBusy(false);
}
};
return (
<VehicleContext.Provider
value={{
busy,
vehicles,
totalVehicles,
models,
years,
getVehicles,
totalVehicles,
addVehicle,
getModels,
getYears,
sendCommand,
getConnections,
getECUs,
getLocations,
getModels,
getState,
getYears,
getVehicles,
sendCommand,
}}
>
{children}