diff --git a/src/components/Contexts/SupplierContext.jsx b/src/components/Contexts/SupplierContext.jsx index bfb995d..7348a0f 100644 --- a/src/components/Contexts/SupplierContext.jsx +++ b/src/components/Contexts/SupplierContext.jsx @@ -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 ( {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); diff --git a/src/components/Suppliers/Details/index.js b/src/components/Suppliers/Details/index.js index b93564d..0d32cc4 100644 --- a/src/components/Suppliers/Details/index.js +++ b/src/components/Suppliers/Details/index.js @@ -39,13 +39,12 @@ const Main = () => { }, } = useUserContext(); - const approve = async (event) => { + const update = async (event) => { event.preventDefault(); try { await updateSupplier(email, token); setMessage(`Updated ${email}`); } catch (e) { - console.log(e); setMessage(e.message); } }; @@ -180,7 +179,7 @@ const Main = () => { variant="contained" color="primary" className={classes.submit} - onClick={approve} + onClick={update} > {busy ? "Submitting..." : "Submit"} diff --git a/src/components/Suppliers/List/__snapshots__/index.test.jsx.snap b/src/components/Suppliers/List/__snapshots__/index.test.jsx.snap index 23043a7..31f2e96 100644 --- a/src/components/Suppliers/List/__snapshots__/index.test.jsx.snap +++ b/src/components/Suppliers/List/__snapshots__/index.test.jsx.snap @@ -23,6 +23,29 @@ exports[`Suppliers page Render 1`] = ` + + + ID + + + + @@ -217,6 +243,9 @@ exports[`Suppliers page Render 1`] = ` + @@ -259,6 +288,9 @@ exports[`Suppliers page Render 1`] = ` + diff --git a/src/components/Suppliers/Table/index.jsx b/src/components/Suppliers/Table/index.jsx index 4b6204e..b888197 100644 --- a/src/components/Suppliers/Table/index.jsx +++ b/src/components/Suppliers/Table/index.jsx @@ -18,6 +18,10 @@ import TableHeaderSortable from "../../Table/HeaderSortable"; import { logger } from "../../../services/monitoring"; const tableColumns = [ + { + id: "id", + label: "ID", + }, { id: "contact", label: "Contact", @@ -115,6 +119,7 @@ const SupplierTable = (props) => { {suppliers.map((row, index) => { return ( + {row?.id} {row.contact} diff --git a/src/utils/validationSupplier.js b/src/utils/validationSupplier.js index eb75896..afb2200 100644 --- a/src/utils/validationSupplier.js +++ b/src/utils/validationSupplier.js @@ -7,27 +7,31 @@ export const validateSupplier = (supplier) => { validateEmail(supplier.email); - if (supplier.contact.length === 0) { + if (!supplier?.id) { + throw new Error("id required"); + } + + if (!supplier?.contact) { throw new Error("contact required"); } - if (supplier.company.length === 0) { + if (!supplier?.company) { throw new Error("company required"); } - if (supplier.address.length === 0) { + if (!supplier?.address) { throw new Error("address required"); } - if (supplier.telephone.length === 0) { + if (!supplier?.telephone) { throw new Error("telephone required"); } - if (supplier.program.length === 0) { + if (!supplier?.program) { throw new Error("program required"); } - if (supplier.ecus.length === 0) { + if (!supplier?.ecus) { throw new Error("ecus required"); } };