diff --git a/src/components/BulkActions/actions/UpdateFlashpackNumbers.jsx b/src/components/BulkActions/actions/UpdateFlashpackNumbers.jsx new file mode 100644 index 0000000..120a507 --- /dev/null +++ b/src/components/BulkActions/actions/UpdateFlashpackNumbers.jsx @@ -0,0 +1,36 @@ +import { forwardRef, useImperativeHandle } from "react"; +import { useStatusContext } from "../../Contexts/StatusContext"; +import { useUserContext } from "../../Contexts/UserContext"; +import vehiclesAPI from "../../../services/vehiclesAPI"; + +export default forwardRef(({ + ids, + idCSV, +}, ref) => { + const { setMessage } = useStatusContext(); + const { token: { idToken: { jwtToken: token } } } = useUserContext(); + + useImperativeHandle(ref, () => ({ + async submit() { + return vehiclesAPI + .flashpackVersionBulkUpdate(ids, token) + .then((data) => { + if (data.error) { + setMessage(`${data.error}: ${data.message}`); + } else if (ids.length === 1) { + setMessage(`Updating flashpack number for ${ids[0]}`); + } else { + setMessage(`Updating flashpack numbers for ${ids.length} cars`); + } + }); + }, + })); + + return ( +
+

+ You are updating flashpack numbers for the following VINs: {idCSV}. +

+
+ ); +}); \ No newline at end of file diff --git a/src/components/BulkActions/actions/UpdateFlashpackNumbers.test.jsx b/src/components/BulkActions/actions/UpdateFlashpackNumbers.test.jsx new file mode 100644 index 0000000..ba89d29 --- /dev/null +++ b/src/components/BulkActions/actions/UpdateFlashpackNumbers.test.jsx @@ -0,0 +1,42 @@ +jest.mock("../../Contexts/UserContext"); +jest.mock("../../Contexts/StatusContext"); +jest.mock("../../../services/vehiclesAPI"); + +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 UpdateFlashpackNumbers from "./UpdateFlashpackNumbers"; +import vehiclesAPI from "../../../services/vehiclesAPI"; + +describe("BulkActions/UpdateFlashpackNumbers", () => { + beforeAll(() => { + setToken(TEST_AUTH_OBJECT_FISKER); + }); + + it("makes request to update flashpack numbers", async () => { + const flashpackVersionBulkUpdate = jest.spyOn(vehiclesAPI, "flashpackVersionBulkUpdate"); + const ref = React.createRef(); + + const vins = ["TESTVIN123456789a", "TESTVIN123456789b", "TESTVIN123456789c"]; + + render( + + + + + + ); + + await act(async () => ref.current.submit()); + expect(flashpackVersionBulkUpdate).toHaveBeenCalledTimes(1); + }); +}); diff --git a/src/components/BulkActions/index.jsx b/src/components/BulkActions/index.jsx index 22ce82c..1f1d62b 100644 --- a/src/components/BulkActions/index.jsx +++ b/src/components/BulkActions/index.jsx @@ -15,6 +15,7 @@ const RemoteCommand = lazy(() => import("./actions/RemoteCommand")); const SendSMS = lazy(() => import("./actions/SendSMS")); const UpdateConfig = lazy(() => import("./actions/UpdateConfig")); const UpdateFleetVehicles = lazy(() => import("./actions/UpdateFleetVehicles")); +const UpdateFlashpackNumbers = lazy(() => import("./actions/UpdateFlashpackNumbers")); export default function BulkActions({ ids = [], @@ -79,6 +80,12 @@ export default function BulkActions({ disabled: false, trigger: () => setActive("updateFleetVehicles"), }, + { + id: "updateFlashpackNumbers", + name: "Update Flashpack Numbers", + disabled: false, + trigger: () => setActive("updateFlashpackNumbers") + } ].filter((action) => actions.includes(action.id)); const payload = { @@ -127,6 +134,7 @@ export default function BulkActions({ {active === "sms" && } {active === "updateConfig" && } {active === "updateFleetVehicles" && } + {active === "updateFlashpackNumbers" && } diff --git a/src/components/Fleets/Status/Vehicles/Table/index.jsx b/src/components/Fleets/Status/Vehicles/Table/index.jsx index 9d17123..8b4c7c1 100644 --- a/src/components/Fleets/Status/Vehicles/Table/index.jsx +++ b/src/components/Fleets/Status/Vehicles/Table/index.jsx @@ -176,7 +176,7 @@ const MainForm = ({ name }) => { { return { message: "Deleted" }; }, + flashpackVersionBulkUpdate: async (data, token) => { + return { message: "Updated" } + } }; export default vehiclesAPI; diff --git a/src/services/vehiclesAPI.js b/src/services/vehiclesAPI.js index c908f27..f3deac4 100644 --- a/src/services/vehiclesAPI.js +++ b/src/services/vehiclesAPI.js @@ -340,6 +340,18 @@ const vehiclesAPI = { }).then(fetchRespHandler) .catch(errorHandler) }, + + flashpackVersionBulkUpdate: async (vins, token) => { + return fetch(`${API_ENDPOINT}/flashpack_version_bulk_update`, { + method: "PUT", + headers: Object.assign( + { "Content-Type": "application/json" }, + getAuthHeaderOptions(token), + ), + body: JSON.stringify({ vins }), + }).then(fetchRespHandler) + .catch(errorHandler) + }, }; export default vehiclesAPI;