* CEC-1965 Supplier update and approval (#162) * CEC-1965 Supplier update and approval Fix calling connectedcars for no VINs Search sets table page to 0 * PR comments * CEC-1965 Cleanup (#163) * CEC-1965 Table footer fix (#164) tiny tiny change
167 lines
4.2 KiB
JavaScript
167 lines
4.2 KiB
JavaScript
import React, { useEffect, useState } from "react";
|
|
import { Link } from "react-router-dom";
|
|
import PropTypes from "prop-types";
|
|
import {
|
|
Table,
|
|
TableBody,
|
|
TableCell,
|
|
TableFooter,
|
|
TablePagination,
|
|
TableRow,
|
|
} from "@material-ui/core";
|
|
import clsx from "clsx";
|
|
|
|
import { useSupplierContext } from "../../Contexts/SupplierContext";
|
|
import { useStatusContext } from "../../Contexts/StatusContext";
|
|
import { LocalDateTimeString } from "../../../utils/dates";
|
|
import TableHeaderSortable from "../../Table/HeaderSortable";
|
|
import { logger } from "../../../services/monitoring";
|
|
|
|
const tableColumns = [
|
|
{
|
|
id: "id",
|
|
label: "ID",
|
|
},
|
|
{
|
|
id: "contact",
|
|
label: "Contact",
|
|
},
|
|
{
|
|
id: "company",
|
|
label: "Company",
|
|
},
|
|
{
|
|
id: "email",
|
|
label: "Email",
|
|
},
|
|
{
|
|
id: "program",
|
|
label: "Program",
|
|
},
|
|
{
|
|
id: "ecus",
|
|
label: "ECUs",
|
|
},
|
|
{
|
|
id: "created_at",
|
|
label: "Registered",
|
|
},
|
|
];
|
|
|
|
const SupplierTable = (props) => {
|
|
const { token, classes } = props;
|
|
const [pageSize, setPageSize] = useState(10);
|
|
const [pageIndex, setPageIndex] = useState(0);
|
|
const [orderBy, setOrderBy] = useState("created_at");
|
|
const [order, setOrder] = useState("asc");
|
|
const { getSuppliers, suppliers, totalSuppliers } = useSupplierContext();
|
|
const { setMessage } = useStatusContext();
|
|
|
|
const sortHandler = (_event, property) => {
|
|
if (property === orderBy) {
|
|
if (order === "asc") {
|
|
setOrder("desc");
|
|
} else {
|
|
setOrder("asc");
|
|
}
|
|
} else {
|
|
setOrderBy(property);
|
|
setOrder("asc");
|
|
}
|
|
};
|
|
|
|
const handleChangePageIndex = (_event, newIndex) => {
|
|
setPageIndex(newIndex);
|
|
};
|
|
|
|
const handleChangePageSize = (event) => {
|
|
setPageSize(parseInt(event.target.value, 10));
|
|
setPageIndex(0);
|
|
};
|
|
|
|
const query = async (signal) => {
|
|
try {
|
|
const options = {
|
|
limit: pageSize,
|
|
offset: pageSize * pageIndex,
|
|
order: `${orderBy} ${order}`,
|
|
};
|
|
await getSuppliers(options, token, signal);
|
|
} catch (e) {
|
|
setMessage(e.message);
|
|
logger.warn(e.stack);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
const controller = new AbortController();
|
|
|
|
query(controller.signal);
|
|
|
|
return () => {
|
|
controller.abort();
|
|
};
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [pageIndex, pageSize, orderBy, order, token]);
|
|
|
|
return (
|
|
<div className={clsx(classes.paper, classes.tableSize)}>
|
|
<Table>
|
|
<TableHeaderSortable
|
|
classes={classes}
|
|
orderBy={orderBy}
|
|
order={order}
|
|
columnData={tableColumns}
|
|
onSortRequest={sortHandler}
|
|
rowCount={suppliers ? suppliers.length : 0}
|
|
/>
|
|
<TableBody>
|
|
{suppliers.map((row, index) => {
|
|
return (
|
|
<TableRow key={index}>
|
|
<TableCell align="center">{row?.id}</TableCell>
|
|
<TableCell align="center">
|
|
<Link to={`/supplier/${row.email}`}>{row.contact}</Link>
|
|
</TableCell>
|
|
<TableCell align="center">{row.company}</TableCell>
|
|
<TableCell align="center">
|
|
<a href={`mailto:${row.email}`}>{row.email}</a>
|
|
</TableCell>
|
|
<TableCell align="center">{row.program}</TableCell>
|
|
<TableCell align="center">{row.ecus.join(", ")}</TableCell>
|
|
<TableCell align="center">
|
|
{LocalDateTimeString(row.created)}
|
|
</TableCell>
|
|
</TableRow>
|
|
);
|
|
})}
|
|
</TableBody>
|
|
<TableFooter>
|
|
<TableRow>
|
|
<TablePagination
|
|
rowsPerPageOptions={[5, 10, 25, 100]}
|
|
colSpan={7}
|
|
count={totalSuppliers}
|
|
rowsPerPage={pageSize}
|
|
page={pageIndex}
|
|
SelectProps={{
|
|
inputProps: { "aria-label": "rows per page" },
|
|
native: true,
|
|
}}
|
|
onPageChange={handleChangePageIndex}
|
|
onRowsPerPageChange={handleChangePageSize}
|
|
/>
|
|
</TableRow>
|
|
</TableFooter>
|
|
</Table>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
SupplierTable.propTypes = {
|
|
token: PropTypes.string.isRequired,
|
|
classes: PropTypes.object.isRequired,
|
|
};
|
|
|
|
export default SupplierTable;
|