import React, { useState, useEffect } from 'react'; import { supabase, createAdminClient } from '../supabase'; const DEPARTMENTS = ['AIDS', 'CSBS', 'CSE', 'CCE', 'MECH', 'VLSI', 'BIO-TECH', 'AIML', 'ECE', 'H&S']; const UserManagementView: React.FC = () => { const [formData, setFormData] = useState({ name: '', email: '', department: '', phone: '', role: 'Faculty', password: '', }); const [isSubmitting, setIsSubmitting] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(null); const [showPassword, setShowPassword] = useState(false); const [coordinators, setCoordinators] = useState([]); const [isLoadingList, setIsLoadingList] = useState(true); useEffect(() => { fetchCoordinators(); }, []); const fetchCoordinators = async () => { try { const { data: adminData, error: adminError } = await supabase .from('Facultyusers') .select('*') .order('name', { ascending: true }); if (adminError && !adminError.message.includes('not found')) throw adminError; // Sort by role: HOD first, then Faculty. Alphabetical within roles. const sorted = (adminData || []).sort((a, b) => { if (a.role === 'HOD' && b.role !== 'HOD') return -1; if (a.role !== 'HOD' && b.role === 'HOD') return 1; return a.name.localeCompare(b.name); }); setCoordinators(sorted); } catch (err) { console.error("Error fetching coordinators:", err); } finally { setIsLoadingList(false); } }; const generatePassword = () => { const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*"; let pass = ""; for (let i = 0; i < 12; i++) { pass += chars.charAt(Math.floor(Math.random() * chars.length)); } setFormData(prev => ({ ...prev, password: pass })); setShowPassword(true); }; const handleInputChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: value })); }; const handleDeleteUser = async (id: string, name: string, email: string) => { if (!window.confirm(`Are you sure you want to remove ${name}? This will delete their profile but they may still exist in Auth.`)) return; try { const { error } = await supabase.from('Facultyusers').delete().eq('id', id); if (error) throw error; setCoordinators(prev => prev.filter(c => c.id !== id)); } catch (err: any) { alert("Error deleting user: " + err.message); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsSubmitting(true); setError(null); setSuccess(null); try { // Use a separate client that doesn't persist sessions to avoid logging out the admin const adminClient = createAdminClient(); const { data, error: signUpError } = await adminClient.auth.signUp({ email: formData.email, password: formData.password, options: { data: { name: formData.name, role: formData.role === 'HOD' ? 'HOD' : 'COORDINATOR', department: formData.department, } } }); if (signUpError) throw signUpError; if (data.user) { const { error: adminDbError } = await supabase.from('Facultyusers').insert({ id: data.user.id, name: formData.name, email: formData.email, phone: formData.phone, department: formData.department, role: formData.role, updated_at: new Date().toISOString() }); if (adminDbError) throw adminDbError; setSuccess(`User ${formData.name} created successfully! Password: ${formData.password}`); setFormData({ name: '', email: '', department: '', phone: '', role: 'Faculty', password: '' }); fetchCoordinators(); } } catch (err: any) { console.error("User creation error:", err); setError(err.message || "Failed to create user."); } finally { setIsSubmitting(false); } }; return (

USER MANAGEMENT

Provision access for Event Coordinators

{/* Creation Card */}
{['Faculty', 'HOD'].map(r => ( ))}
{error && (
{error}
)} {success && (
{success}
)}
{/* Info Card */}

Security Protocol

Coordinators created here will have immediate access to the Event Coordinator Portal. They will be stored in the Facultyusers table.

    {[ 'Automatic role assignment (ADMIN)', 'Departmental scoping enabled', 'Profile synchronization active', 'Sign-up restricted for public' ].map((text, i) => (
  • {text}
  • ))}

Important Note

Creating a user via this interface will register them in Supabase Auth. Ensure the Gmail address is valid as it will be used for portal access.

EXISTING COORDINATORS

Manage active event coordinators

{isLoadingList ? (
) : coordinators.length === 0 ? (

No coordinators found

) : (
{coordinators.map((coord) => (

{coord.name}

{coord.department || coord.dept}

{coord.role || 'Faculty'}
{coord.email}
{coord.phone && (
{coord.phone}
)}
))}
)}
); }; export default UserManagementView;