-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuseCreateContractor.tsx
41 lines (35 loc) · 1.22 KB
/
useCreateContractor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"use client";
import { apiRoutes } from "@/data/apiRoutes";
import { jwtToken } from "@/data/cookieNames";
import { Contractor, admin, landfillManager, stsManager, unassigned } from "@/data/roles";
import { getCookie } from "@/lib/cookieFunctions";
import { message } from "antd";
import axios from "axios";
export default function useCreateContractor() {
// const [stsData, setStsData] = useState<STS>();
function isValid(contractorData: Contractor) {
return (
contractorData.name.length > 0 &&
contractorData.contactNumber.length > 0 &&
contractorData.tinNumber.length !== null
);
}
async function createContractor(contractorData: Contractor) {
if (contractorData && isValid(contractorData)) {
try {
const res = await axios.post(apiRoutes.contractor.create, contractorData, {
headers: {
Authorization: `Bearer ${await getCookie(jwtToken)}`,
},
});
window.location.reload();
return "Contructor Aadded successfully";
} catch (error: any) {
message.error(error.message?.toString() || "Error creating Contructor");
return null;
}
}
return null;
}
return { createContractor };
}