fmproService / frontend /src /components /UsersManagement.tsx
Hkoradiya123
feat: Initialize frontend with Zustand for state management, Tailwind CSS for styling, and TypeScript for type safety
4854e2b
Raw History Blame Contribute Delete
5.33 kB
/**
* Users Management Component
*/
import React, { useState } from 'react';
import { User } from '../types';
import { useAdminStore } from '../store';
interface UsersManagementProps {
users: User[];
}
export const UsersManagement: React.FC<UsersManagementProps> = ({ users }) => {
const deleteUser = useAdminStore((state) => state.deleteUser);
const [selectedUserId, setSelectedUserId] = useState<string | null>(null);
const [showConfirm, setShowConfirm] = useState(false);
const handleDelete = async () => {
if (selectedUserId) {
await deleteUser(selectedUserId);
setShowConfirm(false);
setSelectedUserId(null);
}
};
const userToDelete = users.find((u) => u.id === selectedUserId);
return (
<div className="space-y-4">
{users.length === 0 ? (
<div className="rounded-md border border-dashed border-[color:var(--line)] bg-white/70 py-12 text-center text-[color:var(--muted)]">
<p className="font-bold text-[color:var(--ink)]">No users found</p>
</div>
) : (
<>
<div className="overflow-x-auto">
<table className="data-table">
<thead>
<tr>
<th className="px-4 py-3 text-left text-sm font-semibold text-gray-700">
Email
</th>
<th className="px-4 py-3 text-left text-sm font-semibold text-gray-700">
Full Name
</th>
<th className="px-4 py-3 text-left text-sm font-semibold text-gray-700">
Role
</th>
<th className="px-4 py-3 text-left text-sm font-semibold text-gray-700">
Status
</th>
<th className="px-4 py-3 text-left text-sm font-semibold text-gray-700">
Created At
</th>
<th className="px-4 py-3 text-right text-sm font-semibold text-gray-700">
Actions
</th>
</tr>
</thead>
<tbody className="divide-y divide-gray-200">
{users.map((user) => (
<tr key={user.id}>
<td className="px-4 py-3 text-sm font-medium text-gray-900">
{user.email}
</td>
<td className="px-4 py-3 text-sm text-gray-700">
{user.full_name || 'N/A'}
</td>
<td className="px-4 py-3 text-sm">
<span
className={`status-pill ${
user.role === 'admin'
? 'bg-red-50 text-red-800'
: 'bg-teal-50 text-teal-800'
}`}
>
{user.role.toUpperCase()}
</span>
</td>
<td className="px-4 py-3 text-sm">
<span
className={`status-pill ${
user.is_active
? 'bg-green-50 text-green-800'
: 'bg-gray-100 text-gray-800'
}`}
>
{user.is_active ? 'Active' : 'Inactive'}
</span>
</td>
<td className="px-4 py-3 text-sm text-gray-600">
{new Date(user.created_at).toLocaleDateString()}
</td>
<td className="px-4 py-3 text-right">
<button
onClick={() => {
setSelectedUserId(user.id);
setShowConfirm(true);
}}
className="font-bold text-[color:var(--danger)] hover:underline"
>
Delete
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
{/* Confirmation Modal */}
{showConfirm && userToDelete && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 px-4">
<div className="panel w-full max-w-sm bg-white p-6 shadow-2xl">
<h3 className="mb-4 text-lg font-black text-[color:var(--ink)]">Confirm Delete</h3>
<p className="mb-4 text-[color:var(--muted)]">
Are you sure you want to delete <strong>{userToDelete.email}</strong>? This action
cannot be undone.
</p>
<div className="flex gap-3">
<button
onClick={() => setShowConfirm(false)}
className="btn-quiet flex-1 px-4 py-2"
>
Cancel
</button>
<button
onClick={handleDelete}
className="btn-danger flex-1 px-4 py-2"
>
Delete
</button>
</div>
</div>
</div>
)}
</>
)}
</div>
);
};