CEC-5443 - Ability to add Flashpack/ECU mappings
This commit is contained in:
154
src/components/Flashpack/index.jsx
Normal file
154
src/components/Flashpack/index.jsx
Normal file
@@ -0,0 +1,154 @@
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableFooter,
|
||||
TablePagination,
|
||||
TableRow,
|
||||
} from "@material-ui/core";
|
||||
import { logger } from "../../services/monitoring";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { useVehicleContext, VehicleProvider } from "../Contexts/VehicleContext";
|
||||
import { useStatusContext } from "../Contexts/StatusContext";
|
||||
import { useUserContext } from "../Contexts/UserContext";
|
||||
import TableHeaderSortable from "../Table/HeaderSortable";
|
||||
import { useLocalStorage } from "../useLocalStorage";
|
||||
import useStyles from "../useStyles";
|
||||
|
||||
const tableColumns = [
|
||||
{
|
||||
id: "flashpack",
|
||||
label: "Flashpack Number",
|
||||
},
|
||||
];
|
||||
|
||||
const PAGE_SIZE = "FLASHPACKS_TABLE_PAGE_SIZE";
|
||||
|
||||
const MainForm = () => {
|
||||
const classes = useStyles();
|
||||
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 {
|
||||
getAllFlashpacks,
|
||||
flashpacks,
|
||||
totalFlashpacks,
|
||||
} = useVehicleContext();
|
||||
const {
|
||||
token: {
|
||||
idToken: { jwtToken: token },
|
||||
},
|
||||
} = useUserContext();
|
||||
|
||||
useEffect(() => {
|
||||
setTitle("Flashpacks");
|
||||
setSitePath([
|
||||
{
|
||||
label: "Tools",
|
||||
link: "/tools/flashpacks",
|
||||
},
|
||||
]);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
loadFlashpacks();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [token, token, pageIndex, pageSize, orderBy, order]);
|
||||
|
||||
const loadFlashpacks = async () => {
|
||||
try {
|
||||
if (!token) return;
|
||||
await getAllFlashpacks(
|
||||
{
|
||||
limit: pageSize,
|
||||
offset: pageSize * pageIndex,
|
||||
order: `${orderBy} ${order}`,
|
||||
},
|
||||
token
|
||||
);
|
||||
} catch (e) {
|
||||
setMessage(e.message);
|
||||
logger.warn(e.stack);
|
||||
}
|
||||
};
|
||||
|
||||
const handleChangePageIndex = (event, newIndex) => {
|
||||
setPageIndex(newIndex);
|
||||
};
|
||||
|
||||
const handleChangePageSize = (event) => {
|
||||
setPageSize(parseInt(event.target.value, 10));
|
||||
setPageIndex(0);
|
||||
};
|
||||
|
||||
const handleSort = (event, property) => {
|
||||
try {
|
||||
if (property === orderBy) {
|
||||
if (order === "asc") {
|
||||
setOrder("desc");
|
||||
} else {
|
||||
setOrder("asc");
|
||||
}
|
||||
} else {
|
||||
setOrderBy(property);
|
||||
setOrder("asc");
|
||||
}
|
||||
} catch (e) {
|
||||
logger.warn(e.stack);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Table>
|
||||
<TableHeaderSortable
|
||||
classes={classes}
|
||||
orderBy={orderBy}
|
||||
order={order}
|
||||
columnData={tableColumns}
|
||||
onSortRequest={handleSort}
|
||||
/>
|
||||
<TableBody>
|
||||
{flashpacks && flashpacks.map((row, index) => (
|
||||
<TableRow key={index}>
|
||||
<TableCell align="center">
|
||||
{row}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
<TableFooter>
|
||||
<TableRow>
|
||||
{!flashpacks || flashpacks.length === 0 ? (
|
||||
<TableCell colSpan={8} align="center">No Flashpack Numbers</TableCell>
|
||||
) : (
|
||||
<TablePagination
|
||||
rowsPerPageOptions={[5, 10, 25, 100]}
|
||||
colSpan={1}
|
||||
count={totalFlashpacks}
|
||||
rowsPerPage={pageSize}
|
||||
page={pageIndex}
|
||||
SelectProps={{
|
||||
inputProps: { "aria-label": "rows per page" },
|
||||
native: true,
|
||||
}}
|
||||
onPageChange={handleChangePageIndex}
|
||||
onRowsPerPageChange={handleChangePageSize}
|
||||
/>)}
|
||||
</TableRow>
|
||||
</TableFooter>
|
||||
</Table>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const Flashpacks = () => (
|
||||
<VehicleProvider>
|
||||
<MainForm />
|
||||
</VehicleProvider>
|
||||
);
|
||||
|
||||
export default Flashpacks;
|
||||
Reference in New Issue
Block a user