CEC-2920 Aftersales certificates (#225)

* CEC-2920 aftersales certificates

* smells

* smells
This commit is contained in:
John Wu
2022-10-25 11:00:50 -07:00
committed by GitHub
parent 58890ea40e
commit aaf47f4cc7
7 changed files with 231 additions and 31 deletions

View File

@@ -1,21 +1,25 @@
import React, { useContext, useState } from "react";
import api from "../../services/certificatesAPI";
import { CertTypes } from "../../utils/certificates";
const CertificateContext = React.createContext();
export const CertTypes = {
TBOX: "TBOX",
ICC: "ICC",
Charging: "Charging",
Aftersales: "Aftersales",
};
const validateCreate = (data) => {
if (!data.type) throw new Error("type is required");
if (!data.common_name) throw new Error("common name is required");
};
const callCreateCert = (data, token) => {
if (data.type === CertTypes.Aftersales) {
data.tool_id = data.common_name;
delete data.common_name;
return api.createAftersales(data, token);
}
return api.create(data, token);
};
export const CertificateProvider = ({ children }) => {
const [busy, setBusy] = useState(false);
@@ -25,7 +29,8 @@ export const CertificateProvider = ({ children }) => {
validateCreate(data);
const result = await api.create(data, token);
const result = await callCreateCert(data, token);
if (result.error) {
throw new Error(`Create certificate error. ${result.message}`);
}