Skip to content

Commit

Permalink
Merge pull request #113 from webdevcody/feat-map-delete
Browse files Browse the repository at this point in the history
Ability to delete maps from Play page
  • Loading branch information
webdevcody authored Oct 22, 2024
2 parents 15cd47e + 20be212 commit 1e99338
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function Header() {
return (
<header className="flex items-center justify-between border-b px-6 py-4 shadow-sm">
<Link href="/" className="flex items-center">
<Image src="/logo.png" alt="Logo" width={32} height={32} />
<Image src="/logo.png" alt="Logo" width={32} height={32} priority />
<span className="ml-2 text-xl font-bold">SurviveTheNight</span>
</Link>

Expand Down
31 changes: 27 additions & 4 deletions app/play/page.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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());
Expand Down Expand Up @@ -128,9 +136,24 @@ export default function PlayPage() {
)}
>
<CardHeader>
<CardTitle className="text-center text-xl font-semibold">
Night #{map.level}
</CardTitle>
<div
className={`flex ${isAdmin ? "justify-between" : "justify-center"}`}
>
<CardTitle className="text-center text-xl font-semibold">
Night #{map.level}
</CardTitle>
{isAdmin && (
<Button
onClick={async () => {
await adminDeleteMapMutation({ mapId: map._id });
}}
size="icon"
variant="destructive"
>
<TrashIcon size={16} />
</Button>
)}
</div>
</CardHeader>
<CardContent className="flex flex-grow items-center justify-center">
<GameMap map={map.grid} size={52} />
Expand Down
32 changes: 32 additions & 0 deletions convex/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 1e99338

Please sign in to comment.