admin page for sr done

This commit is contained in:
Kalle (Sendou)
2020-12-19 19:18:27 +02:00
parent ac6b7b4936
commit 167ddc08a6
6 changed files with 91 additions and 10 deletions

View File

@@ -0,0 +1,43 @@
import { SALMON_RUN_ADMIN_DISCORD_IDS } from "lib/constants";
import { getMySession } from "lib/getMySession";
import { NextApiRequest, NextApiResponse } from "next";
import prisma from "prisma/client";
const salmonRunRecordIdHandler = async (
req: NextApiRequest,
res: NextApiResponse
) => {
const user = await getMySession(req);
if (!user || !SALMON_RUN_ADMIN_DISCORD_IDS.includes(user.discordId))
return res.status(401).end();
if (typeof req.query.id !== "string") return res.status(400).end();
const id = parseInt(req.query.id);
if (Number.isNaN(id)) return res.status(400).end();
switch (req.method) {
case "PATCH":
await patchHandler();
break;
case "DELETE":
await deleteHandler();
break;
default:
return res.status(405).end();
}
res.status(200).end();
async function patchHandler() {
await prisma.salmonRunRecord.update({
where: { id },
data: { approved: true },
});
}
async function deleteHandler() {
await prisma.salmonRunRecord.delete({ where: { id } });
}
};
export default salmonRunRecordIdHandler;

View File

@@ -65,6 +65,8 @@ const salmonRunRecordsHandler = async (
case "POST":
await postHandler(req, res);
break;
default:
return res.status(405).end();
}
};

View File

@@ -12,14 +12,19 @@ import {
} from "components/common/Table";
import WeaponImage from "components/common/WeaponImage";
import { SALMON_RUN_ADMIN_DISCORD_IDS } from "lib/constants";
import { sendData } from "lib/postData";
import useUser from "lib/useUser";
import { useRouter } from "next/router";
import { GetAllSalmonRunRecordsData } from "prisma/queries/getAllSalmonRunRecords";
import useSWR from "swr";
import { useState } from "react";
import useSWR, { mutate } from "swr";
import { salmonRunCategoryToNatural } from "./new";
const SalmonRunAdminPage = ({}) => {
const router = useRouter();
const [user, loading] = useUser();
const [sending, setSending] = useState(false);
const [recordsHidden, setRecordsHidden] = useState(new Set<number>());
const { data } = useSWR<GetAllSalmonRunRecordsData>(
"/api/sr/records?unapproved=true"
);
@@ -33,7 +38,23 @@ const SalmonRunAdminPage = ({}) => {
if (loading || !data) return null;
console.log("admin page data", data);
const handleClick = async (type: "DELETE" | "PATCH", id: number) => {
if (!user) {
console.error("Unexpected no logged in user");
return;
}
setSending(true);
const success = await sendData(type, `/api/sr/records/${id}`);
setSending(false);
if (!success) return;
mutate("/api/sr/records");
setRecordsHidden(new Set(Array.from(recordsHidden).concat(id)));
};
const records = data.filter((record) => !recordsHidden.has(record.id));
return (
<>
<Breadcrumbs
@@ -43,8 +64,8 @@ const SalmonRunAdminPage = ({}) => {
{ name: "Admin" },
]}
/>
{data.length === 0 ? (
<>no results</>
{records.length === 0 ? (
<>No results waiting for approval.</>
) : (
<Table>
<TableHead>
@@ -58,7 +79,7 @@ const SalmonRunAdminPage = ({}) => {
</TableRow>
</TableHead>
<TableBody>
{data.map((record) => {
{records.map((record) => {
return (
<TableRow key={record.id}>
<TableCell>{record.createdAt.toLocaleString()}</TableCell>
@@ -82,6 +103,8 @@ const SalmonRunAdminPage = ({}) => {
<TableCell>
{record.goldenEggCount} eggs
<br />
{salmonRunCategoryToNatural[record.category]}
<br />
{new Date(record.rotation.startTime).toLocaleDateString()}
<br />
{record.rotation.stage}
@@ -91,10 +114,23 @@ const SalmonRunAdminPage = ({}) => {
))}
</TableCell>
<TableCell>
<Button>Approve</Button>
<Button
onClick={() => handleClick("PATCH", record.id)}
disabled={sending}
size="sm"
>
Approve
</Button>
</TableCell>
<TableCell>
<Button colorScheme="red">Delete</Button>
<Button
onClick={() => handleClick("DELETE", record.id)}
colorScheme="red"
disabled={sending}
size="sm"
>
Delete
</Button>
</TableCell>
</TableRow>
);

View File

@@ -6,13 +6,12 @@ import Link from "next/link";
const SalmonRunLeaderboardsPage = ({}) => {
const { data, pendingCount } = useSalmonRunRecords();
console.log({ data, pendingCount });
return (
<>
<Breadcrumbs
pages={[{ name: t`Salmon Run` }, { name: t`Leaderboards` }]}
/>
{pendingCount && (
{pendingCount > 0 && (
<Alert status="info" my={4}>
<AlertIcon />
<Plural

View File

@@ -30,7 +30,7 @@ import { Controller, useForm } from "react-hook-form";
import { mutate } from "swr";
import * as z from "zod";
const salmonRunCategoryToNatural = {
export const salmonRunCategoryToNatural = {
TOTAL: t`All waves`,
TOTAL_NO_NIGHT: t`All waves (no night)`,
PRINCESS: t`Princess`,

View File

@@ -16,6 +16,7 @@ export const getAllSalmonRunRecords = async (
rotation: true,
roster: true,
},
orderBy: { createdAt: "desc" },
})
: prisma.salmonRunRecord.findMany({
where: { OR: [{ approved: true }, { submitterId: userId ?? -1 }] },