diff --git a/pages/api/sr/records/[id].ts b/pages/api/sr/records/[id].ts new file mode 100644 index 000000000..3fa97620d --- /dev/null +++ b/pages/api/sr/records/[id].ts @@ -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; diff --git a/pages/api/sr/records/index.ts b/pages/api/sr/records/index.ts index de1550eae..643f5500c 100644 --- a/pages/api/sr/records/index.ts +++ b/pages/api/sr/records/index.ts @@ -65,6 +65,8 @@ const salmonRunRecordsHandler = async ( case "POST": await postHandler(req, res); break; + default: + return res.status(405).end(); } }; diff --git a/pages/sr/leaderboards/admin.tsx b/pages/sr/leaderboards/admin.tsx index 0aeb66854..1a9d09ea7 100644 --- a/pages/sr/leaderboards/admin.tsx +++ b/pages/sr/leaderboards/admin.tsx @@ -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()); const { data } = useSWR( "/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 ( <> { { name: "Admin" }, ]} /> - {data.length === 0 ? ( - <>no results + {records.length === 0 ? ( + <>No results waiting for approval. ) : ( @@ -58,7 +79,7 @@ const SalmonRunAdminPage = ({}) => { - {data.map((record) => { + {records.map((record) => { return ( {record.createdAt.toLocaleString()} @@ -82,6 +103,8 @@ const SalmonRunAdminPage = ({}) => { {record.goldenEggCount} eggs
+ {salmonRunCategoryToNatural[record.category]} +
{new Date(record.rotation.startTime).toLocaleDateString()}
{record.rotation.stage} @@ -91,10 +114,23 @@ const SalmonRunAdminPage = ({}) => { ))}
- + - +
); diff --git a/pages/sr/leaderboards/index.tsx b/pages/sr/leaderboards/index.tsx index d14ce98de..7d5ae281b 100644 --- a/pages/sr/leaderboards/index.tsx +++ b/pages/sr/leaderboards/index.tsx @@ -6,13 +6,12 @@ import Link from "next/link"; const SalmonRunLeaderboardsPage = ({}) => { const { data, pendingCount } = useSalmonRunRecords(); - console.log({ data, pendingCount }); return ( <> - {pendingCount && ( + {pendingCount > 0 && (