Skip to content

Commit c690771

Browse files
committed
finished routes
1 parent 07d5fce commit c690771

File tree

4 files changed

+80
-12
lines changed

4 files changed

+80
-12
lines changed

client/components/dashboard-componenets/mainContent/stsManagerContents/RoutesPanel.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import { getCookie } from "@/lib/cookieFunctions";
99
import { AddNewAreaModal } from "@/components/modals/stsControl/addNewArea";
1010
import { AddNewRouteModal } from "@/components/modals/stsControl/AddNewRoute";
1111
import useGetAllArea from "@/hooks/dataQuery/useGetAllArea";
12+
import useGetAllRoutes from "@/hooks/dataQuery/useGetAllRoutes";
1213

1314
export default function RoutesPanel() {
1415
const { areaData, fetchAllArea } = useGetAllArea();
16+
const { routesData, fetchAllRoutes } = useGetAllRoutes();
1517
const areas = [
1618
"akhalia",
1719
"akhalia",
@@ -85,9 +87,11 @@ export default function RoutesPanel() {
8587

8688
useEffect(() => {
8789
fetchAllArea();
90+
fetchAllRoutes();
8891
}, []);
8992

9093
useEffect(() => {}, [areaData]);
94+
useEffect(() => {}, [routesData]);
9195

9296
return (
9397
<main className="flex flex-1 flex-col gap-4 p-4 lg:gap-6 lg:p-6 max-h-[calc(100vh-60px)] overflow-scroll">
@@ -131,10 +135,10 @@ export default function RoutesPanel() {
131135
<h1 className="pl-6 pt-6 text-2xl font-bold">ROUTES LIST</h1>
132136
<br />
133137
<div className="flex flex-wrap gap-4 mt-4 ml-4">
134-
{routes.map((route, index) => (
138+
{routesData.map((route, index) => (
135139
<div className="bg-slate-100 px-4 py-2 rounded-lg shadow-md w-full md:w-auto">
136140
<p className="text-2xl font-bold text-blue-800">{route.name}</p>
137-
<p className="text-lg text-slate-500 font-semibold">{route.area}</p>
141+
<p className="text-lg text-slate-500 font-semibold">{route.areaName}</p>
138142
<p className="text-md">{route.description}</p>
139143
</div>
140144
))}

client/components/modals/stsControl/AddNewRoute.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export const AddNewRouteModal: React.FC<DialogWrapperProps> = ({ children }) =>
3535
const [RouteName, setRouteName] = useState("");
3636
const [RouteDetails, setRouteDetails] = useState("");
3737
const { areaData, fetchAllArea } = useGetAllArea();
38-
const handleSaveChanges = async () => {
39-
axios.post(apiRoutes.route.create, { name:RouteName, stsId:getCookie(stsId), description: RouteDetails }, {
38+
const handleSaveChanges = async () => {
39+
axios.post(apiRoutes.route.create, { name:RouteName, stsId:getCookie(stsId), description: RouteDetails, areaId: areaId }, {
4040
headers: {
4141
Authorization: `Bearer ${getCookie("token")}`,
4242
},
@@ -66,9 +66,9 @@ export const AddNewRouteModal: React.FC<DialogWrapperProps> = ({ children }) =>
6666
</DialogTrigger>
6767
<DialogContent className="w-[825px]">
6868
<DialogHeader>
69-
<DialogTitle>Add New Area</DialogTitle>
69+
<DialogTitle>Add New Route</DialogTitle>
7070
<DialogDescription>
71-
Add new area here. Click save when you're done.
71+
Add new route here. Click save when you're done.
7272
</DialogDescription>
7373
</DialogHeader>
7474
<div className="grid gap-4 py-4">
@@ -78,7 +78,7 @@ export const AddNewRouteModal: React.FC<DialogWrapperProps> = ({ children }) =>
7878
</Label>
7979
<Input
8080
id="vehicleNumber"
81-
placeholder="Area Name"
81+
placeholder="Enter Route Name"
8282
value={RouteName}
8383
onChange={(e) => setRouteName(e.target.value)}
8484
className="col-span-3"
@@ -87,7 +87,7 @@ export const AddNewRouteModal: React.FC<DialogWrapperProps> = ({ children }) =>
8787
</div>
8888
<div className="grid grid-cols-4 items-center gap-4">
8989
<Label htmlFor="role" className="text-right">
90-
Role
90+
Select Area
9191
</Label>
9292
<Select
9393
value={areaId}
@@ -96,7 +96,7 @@ export const AddNewRouteModal: React.FC<DialogWrapperProps> = ({ children }) =>
9696
<SelectTrigger className="col-span-3">
9797
<SelectValue
9898
id="role"
99-
placeholder="Select role for the user"
99+
placeholder="Select area of the route"
100100
/>
101101
</SelectTrigger>
102102
<SelectContent>
@@ -118,7 +118,7 @@ export const AddNewRouteModal: React.FC<DialogWrapperProps> = ({ children }) =>
118118
</Label>
119119
<Input
120120
id="vehicleNumber"
121-
placeholder="Area Name"
121+
placeholder="Enter Route Details"
122122
value={RouteDetails}
123123
onChange={(e) => setRouteDetails(e.target.value)}
124124
className="col-span-3"
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"use client";
2+
import { apiRoutes } from "@/data/apiRoutes";
3+
import { jwtToken } from "@/data/cookieNames";
4+
import {
5+
Contractor,
6+
admin,
7+
landfillManager,
8+
stsManager,
9+
unassigned,
10+
} from "@/data/roles";
11+
import { getCookie } from "@/lib/cookieFunctions";
12+
import { message } from "antd";
13+
import axios from "axios";
14+
import { useState, useEffect, use } from "react";
15+
16+
export default function useGetAllRoutes() {
17+
const [routesData, setRoutesData] = useState<
18+
{
19+
id: string;
20+
name: string;
21+
description: string;
22+
areaId: string;
23+
stsId: string;
24+
areaName: string;
25+
}[]
26+
>([]);
27+
28+
async function fetchAllRoutes() {
29+
try {
30+
const res = await axios.get(apiRoutes.route.getAll, {
31+
headers: {
32+
Authorization: `Bearer ${await getCookie(jwtToken)}`,
33+
},
34+
});
35+
const routesList = res.data.map((route: any) => {
36+
return route?.stsId === getCookie("stsId") ? {
37+
name: route?.name || "No Name",
38+
areaName: route?.area?.name || "No Area",
39+
description: route?.description || "No Description",
40+
} : null;
41+
});
42+
await setRoutesData(routesList);
43+
console.log(routesList);
44+
} catch (error: any) {
45+
message.error(
46+
error?.response?.data?.message +
47+
"Error fetching routes data... Are you authorized?"
48+
);
49+
}
50+
}
51+
52+
useEffect(() => {
53+
fetchAllRoutes();
54+
}, []);
55+
56+
return { fetchAllRoutes, routesData };
57+
}

server/src/controllers/routeArea.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,27 @@ import { Request, Response } from "express";
44
const prisma = new PrismaClient();
55

66
const addRoute = async (req: Request, res: Response) => {
7-
const { name, description, stsId } = req.body;
7+
const { name, description, stsId, areaId } = req.body;
88
const newRoute = await prisma.route.create({
99
data: {
1010
name,
1111
description,
1212
stsId,
13+
areaId
1314
},
1415
});
1516

1617
res.status(201).json(newRoute);
1718
};
1819

1920
const getAllRoutes = async (req: Request, res: Response) => {
20-
const routes = await prisma.route.findMany();
21+
const routes = await prisma.route.findMany(
22+
{
23+
include: {
24+
area: true,
25+
},
26+
}
27+
);
2128
res.status(200).json(routes);
2229
};
2330

0 commit comments

Comments
 (0)