CEC-749 Generate cert UI (#141)
* Add Create Certificate page * Tests * Update permission check * Use Azure
This commit is contained in:
50
src/components/Contexts/CertificateContext.jsx
Normal file
50
src/components/Contexts/CertificateContext.jsx
Normal file
@@ -0,0 +1,50 @@
|
||||
import React, { useContext, useState } from "react";
|
||||
|
||||
import api from "../../services/certificatesAPI";
|
||||
|
||||
const CertificateContext = React.createContext();
|
||||
|
||||
export const CertTypes = {
|
||||
TREX: "TREX",
|
||||
HMI: "HMI",
|
||||
Charging: "CHARGE",
|
||||
};
|
||||
|
||||
const validateCreate = (data) => {
|
||||
if (!data.type) throw new Error("type is required");
|
||||
if (!data.vin) throw new Error("vin is required");
|
||||
};
|
||||
|
||||
export const CertificateProvider = ({ children }) => {
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
const createCert = async (data, token) => {
|
||||
try {
|
||||
setBusy(true);
|
||||
|
||||
validateCreate(data);
|
||||
|
||||
const result = await api.create(data, token);
|
||||
if (result.error) {
|
||||
throw new Error(`Create certificate error. ${result.message}`);
|
||||
}
|
||||
|
||||
return result;
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<CertificateContext.Provider
|
||||
value={{
|
||||
busy,
|
||||
createCert,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</CertificateContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useCertificateContext = () => useContext(CertificateContext);
|
||||
Reference in New Issue
Block a user