CEC-4640: add bulk add to fleet (#384)
* refactor bulkactions component * refactor bulk actions * update dom tests * add addToFleet hook * make signal optional * implement code splitting * add deps * remove test label
This commit is contained in:
74
src/components/BulkActions/actions/UpdateConfig.jsx
Normal file
74
src/components/BulkActions/actions/UpdateConfig.jsx
Normal file
@@ -0,0 +1,74 @@
|
||||
import { useState, forwardRef, useImperativeHandle } from "react";
|
||||
import {
|
||||
Checkbox,
|
||||
FormControlLabel,
|
||||
} from '@material-ui/core';
|
||||
import { useStatusContext } from "../../Contexts/StatusContext";
|
||||
import { useUserContext } from "../../Contexts/UserContext";
|
||||
import TaskRunner from "../../../utils/taskRunner";
|
||||
import vehiclesAPI from "../../../services/vehiclesAPI";
|
||||
|
||||
export default forwardRef(({
|
||||
vins,
|
||||
vinCSV,
|
||||
}, ref) => {
|
||||
const { setMessage } = useStatusContext();
|
||||
const { token: { idToken: { jwtToken: token } } } = useUserContext();
|
||||
|
||||
const [forcePush, setForcePush] = useState(false);
|
||||
|
||||
useImperativeHandle(ref, () => ({
|
||||
async submit() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const taskRunner = new TaskRunner(5, vins.length);
|
||||
let errorCount = 0;
|
||||
|
||||
const task = (vin, index) => {
|
||||
const progressMessage = `${index + 1}/${vins.length}`;
|
||||
return async () => vehiclesAPI.updateConfig(vin, forcePush, token)
|
||||
.then((response) => {
|
||||
if (response.error) {
|
||||
errorCount += 1;
|
||||
setMessage(`${progressMessage} ${response.error}: ${response.message}`);
|
||||
} else {
|
||||
setMessage(`${progressMessage} Updated config for ${vin}`);
|
||||
}
|
||||
return response;
|
||||
})
|
||||
.catch((error) => reject(error));
|
||||
}
|
||||
|
||||
vins.forEach((vin, i) => {
|
||||
taskRunner.push(task(vin, i));
|
||||
});
|
||||
|
||||
taskRunner.onComplete().then((responses) => {
|
||||
const completeMessage = `${vins.length - errorCount}/${vins.length}`;
|
||||
setMessage(`Successfully updated ${completeMessage} vehicles.`);
|
||||
resolve(responses);
|
||||
});
|
||||
});
|
||||
},
|
||||
}));
|
||||
|
||||
const handleChange = () => {
|
||||
setForcePush((forcePush) => !forcePush);
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<p>
|
||||
You are updating the config for the following VINs: {vinCSV}.
|
||||
</p>
|
||||
<FormControlLabel
|
||||
label="Force Push"
|
||||
control={
|
||||
<Checkbox
|
||||
checked={forcePush}
|
||||
onChange={() => handleChange()}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user