CEC-287 Car connection status (#59)
* Car connection status * Formatting
This commit is contained in:
@@ -39,11 +39,11 @@ export const VehicleProvider = ({ children }) => {
|
||||
if (result.error) {
|
||||
setVehicles([]);
|
||||
throw new Error(`Get vehicles error. ${result.message}`);
|
||||
} else {
|
||||
setVehicles(result.data);
|
||||
if (result.total) {
|
||||
setTotalVehicles(result.total);
|
||||
}
|
||||
}
|
||||
await addConnections(result.data, token);
|
||||
setVehicles(result.data);
|
||||
if (result.total) {
|
||||
setTotalVehicles(result.total);
|
||||
}
|
||||
} finally {
|
||||
setBusy(false);
|
||||
@@ -96,6 +96,30 @@ export const VehicleProvider = ({ children }) => {
|
||||
}
|
||||
};
|
||||
|
||||
const addConnections = async (cars, token) => {
|
||||
const vins = cars.map((car) => car.vin);
|
||||
const result = await api.getConnections(vins, token);
|
||||
|
||||
if (result.error) {
|
||||
throw new Error(`Get connections error. ${result.message}`);
|
||||
}
|
||||
cars.forEach((car) => {
|
||||
car.connected = result[car.vin] || false;
|
||||
});
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<VehicleContext.Provider
|
||||
value={{
|
||||
@@ -109,6 +133,7 @@ export const VehicleProvider = ({ children }) => {
|
||||
getModels,
|
||||
getYears,
|
||||
sendCommand,
|
||||
getConnections,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
|
||||
Reference in New Issue
Block a user