From 14b043e4c3e4e5039f015a1603df053bfaa8b0cf Mon Sep 17 00:00:00 2001 From: amjed-ali-k Date: Sat, 28 Oct 2023 23:03:51 +0530 Subject: [PATCH] feat: add update hall options --- src/app/api/secure/exam-seating/route.ts | 29 +++++++++++++++++++ .../exam-seating/student-batches/route.ts | 11 +++++++ .../hall/[hallId]/_components/newClass.tsx | 21 ++++++++++---- .../hall/_components/classList.tsx | 9 ++++-- src/components/Navigation.tsx | 2 +- 5 files changed, 63 insertions(+), 9 deletions(-) diff --git a/src/app/api/secure/exam-seating/route.ts b/src/app/api/secure/exam-seating/route.ts index 4ef41b7..d547b48 100644 --- a/src/app/api/secure/exam-seating/route.ts +++ b/src/app/api/secure/exam-seating/route.ts @@ -167,3 +167,32 @@ export async function DELETE(request: NextRequest) { return NextResponse.json(results); } + +export async function GET(request: NextRequest) { + const userId = await getUserId(request); + if (!userId) + return NextResponse.json({ message: "Unauthenticated" }, { status: 401 }); + + const id = request.nextUrl.searchParams.get("id"); + if (!id) + return NextResponse.json({ message: "Invalid request" }, { status: 400 }); + + const result = await prisma.examHall.findUnique({ + where: { + id: id, + }, + }); + + if (!result) { + return NextResponse.json({ message: "Hall not found" }, { status: 404 }); + } + + if (result?.createdById !== userId) { + return NextResponse.json( + { message: "You are not authorized to view this hall" }, + { status: 401 } + ); + } + + return NextResponse.json(result); +} diff --git a/src/app/api/secure/exam-seating/student-batches/route.ts b/src/app/api/secure/exam-seating/student-batches/route.ts index a525682..1f7f7b6 100644 --- a/src/app/api/secure/exam-seating/student-batches/route.ts +++ b/src/app/api/secure/exam-seating/student-batches/route.ts @@ -145,5 +145,16 @@ export async function GET(request: NextRequest) { }, }); + if (!result) { + return NextResponse.json({ message: "Batch not found" }, { status: 404 }); + } + + if (result?.createdById !== userId) { + return NextResponse.json( + { message: "You are not authorized to view this batch" }, + { status: 401 } + ); + } + return NextResponse.json(result); } diff --git a/src/app/dashboard/tools/exam-seating/hall/[hallId]/_components/newClass.tsx b/src/app/dashboard/tools/exam-seating/hall/[hallId]/_components/newClass.tsx index 622ed0f..525d469 100644 --- a/src/app/dashboard/tools/exam-seating/hall/[hallId]/_components/newClass.tsx +++ b/src/app/dashboard/tools/exam-seating/hall/[hallId]/_components/newClass.tsx @@ -1,10 +1,9 @@ "use client"; -import React, { useEffect, useRef, useState } from "react"; +import React, { useEffect, useState } from "react"; import { ContextMenu, ContextMenuContent, ContextMenuItem, - ContextMenuShortcut, ContextMenuSeparator, ContextMenuSub, ContextMenuSubContent, @@ -17,10 +16,8 @@ import { DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, - DropdownMenuLabel, DropdownMenuPortal, DropdownMenuSeparator, - DropdownMenuShortcut, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, @@ -69,10 +66,17 @@ function EditHallForm({ id }: { id?: string }) { const { toast } = useToast(); const router = useRouter(); - const { data: existingData } = usePermenantGet( + + const { data: existingData, mutate } = usePermenantGet( `/api/secure/exam-seating?id=${id}` ); + useEffect(() => { + if (!existingData) return; + setHallName(existingData.name); + setStructure(existingData.structure as SeatObjectType[][]); + }, [existingData]); + const onSubmit: React.MouseEventHandler = (e) => { e.preventDefault(); if (hallName === "") @@ -82,12 +86,17 @@ function EditHallForm({ id }: { id?: string }) { variant: "destructive", }); axios - .post("/api/secure/exam-seating", { name: hallName, structure }) + .put("/api/secure/exam-seating/hall", { + name: hallName, + structure, + id, + }) .then((res) => { toast({ title: "Well done!", description: `Class ${res.data.name} added successfully`, }); + mutate(); router.push("/dashboard/tools/exam-seating/"); }); }; diff --git a/src/app/dashboard/tools/exam-seating/hall/_components/classList.tsx b/src/app/dashboard/tools/exam-seating/hall/_components/classList.tsx index fe036d4..fd589d2 100644 --- a/src/app/dashboard/tools/exam-seating/hall/_components/classList.tsx +++ b/src/app/dashboard/tools/exam-seating/hall/_components/classList.tsx @@ -36,6 +36,7 @@ import axios from "axios"; import { mutate } from "swr"; import { Skeleton } from "@/components/ui/skeleton"; import { ExamHall } from "@prisma/client"; +import Link from "next/link"; export const columns: ColumnDef[] = [ { @@ -94,12 +95,16 @@ export const columns: ColumnDef[] = [ Actions - Edit + + Edit + axios - .delete("/api/secure/exam-seating", { + .delete("/api/secure/exam-seating/", { data: { id: row.original.id, }, diff --git a/src/components/Navigation.tsx b/src/components/Navigation.tsx index c2c947d..9d54b5b 100644 --- a/src/components/Navigation.tsx +++ b/src/components/Navigation.tsx @@ -183,7 +183,7 @@ export function NavBar() { Structure of desks and tables in each class