Conversation
sanjanamanivannan
commented
Mar 6, 2026
| const [startAt, setStartAt] = useState(""); | ||
| const [location, setLocation] = useState(""); | ||
|
|
||
| const [message, setMessage] = useState(""); |
There was a problem hiding this comment.
Track success vs error...
const [message, setMessage] = useState<{ text: string; isError: boolean } | null>(null);
Now have to update every setMessage(...) call (I will create a comment on each one to help)
|
|
||
| if (!res.ok) { | ||
| setEvents([]); | ||
| setMessage(data?.error || "Failed to load events"); |
There was a problem hiding this comment.
setMessage({ text: data?.error || "Failed to load events", isError: true });
| setEvents(Array.isArray(data) ? data : []); | ||
| } catch { | ||
| setEvents([]); | ||
| setMessage("Failed to load events"); |
There was a problem hiding this comment.
setMessage({ text: "Failed to load events", isError: true });
| return; | ||
| } | ||
|
|
||
| setMessage("Event created successfully"); |
There was a problem hiding this comment.
setMessage({ text: "Event created successfully", isError: false });
| const data = await res.json(); | ||
|
|
||
| if (!res.ok) { | ||
| setMessage(data?.error || "Failed to create event"); |
There was a problem hiding this comment.
setMessage({ text: data?.error || "Failed to create event", isError: true });
|
|
||
| async function createEvent(e: React.FormEvent) { | ||
| e.preventDefault(); | ||
| setMessage(""); |
|
|
||
| async function loadEvents() { | ||
| setLoading(true); | ||
| setMessage(""); |
| } | ||
| } | ||
|
|
||
| return ( |
There was a problem hiding this comment.
Something like this for the role checking, adding to what i added above:
{canCreate && (
<form onSubmit={createEvent} className="mt-6 space-y-4 max-w-md">
...
</form>
)}
{!canCreate && (
<p className="mt-6 text-sm text-gray-500">You do not have permission to create events.</p>
)}
|
|
||
| import { useEffect, useState } from "react"; | ||
| import AdminLayout from "@/components/admin/AdminLayout"; | ||
|
|
There was a problem hiding this comment.
Need to incorporate RBAC (Role-based access control). So incorporate useSession() and hasRole()
here add this:
import { useSession } from "next-auth/react";
import { hasRole } from "@/lib/rbac";
| }; | ||
|
|
||
| export default function AdminEventsPage() { | ||
| const [events, setEvents] = useState<Event[]>([]); |
There was a problem hiding this comment.
Continuing with the RBAC comments, add this right under AdminEventsPage() and keep the other stuff:
const { data: session } = useSession();
const canCreate = hasRole(session?.user?.role ?? "USER", "AMBASSADOR");