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 (
<SupplierContext.Provider
value={{
busy,
totalSuppliers,
suppliers,
deleteSupplier,
getSuppliers,
updateSupplier,
}}
>
{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);