CEC-4920: Add redeploy bulk-action (#420)
* standardize bulk actions * add redploy bulk-action * add cases to disable redeploy * update status check * rename func
This commit is contained in:
@@ -5,8 +5,8 @@ import TextInputList from "../../Controls/TextInputList";
|
||||
import vehiclesAPI from "../../../services/vehiclesAPI";
|
||||
|
||||
export default forwardRef(({
|
||||
vins,
|
||||
vinCSV,
|
||||
ids,
|
||||
idCSV,
|
||||
}, ref) => {
|
||||
const { setMessage } = useStatusContext();
|
||||
const { token: { idToken: { jwtToken: token } } } = useUserContext();
|
||||
@@ -16,7 +16,7 @@ export default forwardRef(({
|
||||
useImperativeHandle(ref, () => ({
|
||||
async submit() {
|
||||
return vehiclesAPI
|
||||
.addTags(vins, tags, token)
|
||||
.addTags(ids, tags, token)
|
||||
.then((data) => {
|
||||
if (data.error) {
|
||||
setMessage(`${data.error}: ${data.message}`);
|
||||
@@ -32,7 +32,7 @@ export default forwardRef(({
|
||||
return (
|
||||
<div>
|
||||
<p>
|
||||
You are adding tags to the following VINs: {vinCSV}.
|
||||
You are adding tags to the following VINs: {idCSV}.
|
||||
</p>
|
||||
<TextInputList
|
||||
label="Tags"
|
||||
|
||||
@@ -38,8 +38,8 @@ describe("BulkActions/AddTags", () => {
|
||||
<UserProvider>
|
||||
<AddTags
|
||||
ref={ref}
|
||||
vins={["TESTVIN123456789a"]}
|
||||
vinCSV=""
|
||||
ids={["TESTVIN123456789a"]}
|
||||
idCSV=""
|
||||
/>
|
||||
</UserProvider>
|
||||
</StatusProvider>
|
||||
|
||||
@@ -40,8 +40,8 @@ describe("BulkActions/AddToFleet", () => {
|
||||
<UserProvider>
|
||||
<AddToFleet
|
||||
ref={ref}
|
||||
vins={["TESTVIN1234567890"]}
|
||||
vinCSV=""
|
||||
ids={["TESTVIN1234567890"]}
|
||||
idCSV=""
|
||||
/>
|
||||
</UserProvider>
|
||||
</StatusProvider>
|
||||
|
||||
55
src/components/BulkActions/actions/Cancel.jsx
Normal file
55
src/components/BulkActions/actions/Cancel.jsx
Normal file
@@ -0,0 +1,55 @@
|
||||
import { forwardRef, useImperativeHandle } from "react";
|
||||
import { useStatusContext } from "../../Contexts/StatusContext";
|
||||
import { useUserContext } from "../../Contexts/UserContext";
|
||||
import TaskRunner from "../../../utils/taskRunner";
|
||||
import updatesAPI from "../../../services/updatesAPI";
|
||||
|
||||
export default forwardRef(({
|
||||
ids,
|
||||
idCSV,
|
||||
}, ref) => {
|
||||
const { setMessage } = useStatusContext();
|
||||
const { token: { idToken: { jwtToken: token } } } = useUserContext();
|
||||
|
||||
useImperativeHandle(ref, () => ({
|
||||
async submit() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const taskRunner = new TaskRunner(5, ids.length);
|
||||
let errorCount = 0;
|
||||
|
||||
const task = (id, index) => {
|
||||
const progressMessage = `${index + 1}/${ids.length}`;
|
||||
return async () => updatesAPI.cancelCarUpdate(id, token)
|
||||
.then((response) => {
|
||||
if (response.error) {
|
||||
errorCount += 1;
|
||||
setMessage(`${progressMessage} ${response.error}: ${response.message}`);
|
||||
} else {
|
||||
setMessage(`${progressMessage} Canceled update ${id}`);
|
||||
}
|
||||
return response;
|
||||
})
|
||||
.catch((error) => reject(error));
|
||||
}
|
||||
|
||||
ids.forEach((id, i) => {
|
||||
taskRunner.push(task(id, i));
|
||||
});
|
||||
|
||||
taskRunner.onComplete().then((responses) => {
|
||||
const completeMessage = `${ids.length - errorCount}/${ids.length}`;
|
||||
setMessage(`Successfully canceled ${completeMessage} updates.`);
|
||||
resolve(responses);
|
||||
});
|
||||
});
|
||||
},
|
||||
}));
|
||||
|
||||
return (
|
||||
<div>
|
||||
<p>
|
||||
You are canceling the following updates: {idCSV}.
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
40
src/components/BulkActions/actions/Cancel.test.jsx
Normal file
40
src/components/BulkActions/actions/Cancel.test.jsx
Normal file
@@ -0,0 +1,40 @@
|
||||
jest.mock("../../Contexts/UserContext");
|
||||
jest.mock("../../Contexts/StatusContext");
|
||||
jest.mock("../../../services/updatesAPI");
|
||||
|
||||
import React from "react";
|
||||
import {
|
||||
render,
|
||||
act,
|
||||
} from "@testing-library/react";
|
||||
import { UserProvider, setToken } from "../../Contexts/UserContext";
|
||||
import { StatusProvider } from "../../Contexts/StatusContext";
|
||||
import { TEST_AUTH_OBJECT_FISKER } from "../../../utils/testing";
|
||||
import Cancel from "./Cancel";
|
||||
import updatesAPI from "../../../services/updatesAPI";
|
||||
|
||||
describe("Manifest/Cancel", () => {
|
||||
beforeAll(() => {
|
||||
setToken(TEST_AUTH_OBJECT_FISKER);
|
||||
});
|
||||
|
||||
it("makes request to cancel an update", async () => {
|
||||
const api = jest.spyOn(updatesAPI, "cancelCarUpdate");
|
||||
const ref = React.createRef();
|
||||
|
||||
render(
|
||||
<StatusProvider>
|
||||
<UserProvider>
|
||||
<Cancel
|
||||
ref={ref}
|
||||
ids={["1", "2", "3"]}
|
||||
idCSV=""
|
||||
/>
|
||||
</UserProvider>
|
||||
</StatusProvider>
|
||||
);
|
||||
|
||||
await act(async () => ref.current.submit());
|
||||
expect(api).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
});
|
||||
@@ -5,8 +5,8 @@ import TaskRunner from "../../../utils/taskRunner";
|
||||
import vehiclesAPI from "../../../services/vehiclesAPI";
|
||||
|
||||
export default forwardRef(({
|
||||
vins,
|
||||
vinCSV,
|
||||
ids,
|
||||
idCSV,
|
||||
}, ref) => {
|
||||
const { setMessage } = useStatusContext();
|
||||
const { token: { idToken: { jwtToken: token } } } = useUserContext();
|
||||
@@ -14,11 +14,11 @@ export default forwardRef(({
|
||||
useImperativeHandle(ref, () => ({
|
||||
async submit() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const taskRunner = new TaskRunner(5, vins.length);
|
||||
const taskRunner = new TaskRunner(5, ids.length);
|
||||
let errorCount = 0;
|
||||
|
||||
const task = (vin, index) => {
|
||||
const progressMessage = `${index + 1}/${vins.length}`;
|
||||
const progressMessage = `${index + 1}/${ids.length}`;
|
||||
return async () => vehiclesAPI.deleteVehicle(vin, token)
|
||||
.then((response) => {
|
||||
if (response.error) {
|
||||
@@ -32,12 +32,12 @@ export default forwardRef(({
|
||||
.catch((error) => reject(error));
|
||||
}
|
||||
|
||||
vins.forEach((vin, i) => {
|
||||
ids.forEach((vin, i) => {
|
||||
taskRunner.push(task(vin, i));
|
||||
});
|
||||
|
||||
taskRunner.onComplete().then((responses) => {
|
||||
const completeMessage = `${vins.length - errorCount}/${vins.length}`;
|
||||
const completeMessage = `${ids.length - errorCount}/${ids.length}`;
|
||||
setMessage(`Successfully deleted ${completeMessage} vehicles.`);
|
||||
resolve(responses);
|
||||
});
|
||||
@@ -48,7 +48,7 @@ export default forwardRef(({
|
||||
return (
|
||||
<div>
|
||||
<p>
|
||||
You are about to delete the following VINs: {vinCSV}.
|
||||
You are about to delete the following VINs: {idCSV}.
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -27,8 +27,8 @@ describe("BulkActions/DeleteVehicles", () => {
|
||||
<UserProvider>
|
||||
<DeleteVehicles
|
||||
ref={ref}
|
||||
vins={["TESTVIN123456789a", "TESTVIN123456789b", "TESTVIN123456789c"]}
|
||||
vinCSV=""
|
||||
ids={["TESTVIN123456789a", "TESTVIN123456789b", "TESTVIN123456789c"]}
|
||||
idCSV=""
|
||||
/>
|
||||
</UserProvider>
|
||||
</StatusProvider>
|
||||
|
||||
55
src/components/BulkActions/actions/Redeploy.jsx
Normal file
55
src/components/BulkActions/actions/Redeploy.jsx
Normal file
@@ -0,0 +1,55 @@
|
||||
import { forwardRef, useImperativeHandle } from "react";
|
||||
import { useStatusContext } from "../../Contexts/StatusContext";
|
||||
import { useUserContext } from "../../Contexts/UserContext";
|
||||
import TaskRunner from "../../../utils/taskRunner";
|
||||
import updatesAPI from "../../../services/updatesAPI";
|
||||
|
||||
export default forwardRef(({
|
||||
ids,
|
||||
idCSV,
|
||||
}, ref) => {
|
||||
const { setMessage } = useStatusContext();
|
||||
const { token: { idToken: { jwtToken: token } } } = useUserContext();
|
||||
|
||||
useImperativeHandle(ref, () => ({
|
||||
async submit() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const taskRunner = new TaskRunner(5, ids.length);
|
||||
let errorCount = 0;
|
||||
|
||||
const task = (id, index) => {
|
||||
const progressMessage = `${index + 1}/${ids.length}`;
|
||||
return async () => updatesAPI.deployCarUpdate(id, token)
|
||||
.then((response) => {
|
||||
if (response.error) {
|
||||
errorCount += 1;
|
||||
setMessage(`${progressMessage} ${response.error}: ${response.message}`);
|
||||
} else {
|
||||
setMessage(`${progressMessage} Redeployed update ${id}`);
|
||||
}
|
||||
return response;
|
||||
})
|
||||
.catch((error) => reject(error));
|
||||
}
|
||||
|
||||
ids.forEach((id, i) => {
|
||||
taskRunner.push(task(id, i));
|
||||
});
|
||||
|
||||
taskRunner.onComplete().then((responses) => {
|
||||
const completeMessage = `${ids.length - errorCount}/${ids.length}`;
|
||||
setMessage(`Successfully rededployed ${completeMessage} updates.`);
|
||||
resolve(responses);
|
||||
});
|
||||
});
|
||||
},
|
||||
}));
|
||||
|
||||
return (
|
||||
<div>
|
||||
<p>
|
||||
You are redeploying the following updates: {idCSV}.
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
40
src/components/BulkActions/actions/Redeploy.test.jsx
Normal file
40
src/components/BulkActions/actions/Redeploy.test.jsx
Normal file
@@ -0,0 +1,40 @@
|
||||
jest.mock("../../Contexts/UserContext");
|
||||
jest.mock("../../Contexts/StatusContext");
|
||||
jest.mock("../../../services/updatesAPI");
|
||||
|
||||
import React from "react";
|
||||
import {
|
||||
render,
|
||||
act,
|
||||
} from "@testing-library/react";
|
||||
import { UserProvider, setToken } from "../../Contexts/UserContext";
|
||||
import { StatusProvider } from "../../Contexts/StatusContext";
|
||||
import { TEST_AUTH_OBJECT_FISKER } from "../../../utils/testing";
|
||||
import Redeploy from "./Redeploy";
|
||||
import updatesAPI from "../../../services/updatesAPI";
|
||||
|
||||
describe("Manifest/Redeploy", () => {
|
||||
beforeAll(() => {
|
||||
setToken(TEST_AUTH_OBJECT_FISKER);
|
||||
});
|
||||
|
||||
it("makes request to redeploy an update", async () => {
|
||||
const api = jest.spyOn(updatesAPI, "deployCarUpdate");
|
||||
const ref = React.createRef();
|
||||
|
||||
render(
|
||||
<StatusProvider>
|
||||
<UserProvider>
|
||||
<Redeploy
|
||||
ref={ref}
|
||||
ids={["1", "2", "3"]}
|
||||
idCSV=""
|
||||
/>
|
||||
</UserProvider>
|
||||
</StatusProvider>
|
||||
);
|
||||
|
||||
await act(async () => ref.current.submit());
|
||||
expect(api).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
});
|
||||
@@ -12,8 +12,8 @@ import smsAPI from "../../../services/smsAPI";
|
||||
import { SMS } from "../../Contexts/SMSContext";
|
||||
|
||||
export default forwardRef(({
|
||||
vins,
|
||||
vinCSV,
|
||||
ids,
|
||||
idCSV,
|
||||
}, ref) => {
|
||||
const [shouldAwait, setShouldAwait] = useState(false);
|
||||
const [smsMessage, setSmsMessage] = useState("");
|
||||
@@ -23,9 +23,9 @@ export default forwardRef(({
|
||||
useImperativeHandle(ref, () => ({
|
||||
async submit() {
|
||||
return new Promise((resolve) => {
|
||||
const taskRunner = new TaskRunner(5, vins.length);
|
||||
const taskRunner = new TaskRunner(5, ids.length);
|
||||
|
||||
vins.forEach((vin) => {
|
||||
ids.forEach((vin) => {
|
||||
taskRunner.push(async () => {
|
||||
|
||||
const { iccid } = await vehiclesAPI.getVehicle(vin, token);
|
||||
@@ -47,7 +47,7 @@ export default forwardRef(({
|
||||
|
||||
taskRunner.onComplete()
|
||||
.then((responses) => {
|
||||
setMessage(`Sent ${vins.length} SMS messages`);
|
||||
setMessage(`Sent ${ids.length} SMS messages`);
|
||||
resolve(responses);
|
||||
});
|
||||
});
|
||||
@@ -57,7 +57,7 @@ export default forwardRef(({
|
||||
return (
|
||||
<div>
|
||||
<p>
|
||||
You are about to send SMS to the following vins: {vinCSV}.
|
||||
You are about to send SMS to the following vins: {idCSV}.
|
||||
</p>
|
||||
<TextField
|
||||
fullWidth
|
||||
|
||||
@@ -53,8 +53,8 @@ describe("BulkActions/SendSMS", () => {
|
||||
<UserProvider>
|
||||
<SendSMS
|
||||
ref={ref}
|
||||
vins={["3C4PDCBG0ET127145"]}
|
||||
vinCSV=""
|
||||
ids={["3C4PDCBG0ET127145"]}
|
||||
idCSV=""
|
||||
/>
|
||||
</UserProvider>
|
||||
</StatusProvider>
|
||||
|
||||
@@ -9,8 +9,8 @@ import TaskRunner from "../../../utils/taskRunner";
|
||||
import vehiclesAPI from "../../../services/vehiclesAPI";
|
||||
|
||||
export default forwardRef(({
|
||||
vins,
|
||||
vinCSV,
|
||||
ids,
|
||||
idCSV,
|
||||
}, ref) => {
|
||||
const { setMessage } = useStatusContext();
|
||||
const { token: { idToken: { jwtToken: token } } } = useUserContext();
|
||||
@@ -20,11 +20,11 @@ export default forwardRef(({
|
||||
useImperativeHandle(ref, () => ({
|
||||
async submit() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const taskRunner = new TaskRunner(5, vins.length);
|
||||
const taskRunner = new TaskRunner(5, ids.length);
|
||||
let errorCount = 0;
|
||||
|
||||
const task = (vin, index) => {
|
||||
const progressMessage = `${index + 1}/${vins.length}`;
|
||||
const progressMessage = `${index + 1}/${ids.length}`;
|
||||
return async () => vehiclesAPI.updateConfig(vin, forcePush, token)
|
||||
.then((response) => {
|
||||
if (response.error) {
|
||||
@@ -38,12 +38,12 @@ export default forwardRef(({
|
||||
.catch((error) => reject(error));
|
||||
}
|
||||
|
||||
vins.forEach((vin, i) => {
|
||||
ids.forEach((vin, i) => {
|
||||
taskRunner.push(task(vin, i));
|
||||
});
|
||||
|
||||
taskRunner.onComplete().then((responses) => {
|
||||
const completeMessage = `${vins.length - errorCount}/${vins.length}`;
|
||||
const completeMessage = `${ids.length - errorCount}/${ids.length}`;
|
||||
setMessage(`Successfully updated ${completeMessage} vehicles.`);
|
||||
resolve(responses);
|
||||
});
|
||||
@@ -58,7 +58,7 @@ export default forwardRef(({
|
||||
return (
|
||||
<div>
|
||||
<p>
|
||||
You are updating the config for the following VINs: {vinCSV}.
|
||||
You are updating the config for the following VINs: {idCSV}.
|
||||
</p>
|
||||
<FormControlLabel
|
||||
label="Force Push"
|
||||
|
||||
@@ -27,8 +27,8 @@ describe("BulkActions/UpdateConfig", () => {
|
||||
<UserProvider>
|
||||
<UpdateConfig
|
||||
ref={ref}
|
||||
vins={["TESTVIN123456789a", "TESTVIN123456789b", "TESTVIN123456789c"]}
|
||||
vinCSV=""
|
||||
ids={["TESTVIN123456789a", "TESTVIN123456789b", "TESTVIN123456789c"]}
|
||||
idCSV=""
|
||||
/>
|
||||
</UserProvider>
|
||||
</StatusProvider>
|
||||
|
||||
Reference in New Issue
Block a user