-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuseCreateLandfill.tsx
46 lines (40 loc) · 1.28 KB
/
useCreateLandfill.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
42
43
44
45
46
"use client";
import { apiRoutes } from "@/data/apiRoutes";
import { jwtToken } from "@/data/cookieNames";
import { admin, landfillManager, stsManager, unassigned } from "@/data/roles";
import { getCookie } from "@/lib/cookieFunctions";
import axios from "axios";
export type LandFill = {
name: string;
capacity: number;
latitude: number;
longitude: number;
};
export default function useCreateLandFill() {
// const [landfillData, setlandfillData] = useState<STS>();
function isValid(landfillData: LandFill) {
return (
landfillData.name.length > 0 &&
landfillData.capacity > 0 &&
landfillData.latitude !== null &&
landfillData.longitude !== null
);
}
async function createLandfill(landfillData: LandFill) {
if (landfillData && isValid(landfillData)) {
try {
const res = await axios.post(apiRoutes.landfill.create, landfillData, {
headers: {
Authorization: `Bearer ${await getCookie(jwtToken)}`,
},
});
window.location.reload();
return "Landfill Aadded successfully";
} catch (error: any) {
return error?.response?.data.message?.toString() || "Error creating Landfill";
}
}
return null;
}
return { createLandfill };
}