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:
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);
|
||||
Reference in New Issue
Block a user