From 20be21205bf6aab9b6f9e38d37dfb8d1ec4abc4b Mon Sep 17 00:00:00 2001 From: Aaron Delasy Date: Tue, 22 Oct 2024 02:57:52 +0300 Subject: [PATCH] Ability to delete maps from Play page --- app/header.tsx | 2 +- app/play/page.tsx | 31 +++++++++++++++++++++++++++---- convex/maps.ts | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/app/header.tsx b/app/header.tsx index 25c7948..9238bab 100644 --- a/app/header.tsx +++ b/app/header.tsx @@ -45,7 +45,7 @@ export default function Header() { return (
- Logo + Logo SurviveTheNight diff --git a/app/play/page.tsx b/app/play/page.tsx index 332da1e..bb8f0c5 100644 --- a/app/play/page.tsx +++ b/app/play/page.tsx @@ -1,7 +1,13 @@ "use client"; import { useEffect, useState } from "react"; -import { Authenticated, Unauthenticated, useQuery } from "convex/react"; +import { + Authenticated, + Unauthenticated, + useMutation, + useQuery, +} from "convex/react"; +import { TrashIcon } from "lucide-react"; import Link from "next/link"; import { Map as GameMap } from "@/components/Map"; import { Button } from "@/components/ui/button"; @@ -18,9 +24,11 @@ import { api } from "@/convex/_generated/api"; import { cn } from "@/lib/utils"; export default function PlayPage() { + const isAdmin = useQuery(api.users.isAdmin); const maps = useQuery(api.maps.getMaps, {}); const userMapResults = useQuery(api.playerresults.getUserMapStatus); const mapCountResults = useQuery(api.playerresults.getMapsWins); + const adminDeleteMapMutation = useMutation(api.maps.deleteMap); const [resMap, setResMap] = useState(new Map()); const [countMap, setCountMap] = useState(new Map()); @@ -128,9 +136,24 @@ export default function PlayPage() { )} > - - Night #{map.level} - +
+ + Night #{map.level} + + {isAdmin && ( + + )} +
diff --git a/convex/maps.ts b/convex/maps.ts index 547d599..9841252 100644 --- a/convex/maps.ts +++ b/convex/maps.ts @@ -257,6 +257,38 @@ export const rejectMap = adminMutationBuilder({ }, }); +export const deleteMap = adminMutationBuilder({ + args: { + mapId: v.id("maps"), + }, + handler: async (ctx, args) => { + const map = await ctx.db.get(args.mapId); + + if (map === null) { + return; + } + + await ctx.db.delete(args.mapId); + + if (map.level === undefined) { + return; + } + + const higherLevelMaps = await ctx.db + .query("maps") + .withIndex("by_level", (q) => q.gt("level", map.level)) + .collect(); + + await Promise.all( + higherLevelMaps.map(async (higherLevelMap) => { + return await ctx.db.patch(higherLevelMap._id, { + level: higherLevelMap.level! - 1, + }); + }), + ); + }, +}); + export const getMapByLevel = query({ args: { level: v.number() }, handler: async (ctx, args) => {