Skip to content

Commit

Permalink
feat: add update hall options
Browse files Browse the repository at this point in the history
  • Loading branch information
amjed-ali-k committed Oct 28, 2023
1 parent 0d95796 commit 14b043e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
29 changes: 29 additions & 0 deletions src/app/api/secure/exam-seating/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
11 changes: 11 additions & 0 deletions src/app/api/secure/exam-seating/student-batches/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -17,10 +16,8 @@ import {
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuPortal,
DropdownMenuSeparator,
DropdownMenuShortcut,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
Expand Down Expand Up @@ -69,10 +66,17 @@ function EditHallForm({ id }: { id?: string }) {

const { toast } = useToast();
const router = useRouter();
const { data: existingData } = usePermenantGet<ExamHall>(

const { data: existingData, mutate } = usePermenantGet<ExamHall>(
`/api/secure/exam-seating?id=${id}`
);

useEffect(() => {
if (!existingData) return;
setHallName(existingData.name);
setStructure(existingData.structure as SeatObjectType[][]);
}, [existingData]);

const onSubmit: React.MouseEventHandler<HTMLButtonElement> = (e) => {
e.preventDefault();
if (hallName === "")
Expand All @@ -82,12 +86,17 @@ function EditHallForm({ id }: { id?: string }) {
variant: "destructive",
});
axios
.post<ExamHall>("/api/secure/exam-seating", { name: hallName, structure })
.put<ExamHall>("/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/");
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExamHall>[] = [
{
Expand Down Expand Up @@ -94,12 +95,16 @@ export const columns: ColumnDef<ExamHall>[] = [
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuLabel>Actions</DropdownMenuLabel>
<DropdownMenuItem>Edit</DropdownMenuItem>
<Link
href={`/dashboard/tools/exam-seating/hall/${row.original.id}`}
>
<DropdownMenuItem>Edit</DropdownMenuItem>
</Link>
<DropdownMenuItem
className="text-red-500"
onClick={() =>
axios
.delete("/api/secure/exam-seating", {
.delete("/api/secure/exam-seating/", {
data: {
id: row.original.id,
},
Expand Down
2 changes: 1 addition & 1 deletion src/components/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export function NavBar() {
</NavigationMenuLink>
</li>
<ListItem
href="/dashboard/tools/exam-seating/class-list"
href="/dashboard/tools/exam-seating/hall"
title="Class layouts"
>
Structure of desks and tables in each class
Expand Down

0 comments on commit 14b043e

Please sign in to comment.