Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability for super admins to edit events #160

Merged
merged 9 commits into from
Feb 2, 2025
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