CEC-1965 Cleanup (#163)

This commit is contained in:
John Wu
2022-07-01 13:00:26 -07:00
committed by GitHub
parent fe40c26c56
commit 86418d093c
5 changed files with 49 additions and 81 deletions

View File

@@ -25,43 +25,13 @@ export const SupplierProvider = ({ children }) => {
} }
}; };
const updateSupplier = async (email, s, token) => {
try {
setBusy(true);
validateSupplier(s);
const result = await api.updateSupplier(email, s, token);
if (result.error)
throw new Error(`Update supplier error. ${result.message}`);
return result;
} finally {
setBusy(false);
}
};
const deleteSupplier = async (vin, token) => {
try {
setBusy(true);
validateEmail(vin);
const result = await api.deleteSupplier(vin, token);
if (result.error)
throw new Error(`Delete supplier error. ${result.message}`);
return result;
} finally {
setBusy(false);
}
};
return ( return (
<SupplierContext.Provider <SupplierContext.Provider
value={{ value={{
busy, busy,
totalSuppliers, totalSuppliers,
suppliers, suppliers,
deleteSupplier,
getSuppliers, getSuppliers,
updateSupplier,
}} }}
> >
{children} {children}
@@ -69,46 +39,4 @@ export const SupplierProvider = ({ children }) => {
); );
}; };
const validateSupplier = (s) => {
if (s == null) {
throw new Error("No supplier data");
}
validateEmail(s.email);
if (s.contact.length === 0) {
throw new Error("contact required");
}
if (s.company.length === 0) {
throw new Error("company required");
}
if (s.address.length === 0) {
throw new Error("address required");
}
if (s.telephone.length === 0) {
throw new Error("telephone required");
}
if (s.program.length === 0) {
throw new Error("program required");
}
if (s.ecus.length === 0) {
throw new Error("ecus required");
}
};
const rxEmail =
/^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
const errInvalidEmail = new Error("invalid email");
const validateEmail = (email) => {
if (!email) throw errInvalidEmail;
if (!rxEmail.test(email)) throw errInvalidEmail;
};
export const useSupplierContext = () => useContext(SupplierContext); export const useSupplierContext = () => useContext(SupplierContext);

View File

@@ -39,13 +39,12 @@ const Main = () => {
}, },
} = useUserContext(); } = useUserContext();
const approve = async (event) => { const update = async (event) => {
event.preventDefault(); event.preventDefault();
try { try {
await updateSupplier(email, token); await updateSupplier(email, token);
setMessage(`Updated ${email}`); setMessage(`Updated ${email}`);
} catch (e) { } catch (e) {
console.log(e);
setMessage(e.message); setMessage(e.message);
} }
}; };
@@ -180,7 +179,7 @@ const Main = () => {
variant="contained" variant="contained"
color="primary" color="primary"
className={classes.submit} className={classes.submit}
onClick={approve} onClick={update}
> >
{busy ? "Submitting..." : "Submit"} {busy ? "Submitting..." : "Submit"}
</Button> </Button>

View File

@@ -23,6 +23,29 @@ exports[`Suppliers page Render 1`] = `
<tr <tr
class="MuiTableRow-root MuiTableRow-head" class="MuiTableRow-root MuiTableRow-head"
> >
<th
class="MuiTableCell-root MuiTableCell-head MuiTableCell-alignCenter"
scope="col"
>
<span
aria-disabled="false"
class="MuiButtonBase-root MuiTableSortLabel-root"
role="button"
tabindex="0"
>
ID
<svg
aria-hidden="true"
class="MuiSvgIcon-root MuiTableSortLabel-icon MuiTableSortLabel-iconDirectionAsc"
focusable="false"
viewBox="0 0 24 24"
>
<path
d="M20 12l-1.41-1.41L13 16.17V4h-2v12.17l-5.58-5.59L4 12l8 8 8-8z"
/>
</svg>
</span>
</th>
<th <th
class="MuiTableCell-root MuiTableCell-head MuiTableCell-alignCenter" class="MuiTableCell-root MuiTableCell-head MuiTableCell-alignCenter"
scope="col" scope="col"
@@ -175,6 +198,9 @@ exports[`Suppliers page Render 1`] = `
<tr <tr
class="MuiTableRow-root" class="MuiTableRow-root"
> >
<td
class="MuiTableCell-root MuiTableCell-body MuiTableCell-alignCenter"
/>
<td <td
class="MuiTableCell-root MuiTableCell-body MuiTableCell-alignCenter" class="MuiTableCell-root MuiTableCell-body MuiTableCell-alignCenter"
> >
@@ -217,6 +243,9 @@ exports[`Suppliers page Render 1`] = `
<tr <tr
class="MuiTableRow-root" class="MuiTableRow-root"
> >
<td
class="MuiTableCell-root MuiTableCell-body MuiTableCell-alignCenter"
/>
<td <td
class="MuiTableCell-root MuiTableCell-body MuiTableCell-alignCenter" class="MuiTableCell-root MuiTableCell-body MuiTableCell-alignCenter"
> >
@@ -259,6 +288,9 @@ exports[`Suppliers page Render 1`] = `
<tr <tr
class="MuiTableRow-root" class="MuiTableRow-root"
> >
<td
class="MuiTableCell-root MuiTableCell-body MuiTableCell-alignCenter"
/>
<td <td
class="MuiTableCell-root MuiTableCell-body MuiTableCell-alignCenter" class="MuiTableCell-root MuiTableCell-body MuiTableCell-alignCenter"
> >

View File

@@ -18,6 +18,10 @@ import TableHeaderSortable from "../../Table/HeaderSortable";
import { logger } from "../../../services/monitoring"; import { logger } from "../../../services/monitoring";
const tableColumns = [ const tableColumns = [
{
id: "id",
label: "ID",
},
{ {
id: "contact", id: "contact",
label: "Contact", label: "Contact",
@@ -115,6 +119,7 @@ const SupplierTable = (props) => {
{suppliers.map((row, index) => { {suppliers.map((row, index) => {
return ( return (
<TableRow key={index}> <TableRow key={index}>
<TableCell align="center">{row?.id}</TableCell>
<TableCell align="center"> <TableCell align="center">
<Link to={`/supplier/${row.email}`}>{row.contact}</Link> <Link to={`/supplier/${row.email}`}>{row.contact}</Link>
</TableCell> </TableCell>

View File

@@ -7,27 +7,31 @@ export const validateSupplier = (supplier) => {
validateEmail(supplier.email); validateEmail(supplier.email);
if (supplier.contact.length === 0) { if (!supplier?.id) {
throw new Error("id required");
}
if (!supplier?.contact) {
throw new Error("contact required"); throw new Error("contact required");
} }
if (supplier.company.length === 0) { if (!supplier?.company) {
throw new Error("company required"); throw new Error("company required");
} }
if (supplier.address.length === 0) { if (!supplier?.address) {
throw new Error("address required"); throw new Error("address required");
} }
if (supplier.telephone.length === 0) { if (!supplier?.telephone) {
throw new Error("telephone required"); throw new Error("telephone required");
} }
if (supplier.program.length === 0) { if (!supplier?.program) {
throw new Error("program required"); throw new Error("program required");
} }
if (supplier.ecus.length === 0) { if (!supplier?.ecus) {
throw new Error("ecus required"); throw new Error("ecus required");
} }
}; };