-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add admin dashboard, and ability to delete ratings
- Loading branch information
1 parent
e0274c7
commit 7519810
Showing
6 changed files
with
221 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export default function DocsLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
return ( | ||
<section className="flex flex-col items-center justify-center gap-4 max-h-[72vh] -mt-5 "> | ||
<div className="inline-block text-center justify-center overflow-scroll rounded-lg scrollbar-hide"> | ||
{children} | ||
</div> | ||
</section> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
"use client"; | ||
|
||
import useSWR from "swr"; | ||
import { useState } from "react"; | ||
import React from "react"; | ||
import { signIn, signOut, useSession } from "next-auth/react"; | ||
import { | ||
Table, | ||
TableHeader, | ||
TableColumn, | ||
TableBody, | ||
TableRow, | ||
TableCell, | ||
getKeyValue, | ||
Dropdown, | ||
DropdownTrigger, | ||
Button, | ||
DropdownMenu, | ||
DropdownItem, | ||
} from "@nextui-org/react"; | ||
|
||
import { MoreVert } from "@mui/icons-material"; | ||
import axios from "axios"; | ||
|
||
export default function AdminPage() { | ||
const { data: session, status } = useSession(); | ||
const fetcher = (url: any) => fetch(url).then((r) => r.json()); | ||
|
||
const { | ||
data: ratings, | ||
isLoading, | ||
error, | ||
} = useSWR("/api/getRatings", fetcher, { refreshInterval: 5000 }); | ||
let columns = []; | ||
let filtered_ratings: any = []; | ||
|
||
if (!isLoading) { | ||
for (let rating of ratings) { | ||
const name = rating.User?.name; | ||
const email = rating.User?.email; | ||
rating.name = name; | ||
rating.email = email; | ||
|
||
filtered_ratings.push(rating); | ||
} | ||
|
||
if (filtered_ratings.length > 0) { | ||
for (let key in filtered_ratings[0]) { | ||
if (key != "User") { | ||
columns.push({ key: key, label: key }); | ||
} | ||
} | ||
} | ||
columns.push({ key: "actions", value: "actions" }); | ||
} | ||
|
||
async function deleteRating(ratingID: any) { | ||
console.log("Rating Gone, finito, finished"); | ||
await axios | ||
.post("/api/deleteRating", { | ||
ratingID: ratingID, | ||
}) | ||
.then(function (response) { | ||
// Handle response | ||
console.log(response); | ||
}) | ||
.catch(function (error) { | ||
console.log(error); | ||
}); | ||
} | ||
|
||
const renderCell = React.useCallback((user: any, columnKey: React.Key) => { | ||
const cellValue = user[columnKey as keyof any]; | ||
|
||
switch (columnKey) { | ||
case "actions": | ||
return ( | ||
<div className="relative flex justify-end items-center gap-2"> | ||
<Dropdown> | ||
<DropdownTrigger> | ||
<Button isIconOnly size="sm" variant="light"> | ||
<MoreVert className="text-default-300" /> | ||
</Button> | ||
</DropdownTrigger> | ||
<DropdownMenu> | ||
<DropdownItem onClick={() => deleteRating(user.id)}> | ||
Delete | ||
</DropdownItem> | ||
</DropdownMenu> | ||
</Dropdown> | ||
</div> | ||
); | ||
default: | ||
return cellValue; | ||
} | ||
}, []); | ||
|
||
if (status === "authenticated") { | ||
// @ts-ignore | ||
if (session.user?.role === "admin") { | ||
return ( | ||
<div className="w-[90vw]"> | ||
<Table | ||
isHeaderSticky | ||
className="overflow-scroll" | ||
fullWidth | ||
aria-label="Rating table with dynamic content(ratings)" | ||
> | ||
<TableHeader columns={columns}> | ||
{(column) => ( | ||
<TableColumn key={column.key}>{column.label}</TableColumn> | ||
)} | ||
</TableHeader> | ||
<TableBody items={filtered_ratings}> | ||
{(item: any) => ( | ||
<TableRow key={item.id}> | ||
{(columnKey) => ( | ||
<TableCell>{renderCell(item, columnKey)}</TableCell> | ||
)} | ||
</TableRow> | ||
)} | ||
</TableBody> | ||
</Table> | ||
</div> | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// api/test.ts | ||
import { NextResponse, NextRequest } from "next/server"; | ||
|
||
import prisma from "../../../lib/prisma"; | ||
import { auth } from "../../../lib/auth"; | ||
import { getPlanCookie } from "../../../app/actions"; | ||
|
||
export async function POST(request: NextRequest) { | ||
const data = await request.json(); | ||
|
||
const ratingID = data.ratingID; | ||
|
||
const courses = await prisma.rating.delete({ | ||
where: { | ||
id: parseInt(ratingID), | ||
}, | ||
}); | ||
|
||
return NextResponse.json(courses, { status: 200 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { NextResponse, NextRequest } from "next/server"; | ||
|
||
import prisma from "../../../lib/prisma"; | ||
|
||
export async function GET(request: NextRequest) { | ||
const profs = await prisma.rating.findMany({ | ||
include: { | ||
User: { | ||
select: { | ||
name: true, | ||
email: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
return NextResponse.json(profs, { status: 200 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters