-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuseSTSAvailableVehicles.tsx
75 lines (63 loc) · 2.36 KB
/
useSTSAvailableVehicles.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { useState } from "react";
import axios from "axios";
import { uri } from "@/data/constant";
import { apiRoutes } from "@/data/apiRoutes";
import { jwtToken, stsId } from "@/data/cookieNames";
import { getCookie } from "@/lib/cookieFunctions";
import { message } from "antd";
type Vehicle = {
id: string;
vehicleNumber: string;
vehicleType: string;
capacity: string;
currentLatitude: string,
currentLongitude: string,
landFillId: string;
stsId: string;
};
type VehicleCoordinateType = {
vehicleNumber: string;
vehicleType: string;
capacity: string;
coordinate: string;
};
export default function useGetSTSAvailableVehicles() {
const [vehicleList, setVehicleList] = useState<Vehicle[]>([]); // Initialize with an empty array of Vehicle objects
const [vehicleNumberList, setVehicleNumberList] = useState<string[]>([]);
const [vehicleRoute, setVehicleRoute] = useState<VehicleCoordinateType[]>([]);
async function GetSTSAvailableVehicles() {
try {
const res = await axios.get(`${apiRoutes.sts.vehicle.current}/${getCookie(stsId)}/get-available-vehicles`, {
headers: { Authorization: `Bearer ${getCookie(jwtToken)}` },
});
// Assuming the response data is an array of vehicles
const AllVehicle: Vehicle[] = res.data.map((vehicle: any) => ({
id: vehicle.id,
vehicleNumber: vehicle.vehicleNumber,
vehicleType: vehicle.vehicleType,
capacity: vehicle.capacity,
landFillId: vehicle.landFillId,
stsId: vehicle.stsId,
currentLatitude: vehicle.currentLatitude,
currentLongitude: vehicle.currentLongitude
}));
const vehicleNumbers = res.data.map(
(vehicle: Vehicle) => vehicle.vehicleNumber
);
const stsRouteCalc: VehicleCoordinateType[] = AllVehicle.map((data: Vehicle) => ({
coordinate: `${data.currentLatitude}, ${data.currentLongitude}`,
vehicleNumber: data.vehicleNumber,
vehicleType: data.vehicleType,
capacity: data.capacity
}));
setVehicleRoute(stsRouteCalc);
setVehicleList(AllVehicle);
setVehicleNumberList(vehicleNumbers);
return true;
} catch (error: any) {
message.error(error?.response?.data?.message?.toString() || "Error fetching vehicle list");
return false;
}
}
return { vehicleList,vehicleRoute, vehicleNumberList, GetSTSAvailableVehicles };
}