"use client"; import { useEffect, useState } from "react"; import { supabaseBrowser } from "@/utils/supabase/client"; interface UserProfile { id: string; full_name: string; role: string; } interface Contract { id: string; title: string; price: number; status: string; freelancer_name: string; client_name: string; } export default function AdminDashboard() { const [profiles, setProfiles] = useState([]); const [contracts, setContracts] = useState([]); const [loading, setLoading] = useState(true); const supabase = supabaseBrowser(); useEffect(() => { const fetchAdminData = async () => { const { data: { user }, } = await supabase.auth.getUser(); if (!user) { setLoading(false); return; } // check if admin const { data: profile } = await supabase .from("profiles") .select("role") .eq("id", user.id) .single(); if (!profile || profile.role !== "admin") { alert("Access denied. Admins only."); return; } await loadUsers(); await loadContracts(); setLoading(false); }; fetchAdminData(); }, []); const loadUsers = async () => { const { data: users, error } = await supabase .from("profiles") .select("id, full_name, role"); if (error) console.error(error); setProfiles(users || []); }; const loadContracts = async () => { const { data: contractsData, error } = await supabase .from("contracts") .select("id, title, price, status, freelancer_name, client_name"); if (error) console.error(error); setContracts(contractsData || []); }; // 🔹 Delete User const deleteUser = async (userId: string) => { if (!confirm("Are you sure you want to delete this user?")) return; const { error } = await supabase.from("profiles").delete().eq("id", userId); if (error) { console.error("Error deleting user:", error); alert("Failed to delete user"); } else { alert("User deleted successfully"); loadUsers(); } }; // 🔹 Update Role const updateUserRole = async (userId: string, newRole: string) => { const { error } = await supabase .from("profiles") .update({ role: newRole }) .eq("id", userId); if (error) { console.error("Error updating role:", error); alert("Failed to update role"); } else { alert("Role updated successfully"); loadUsers(); } }; // 🔹 Force Close Contract const closeContract = async (contractId: string) => { const { error } = await supabase .from("contracts") .update({ status: "closed" }) .eq("id", contractId); if (error) { console.error("Error closing contract:", error); alert("Failed to close contract"); } else { alert("Contract closed successfully"); loadContracts(); } }; if (loading) return

Loading admin data...

; return (

Admin Dashboard

{/* Users Section */}

All Users

{profiles.map((profile) => ( ))}
ID Full Name Role Actions
{profile.id} {profile.full_name} {profile.role} {/* Delete Button */} {/* Role Update Dropdown */}
{/* Contracts Section */}

All Contracts

{contracts.map((c) => ( ))}
Title Price Freelancer Client Status Actions
{c.title} ₹{c.price} {c.freelancer_name} {c.client_name} {c.status} {c.status !== "closed" && ( )}
); }