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

Added approve/reject methods for communities in Super Admin Page #278

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 107 additions & 16 deletions src/app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,25 @@ interface Event {
is_approved: boolean | null; // Include the approval status
}

interface Community {
id: string;
community_name: string;
community_description: string;
community_location: string;
community_category: string;
community_members_count: number;
community_image: string;
community_creation_date: string;
is_approved: boolean | null; // Include the approval status
}

export default function Admin() {
const router = useRouter();
const { isAuthenticated, isLoading } = useKindeBrowserClient();

const [user, setUser] = useState<User | null>(null);
const [events, setEvents] = useState<Event[]>([]);
const [communities, setCommunities] = useState<Community[]>([]); // State to hold communities
const [loading, setLoading] = useState(true);

useEffect(() => {
Expand All @@ -44,23 +57,34 @@ export default function Admin() {
}, []);

useEffect(() => {
const organizeEvents = async () => {
const fetchEventsAndCommunities = async () => {
if (!user) return;

setLoading(true);
const { data: organised_events, error } = await supabase

// Fetch events
const { data: organised_events, error: eventError } = await supabase
.from("event_details")
.select("*");
// .eq("organizer_email", user.email);

if (error) {
console.log(error);
if (eventError) {
console.log(eventError);
}

setEvents(organised_events || []);

// Fetch communities
const { data: communities_data, error: communityError } = await supabase
.from("communities") // Assuming you have a 'communities' table
.select("*");
if (communityError) {
console.log(communityError);
}

setCommunities(communities_data || []);
setLoading(false);
};

organizeEvents();
fetchEventsAndCommunities();
}, [user]);

const handleEventStatusChange = async (
Expand All @@ -75,7 +99,6 @@ export default function Admin() {
if (error) {
console.error(`Error changing event approval status:`, error);
} else {
// Update local state to reflect the change
setEvents((prevEvents) =>
prevEvents.map((event) =>
event.id === eventId ? { ...event, is_approved: isApproved } : event
Expand All @@ -87,6 +110,32 @@ export default function Admin() {
}
};

const handleCommunityStatusChange = async (
communityId: string,
isApproved: boolean
) => {
const { error } = await supabase
.from("communities")
.update({ is_approved: isApproved })
.eq("id", communityId);

if (error) {
console.error(`Error changing community approval status:`, error);
} else {
// Update local state to reflect the change
setCommunities((prevCommunities) =>
prevCommunities.map((community) =>
community.id === communityId
? { ...community, is_approved: isApproved }
: community
)
);
toast.success(
`Community ${isApproved ? "approved" : "rejected"} successfully.`
);
}
};

if (isLoading) {
return <Loading />;
}
Expand All @@ -99,27 +148,28 @@ export default function Admin() {
<div className="w-full h-auto bg-black text-white py-[8rem] px-4 flex flex-col">
<div className="flex flex-col justify-center items-center gap-4">
<h1 className="text-3xl font-bold text-center my-10">
Administer Events
Administer Events & Communities
</h1>

{/* Events Section */}
<div className="w-full flex flex-wrap gap-6 justify-center">
<h2 className="text-xl font-semibold w-full text-center mb-6">
Events
</h2>
{events.map((event) => (
<Card
key={event.id}
className="bg-black w-full flex flex-col h-[auto]"
>
<Card key={event.id} className="bg-black w-full flex flex-col h-[auto]">
<CardHeader>
<CardTitle>{event.event_title}</CardTitle>
</CardHeader>
<CardContent className="flex-grow flex flex-col justify-between">
<p className="flex-grow mb-4">{event.event_description}</p>
{/* Display the current approval status */}
<p className="mb-4">
Status:{" "}
{event.is_approved === null
? "Pending"
: event.is_approved
? "Approved"
: "Rejected"}
? "Approved"
: "Rejected"}
</p>
<div className="flex space-x-4">
<Button
Expand All @@ -141,6 +191,47 @@ export default function Admin() {
</Card>
))}
</div>

{/* Communities Section */}
<div className="w-full flex flex-wrap gap-6 justify-center mt-12">
<h2 className="text-xl font-semibold w-full text-center mb-6">
Communities
</h2>
{communities.map((community) => (
<Card key={community.id} className="bg-black w-full flex flex-col h-[auto]">
<CardHeader>
<CardTitle>{community.community_name}</CardTitle>
</CardHeader>
<CardContent className="flex-grow flex flex-col justify-between">
<p className="flex-grow mb-4">{community.community_description}</p>
<p className="mb-4">
Status:{" "}
{community.is_approved === null
? "Pending"
: community.is_approved
? "Approved"
: "Rejected"}
</p>
<div className="flex space-x-4">
<Button
variant="outline"
className="w-32 bg-transparent hover:bg-green-500"
onClick={() => handleCommunityStatusChange(community.id, true)} // Approve
>
Approve
</Button>
<Button
variant="outline"
className="w-32 bg-transparent hover:bg-red-500"
onClick={() => handleCommunityStatusChange(community.id, false)} // Reject
>
Reject
</Button>
</div>
</CardContent>
</Card>
))}
</div>
</div>
</div>
)}
Expand Down