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
This commit is contained in:
114
src/components/Contexts/SupplierContext.jsx
Normal file
114
src/components/Contexts/SupplierContext.jsx
Normal file
@@ -0,0 +1,114 @@
|
||||
import React, { useContext, useState } from "react";
|
||||
import api from "../../services/suppliersAPI";
|
||||
|
||||
const SupplierContext = React.createContext();
|
||||
|
||||
export const SupplierProvider = ({ children }) => {
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [suppliers, setSuppliers] = useState([]);
|
||||
const [totalSuppliers, setTotalSuppliers] = useState(0);
|
||||
|
||||
const getSuppliers = async (search, token) => {
|
||||
try {
|
||||
setBusy(true);
|
||||
const result = await api.getSuppliers(search, token);
|
||||
if (result.error) {
|
||||
setSuppliers([]);
|
||||
throw new Error(`Get suppliers error. ${result.message}`);
|
||||
}
|
||||
setSuppliers(result.data ?? []);
|
||||
if (result.total) {
|
||||
setTotalSuppliers(result.total);
|
||||
}
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
};
|
||||
|
||||
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}
|
||||
</SupplierContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
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);
|
||||
126
src/components/Contexts/SupplierDetailsContext.jsx
Normal file
126
src/components/Contexts/SupplierDetailsContext.jsx
Normal file
@@ -0,0 +1,126 @@
|
||||
import React, { useContext, useEffect, useState } from "react";
|
||||
import api from "../../services/suppliersAPI";
|
||||
|
||||
import {
|
||||
validateSupplier,
|
||||
validateEmail,
|
||||
} from "../../utils/validationSupplier";
|
||||
import { useSupplierContext } from "./SupplierContext";
|
||||
|
||||
const SupplierDetailsContext = React.createContext();
|
||||
|
||||
export const SupplierDetailsProvider = ({ children }) => {
|
||||
const { getSuppliers, suppliers } = useSupplierContext();
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [id, setID] = useState("");
|
||||
const [contact, setContact] = useState("");
|
||||
const [company, setCompany] = useState("");
|
||||
const [address, setAddress] = useState("");
|
||||
const [phone, setPhone] = useState("");
|
||||
const [program, setProgram] = useState("");
|
||||
const [ecus, setECUs] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
if (!suppliers || suppliers.length === 0) return;
|
||||
|
||||
const supplier = suppliers[0];
|
||||
|
||||
setID(supplier?.id || "");
|
||||
setContact(supplier?.contact || "");
|
||||
setCompany(supplier?.company || "");
|
||||
setAddress(supplier?.address || "");
|
||||
setPhone(supplier?.telephone || "");
|
||||
setProgram(supplier?.program || "");
|
||||
setECUs(supplier?.ecus.join(",") || "");
|
||||
}, [suppliers]);
|
||||
|
||||
const getSupplier = async (email, token) => {
|
||||
try {
|
||||
setBusy(true);
|
||||
const result = await getSuppliers({ email }, token);
|
||||
if (result?.error)
|
||||
throw new Error(`Get supplier error. ${result.message}`);
|
||||
return result;
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
};
|
||||
|
||||
const getECUs = () => {
|
||||
if (!ecus) return [];
|
||||
|
||||
const items = ecus.split(",");
|
||||
|
||||
return items.map((item) => item.trim());
|
||||
};
|
||||
|
||||
const formData = (email) => ({
|
||||
id,
|
||||
contact,
|
||||
company,
|
||||
address,
|
||||
telephone: phone,
|
||||
email,
|
||||
program,
|
||||
ecus: getECUs(),
|
||||
});
|
||||
|
||||
const updateSupplier = async (email, token) => {
|
||||
try {
|
||||
const supplier = formData(email);
|
||||
setBusy(true);
|
||||
validateSupplier(supplier);
|
||||
|
||||
const result = await api.updateSupplier(email, supplier, 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 (
|
||||
<SupplierDetailsContext.Provider
|
||||
value={{
|
||||
busy,
|
||||
id,
|
||||
contact,
|
||||
company,
|
||||
address,
|
||||
phone,
|
||||
program,
|
||||
ecus,
|
||||
deleteSupplier,
|
||||
getSupplier,
|
||||
updateSupplier,
|
||||
setID,
|
||||
setContact,
|
||||
setCompany,
|
||||
setAddress,
|
||||
setPhone,
|
||||
setProgram,
|
||||
setECUs,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</SupplierDetailsContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useSupplierDetailsContext = () =>
|
||||
useContext(SupplierDetailsContext);
|
||||
@@ -36,6 +36,7 @@ export const VehicleProvider = ({ children }) => {
|
||||
|
||||
const addConnections = async (cars, token) => {
|
||||
try {
|
||||
if (cars.length === 0) return;
|
||||
const vins = cars.map((car) => car.vin);
|
||||
const result = await api.getConnections(vins, token);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user