CEC-2920 Aftersales certificates (#225)
* CEC-2920 aftersales certificates * smells * smells
This commit is contained in:
@@ -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}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user