From 43d1721804d4b1533a6a2d766dd8b669886f6d32 Mon Sep 17 00:00:00 2001 From: padamsen_fisker Date: Fri, 12 Jan 2024 16:36:28 -0500 Subject: [PATCH] delete working --- src/components/Contexts/VehicleContext.jsx | 29 +++++++++- src/components/Flashpack/Add/index.jsx | 0 src/components/Flashpack/index.jsx | 66 ++++++++++++++++++++-- src/components/Routes/SiteRoutes.jsx | 10 ++++ src/services/vehiclesAPI.js | 4 +- 5 files changed, 100 insertions(+), 9 deletions(-) create mode 100644 src/components/Flashpack/Add/index.jsx diff --git a/src/components/Contexts/VehicleContext.jsx b/src/components/Contexts/VehicleContext.jsx index b74d50f..c71860f 100644 --- a/src/components/Contexts/VehicleContext.jsx +++ b/src/components/Contexts/VehicleContext.jsx @@ -304,11 +304,11 @@ export const VehicleProvider = ({ children }) => { if (options && options.offset === 0 && result.total) { setTotalFlashpacks(result.total); } + + return result; } finally { setBusy(false); } - - return result; }; const getFlashpackECUMappings = async (model, year, flashpack, options, token) => { @@ -324,11 +324,33 @@ export const VehicleProvider = ({ children }) => { if (options && options.offset === 0 && result.total) { setTotalFlashpackECUMappings(result.total); } + + return result; + } finally { + setBusy(false); + } + }; + + const deleteFlashpackVersion = async (model, year, flashpack, token) => { + let result; + try { + setBusy(true); + + const data = { + "car_model": model, + "car_year": year, + "flashpack": flashpack, + } + + result = await api.deleteFlashpackVersion(data, token); + if (result.error) + throw new Error(`Delete flashpack ecu mappings error. ${result.message}`); + + return result; } finally { setBusy(false); } - return result; }; return ( @@ -366,6 +388,7 @@ export const VehicleProvider = ({ children }) => { totalFlashpackECUMappings, flashpackECUMappings, getFlashpackECUMappings, + deleteFlashpackVersion, }} > {children} diff --git a/src/components/Flashpack/Add/index.jsx b/src/components/Flashpack/Add/index.jsx new file mode 100644 index 0000000..e69de29 diff --git a/src/components/Flashpack/index.jsx b/src/components/Flashpack/index.jsx index de81b58..b0f7499 100644 --- a/src/components/Flashpack/index.jsx +++ b/src/components/Flashpack/index.jsx @@ -1,4 +1,6 @@ import { + Grid, + IconButton, Table, TableBody, TableCell, @@ -6,6 +8,9 @@ import { TablePagination, TableRow, } from "@material-ui/core"; +import AddCircleIcon from "@material-ui/icons/AddCircle"; +import DeleteIcon from "@material-ui/icons/Delete"; +import clsx from "clsx"; import { Link } from "react-router-dom"; import { logger } from "../../services/monitoring"; import React, { useEffect, useState } from "react"; @@ -14,6 +19,8 @@ import { useStatusContext } from "../Contexts/StatusContext"; import { useUserContext } from "../Contexts/UserContext"; import TableHeaderSortable from "../Table/HeaderSortable"; import { useLocalStorage } from "../useLocalStorage"; +import DeleteConfirmation from "../DeleteConfirmation"; +import { RoleWrap } from "../Controls/RoleWrap"; import useStyles from "../useStyles"; const tableColumns = [ @@ -38,17 +45,20 @@ const MainForm = () => { const { setMessage, setTitle, setSitePath } = useStatusContext(); const [pageSize, setPageSize] = useLocalStorage(PAGE_SIZE, 10); const [pageIndex, setPageIndex] = useState(0); - const [orderBy, setOrderBy] = useState("flashpack"); - const [order, setOrder] = useState("desc"); + const [showDeleteModal, setShowDeleteModal] = useState(false); + const [rowToDelete, setRowToDelete] = useState({}); const { getAllFlashpacks, flashpacks, totalFlashpacks, + deleteFlashpackVersion, } = useVehicleContext(); const { token: { idToken: { jwtToken: token }, }, + groups, + providers, } = useUserContext(); useEffect(() => { @@ -65,7 +75,7 @@ const MainForm = () => { useEffect(() => { loadFlashpacks(); // eslint-disable-next-line react-hooks/exhaustive-deps - }, [token, token, pageIndex, pageSize, orderBy, order]); + }, [token, token, pageIndex, pageSize]); const loadFlashpacks = async () => { try { @@ -74,7 +84,6 @@ const MainForm = () => { { limit: pageSize, offset: pageSize * pageIndex, - order: `${orderBy} ${order}`, }, token ); @@ -93,8 +102,35 @@ const MainForm = () => { setPageIndex(0); }; + const onDeleteClick = (row) => { + setRowToDelete(row); + setShowDeleteModal(true); + } + + const sendDelete = async () => { + if (rowToDelete) { + try { + const result = await deleteFlashpackVersion(rowToDelete.car_model, rowToDelete.car_year, rowToDelete.flashpack, token); + if (!result || result.error) return; + + setMessage(`Deleted ${rowToDelete.car_year} ${rowToDelete.car_model} ${rowToDelete.flashpack}`); + loadFlashpacks(); + } catch (e) { + setMessage(e.message); + logger.warn(e.stack); + } + } + }; + return (
+ + + + + + + { {row.car_year} + + + onDeleteClick(row)} + aria-label={`Send delete for ${row.car_year} ${row.car_model} ${row.flashpack}`} + size="small" + color="primary" + > + + + + ))} @@ -138,6 +190,12 @@ const MainForm = () => {
+ setShowDeleteModal(false)} + deleteFunction={sendDelete} + />
); }; diff --git a/src/components/Routes/SiteRoutes.jsx b/src/components/Routes/SiteRoutes.jsx index 57575e3..aac7613 100644 --- a/src/components/Routes/SiteRoutes.jsx +++ b/src/components/Routes/SiteRoutes.jsx @@ -42,6 +42,7 @@ const SuppliersList = React.lazy(() => import("../Suppliers/List")); const SupplierDetails = React.lazy(() => import("../Suppliers/Details")); const Flashpacks = React.lazy(() => import("../Flashpack")); const FlashpackDetails = React.lazy(() => import("../Flashpack/Details")); +const FlashpackAdd = React.lazy(() => import("../Flashpack/Add")) const Datascope = React.lazy(() => import("../Dashboard")); const SumsRxSwin = React.lazy(() => import("../SUMS")); const SumsRxSwinAdd = React.lazy(() => import("../SUMS/Add")); @@ -300,6 +301,15 @@ const SiteRoutes = () => { rolesPerGroup={Permissions.FiskerRead} providers={providers} /> + } + type={TYPES.PROTECTED} + token={token} + groups={groups} + rolesPerGroup={Permissions.FiskerCreate} + providers={providers} + /> } diff --git a/src/services/vehiclesAPI.js b/src/services/vehiclesAPI.js index 1e45fcb..6280d14 100644 --- a/src/services/vehiclesAPI.js +++ b/src/services/vehiclesAPI.js @@ -295,8 +295,8 @@ const vehiclesAPI = { .catch(errorHandler) }, - deleteFlashpackECUMapping: async (data, token) => { - return fetch(`${API_ENDPOINT}/flashpack_ecu_mapping`, { + deleteFlashpackVersion: async (data, token) => { + return fetch(`${API_ENDPOINT}/flashpack_version`, { method: "DELETE", headers: Object.assign( { "Content-Type": "application/json" },