Skip to content

Sts systems #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions client/components/dataTables/StsVehicleList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { DeleteSTSModal } from "../modals/DeleteSTSModal";
import useVehicleList from "@/hooks/vehicles/useVehiclesData";
import useVehicleListForSTS from "@/hooks/vehicles/useGetVeicleForSTS";
import { DeleteVehicleModalForSTS } from "../modals/DeleteVehicleModalForSTS";
import { STSVehicleRelease } from "../modals/STSVehicleReleaseModal";

type Vehicle = {
entryId: string,
Expand Down Expand Up @@ -130,7 +131,7 @@ export const columns: ColumnDef<Vehicle>[] = [
);
},
cell: ({ row }) => (
<div className="text-center font-medium">{row.getValue("entryTime")}</div>
<div className="text-center font-medium">{row.getValue("entryTime".toLocaleString())}</div>
),
},
{
Expand Down Expand Up @@ -162,7 +163,7 @@ export const columns: ColumnDef<Vehicle>[] = [
return (
<div>
<DeleteVehicleModalForSTS vehicleInfo={sts} />
{/* <EditSTSInfoModal stsInfo={sts} /> */}
<STSVehicleRelease vehicleInfo={sts} />
</div>
);
},
Expand Down
56 changes: 56 additions & 0 deletions client/components/maps/VehicleReleaseRoute.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React, { useState, useEffect } from "react";

interface CoordinateProps {
vehicleOriginLatitude: string;
vehicleOriginLongitude: string;
vehicleDestinationLatitude: string;
vehicleDestinationLongitude: string;
distance: string;
duration: string;
setDistance: React.Dispatch<React.SetStateAction<string>>;
setDuration: React.Dispatch<React.SetStateAction<string>>;
}

const VehicleReleaseRoute: React.FC<CoordinateProps> = ({
vehicleOriginLatitude,
vehicleOriginLongitude,
vehicleDestinationLatitude,
vehicleDestinationLongitude,
setDistance,
setDuration,
distance,
duration
}) => {


useEffect(() => {
calculateRoute();
}, []);

async function calculateRoute() {
const VehicleOrigin = `${vehicleOriginLatitude} , ${vehicleOriginLongitude}`;
const VehicleDestination = `${vehicleDestinationLatitude} , ${vehicleDestinationLongitude}`;
console.log(VehicleOrigin);
console.log(VehicleDestination);
const directionsService = new google.maps.DirectionsService();
const results = await directionsService.route({
origin: VehicleOrigin,
destination: VehicleDestination,
travelMode: google.maps.TravelMode.DRIVING,
});

if (results && results.routes.length > 0) {
setDistance(results.routes[0].legs[0].distance?.text || "");
setDuration(results.routes[0].legs[0].duration?.text || "");
}
}

return (
<div>
<p><b>Distance: </b> {distance}</p>
<p><b> Duration: </b> {duration}</p>
</div>
);
}

export default VehicleReleaseRoute;
185 changes: 185 additions & 0 deletions client/components/modals/STSVehicleReleaseModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
DialogClose
} from "@/components/ui/dialog";

import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import React, { use, useEffect, useState } from "react";
import { Send, Trash } from "lucide-react";
import deleteUser from "@/hooks/user_data/deleteUser";
import {
Select,
SelectTrigger,
SelectValue,
SelectContent,
SelectGroup,
SelectLabel,
SelectItem,
} from "../ui/select";
import editUser from "@/hooks/user_data/editUser";
import gettAllRoles from "@/hooks/user_data/useGetAllRole";
import { number } from "prop-types";
import { admin, landfillManager, stsManager, unassigned } from "@/data/roles";
import editSTS from "@/hooks/entityCreation/editSTS";
import getUserByRole from "@/hooks/user_data/getUserByRole";
import VehicleRelaseRoute from "../maps/VehicleReleaseRoute";
import useVehicleReleaseFromSTS from "@/hooks/StsDashboard/useVehicleReleaseFromSTS";


type Vehicle = {
entryId: string,
id: string,
vehicleNumber: string,
vehicleType: string,
capacity: string,
loadedFuelCostPerKm: string,
unloadedFuelCostPerKm: string,
landFillId: string,
entryTime: string,
landFillName: string,
stsLattitude: string,
stsLongitude: string,
landfillLattitude: string,
landfillLongitude: string,
};

export const STSVehicleRelease = ({ vehicleInfo }: { vehicleInfo: Vehicle }) => {
const [vehicleData, setVehicleData] = useState(vehicleInfo);
const [weightOfWaste , setWeightOfWaste] = useState("");
const [exitTime, setExitTime] = useState(new Date().toLocaleString());
const [stsCoordinate, setStsCoordinate] = useState("");
const [landFillCoordinate, setLandFillCoordinate] = useState("");
const [distance, setDistance] = useState<string>("");
const [duration, setDuration] = useState<string>("");const { VehicleReleaseFromSTS } = useVehicleReleaseFromSTS();



// const dummyCoordinates[
// 23.76652752, 90.4258899
// 23.76449486, 90.3879528
// 23.73897468, 90.3750954
// 23.76431111, 90.3651622
// 23.77393625, 90.3814204
// 23.76461481, 90.3915441
// 23.77089053, 90.4042765
// 23.72965447, 90.3873709
// ]
// {
// "stsVehicleId": "sv1",
// "weightOfWaste" : 2,
// "exitTime" : "2024-03-27T08:00:00Z",
// "distance": "156.3",
// "estimatedDuration": "23"
// }


const handleSaveChanges = async () => {
try {

const postEntry = await VehicleReleaseFromSTS({
stsVehicleId: vehicleInfo.entryId,
weightOfWaste: weightOfWaste,
exitTime: exitTime,
distance: distance,
estimatedDuration: duration
});

} catch (error) {
console.error("Error:", error);
}


};


return (
<Dialog>
<DialogTrigger asChild>
<Button variant="ghost" title="Edit STS Info" className="h-8 w-8 p-0">
<Send className="h-4 w-4" />
</Button>
</DialogTrigger>
<DialogContent className="max-w-[425px]">
<DialogHeader>
<DialogTitle className="mt-4 text-xl sm:text-2xl">
Vehicle Release Details
</DialogTitle>
<DialogDescription>
<div className="mt-4 flex flex-col justify-center items-start text-left p-4 rounded-lg border shadow-xl text-md">
<h1>
<span className="font-bold">Vehicle Number: </span>
{vehicleInfo.stsLattitude}
</h1>
<p>
<span className="font-bold">Entry Time: </span>
{vehicleInfo.stsLongitude}
</p>
<p>
<span className="font-bold">Capacity: </span>
{vehicleInfo.capacity}
</p>
<p>
<span className="font-bold">Landfill Name: </span>
{vehicleInfo.landFillName}
</p>
<p>
<span className="font-bold">Optimized Route: </span>
<VehicleRelaseRoute
vehicleOriginLatitude={vehicleInfo.stsLattitude}
vehicleOriginLongitude={vehicleInfo.stsLongitude}
vehicleDestinationLatitude={vehicleInfo.landfillLattitude}
vehicleDestinationLongitude={vehicleInfo.landfillLongitude}
setDistance={setDistance}
setDuration={setDuration}
distance={distance}
duration={duration}
/>
</p>

</div>
</DialogDescription>
</DialogHeader>
<div className="grid gap-4 py-4">
<div className="grid grid-cols-4 items-center gap-4">
<Label htmlFor="name" className="text-right">
Weight of Waste
</Label>
<Input
id="weightOfWaste"
placeholder="Volume (in tons)"
className="col-span-3"
value={weightOfWaste}
onChange={(e) => setWeightOfWaste(e.target.value)}
/>
</div>

<div className="grid grid-cols-4 items-center gap-4">
<Label htmlFor="capacity" className="text-right">
Deparature Time
</Label>
<Input
id="capacity"
placeholder="1-100"
className="col-span-3"
value={exitTime}
onChange={(e) => setExitTime(e.target.value)}
/>
</div>
</div>
<DialogFooter>
<DialogClose asChild>
<Button type="button" onClick={handleSaveChanges}>Save changes</Button>
</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
);
};
52 changes: 52 additions & 0 deletions client/hooks/StsDashboard/useVehicleReleaseFromSTS.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { useState } from 'react';
import {admin, landfillManager, stsManager, unassigned} from '@/data/roles';
import { setCookie, getCookie } from '@/lib/cookieFunctions';
import axios from 'axios';
import { jwtToken, role , uid , username, stsId} from '@/data/cookieNames';
import { uri } from '@/data/constant';

export default function useVehicleReleaseFromSTS() {



async function VehicleReleaseFromSTS(data: {
stsVehicleId: string,
weightOfWaste: string,
exitTime: string,
distance: string,
estimatedDuration: string
}) {
const userStsId = getCookie(stsId);
// console.log(stsId);


try {
const isoString = new Date(data.exitTime).toISOString();

const distanceWithoutUnit = parseFloat(data.distance.replace(" km", ""));
const durationWithoutUnit = parseFloat(data.estimatedDuration.replace("mins", ""));
// console.log(data.stsVehicleId);
// console.log(data.weightOfWaste);
// console.log(data.exitTime);
// console.log(distanceWithoutUnit);
// console.log(durationWithoutUnit);
const res = await axios.post('http://localhost:8585/trips/create', {
stsVehicleId: data.stsVehicleId,
weightOfWaste: data.weightOfWaste,
exitTime: isoString,
distance: distanceWithoutUnit,
estimatedDuration: durationWithoutUnit
});
//use the response from here
console.log(res.data);

return true;
} catch (error: any) {
alert(error.message?.toString() || "error logging in");
return false;
}

}

return {VehicleReleaseFromSTS};
}
10 changes: 5 additions & 5 deletions client/hooks/vehicles/useGetVeicleForSTS.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export default function useVehicleListForSTS() {
unloadedFuelCostPerKm: vehicle.vehicle.unloadedFuelCostPerKm,
landFillId: vehicle.landFillId,
entryTime: vehicle.entryTime,
landFillName: "vehicle.landfill.landFillName",
stsLattitude: vehicle.sts.stsLattitude,
stsLongitude: vehicle.sts.stsLongitude,
landfillLattitude: "vehicle.landfill.landfillLattitude",
landfillLongitude: "vehicle.landfill.landfillLongitude",
landFillName: vehicle.vehicle.landFill.name,
stsLattitude: vehicle.sts.latitude,
stsLongitude: vehicle.sts.longitude,
landfillLattitude: vehicle.vehicle.landFill.latitude,
landfillLongitude: vehicle.vehicle.landFill.longitude,
}));
const vehicleNumbers = res.data.map((vehicle: Vehicle) => vehicle.vehicleNumber);

Expand Down
16 changes: 8 additions & 8 deletions server/src/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,25 +207,25 @@ const stsData: Prisma.STSCreateInput[] = [
name: "Moheshkhali",
wardNumber: "13",
capacity: 1000,
latitude: 342.456,
longitude: 85.91,
latitude: 23.76652752,
longitude: 90.4258899
},
{
id: "sts2",
name: "Gulshan",
wardNumber: "2",
capacity: 2000,
latitude: 234.456,
longitude: 75.912,
latitude: 23.76449486,
longitude: 90.3879528,
},

{
id: "sts3",
name: "Bonani",
wardNumber: "4",
capacity: 1500,
latitude: 425.456,
longitude: 123.912,
latitude: 23.73897468,
longitude: 90.3750954,
},
];

Expand All @@ -234,8 +234,8 @@ const landfillData: Prisma.LandfillCreateInput[] = [
id: "c4028362-6c17-4cf0-9b0e-ae20acfa2fbd",
name: "Amin Bazar",
capacity: 10000,
latitude: 23.7894892,
longitude: 90.2669163,
latitude: 23.76431111,
longitude: 90.3651622,
},
];

Expand Down