Skip to content

Commit

Permalink
Add ability for super admins to edit events (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjanderson1227 authored Feb 2, 2025
1 parent 85bd265 commit 04af57e
Show file tree
Hide file tree
Showing 9 changed files with 441 additions and 7 deletions.
28 changes: 28 additions & 0 deletions apps/web/src/actions/admin/event-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use server";

import { superAdminAction } from "@/lib/safe-action";
import { newEventFormSchema as editEventFormSchema } from "@/validators/event";
import { editEvent as modifyEvent } from "db/functions";
import { revalidatePath } from "next/cache";

export const editEvent = superAdminAction
.schema(editEventFormSchema)
.action(async ({ parsedInput }) => {
const { id, ...options } = parsedInput;

if (id === undefined) {
throw new Error("The event's ID is not defined");
}

try {
await modifyEvent(id, options);
revalidatePath("/admin/events");
revalidatePath("/dash/schedule");
revalidatePath(`/schedule/${id}`);
} catch (e) {
console.error(e);
throw new Error(
"Event update failed. Check the server console for errors.",
);
}
});
34 changes: 34 additions & 0 deletions apps/web/src/app/admin/events/edit/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { getEventById } from "db/functions";
import { notFound } from "next/navigation";
import EditEventForm from "@/components/events/admin/EditEventForm";

export default async function EditEventPage({
params,
}: {
params: { slug: string };
}) {
const eventId = parseInt(params.slug);

if (!eventId) {
return notFound();
}

const event = await getEventById(eventId);

if (!event) {
return notFound();
}

return (
<div className="mx-auto max-w-3xl pt-32">
<div className="grid grid-cols-2">
<h1 className="text-3xl font-bold tracking-tight">
Edit Event
</h1>
</div>
<div className="mt-2 rounded-xl border border-muted p-5">
<EditEventForm {...event} />
</div>
</div>
);
}
19 changes: 17 additions & 2 deletions apps/web/src/app/admin/events/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
"use server";

import { EventDataTable } from "@/components/events/shared/EventDataTable";
import { columns } from "@/components/events/shared/EventColumns";
import { Button } from "@/components/shadcn/ui/button";
import { PlusCircle } from "lucide-react";
import Link from "next/link";
import { getAllEvents } from "db/functions";
import { getAllEvents, getUser } from "db/functions";
import { auth, redirectToSignIn } from "@clerk/nextjs/server";

export default async function Page() {
const { userId } = auth();
if (!userId) {
return redirectToSignIn();
}

const userData = await getUser(userId);
const isSuperAdmin = userData?.role === "super_admin";

const events = await getAllEvents();

return (
Expand All @@ -29,7 +41,10 @@ export default async function Page() {
</Link>
</div>
</div>
<EventDataTable columns={columns} data={events} />
<EventDataTable
columns={columns}
data={events.map((ev) => ({ ...ev, isSuperAdmin }))}
/>
</div>
);
}
Loading

0 comments on commit 04af57e

Please sign in to comment.