113 lines
4.2 KiB
TypeScript
113 lines
4.2 KiB
TypeScript
import React, { useState } from 'react';
|
|
import { User } from '../../types';
|
|
import { useAppContext } from '../../context/AppContext';
|
|
import UserForm from './UserForm';
|
|
import { userAvatars } from '../auth/PersonaSelector.tsx';
|
|
|
|
const UserTab: React.FC = () => {
|
|
const { users, groups, roles, onAddUser, onUpdateUser, onDeleteUser } = useAppContext();
|
|
const [isFormOpen, setIsFormOpen] = useState(false);
|
|
const [editingUser, setEditingUser] = useState<User | null>(null);
|
|
|
|
const handleAddNew = () => {
|
|
setEditingUser(null);
|
|
setIsFormOpen(true);
|
|
};
|
|
|
|
const handleEdit = (user: User) => {
|
|
setEditingUser(user);
|
|
setIsFormOpen(true);
|
|
};
|
|
|
|
const handleSave = (userData: Omit<User, 'id'>, id?: string) => {
|
|
if (id) {
|
|
onUpdateUser({ ...userData, id });
|
|
} else {
|
|
onAddUser(userData);
|
|
}
|
|
setIsFormOpen(false);
|
|
};
|
|
|
|
const findGroupName = (id: string) => groups.find((g) => g.id === id)?.name || 'N/A';
|
|
const findRoleName = (id: string) => roles.find((r) => r.id === id)?.name || 'N/A';
|
|
|
|
return (
|
|
<div className="bg-white p-6 rounded-xl shadow-sm">
|
|
<div className="flex justify-between items-center mb-4">
|
|
<h2 className="text-xl font-bold text-gray-800">Daftar Pengguna</h2>
|
|
<button
|
|
onClick={handleAddNew}
|
|
className="flex items-center space-x-2 bg-red-600 text-white px-4 py-2 rounded-lg hover:bg-red-700 transition duration-300 shadow-sm font-semibold text-sm"
|
|
>
|
|
<i className="fa-solid fa-plus"></i>
|
|
<span>Tambah Pengguna</span>
|
|
</button>
|
|
</div>
|
|
<div className="overflow-x-auto">
|
|
<table className="min-w-full text-sm">
|
|
<thead className="bg-gray-100 text-left text-gray-600">
|
|
<tr>
|
|
<th className="p-3 font-semibold">Nama</th>
|
|
<th className="p-3 font-semibold">Email</th>
|
|
<th className="p-3 font-semibold">Grup</th>
|
|
<th className="p-3 font-semibold">Peran</th>
|
|
<th className="p-3 font-semibold text-right">Aksi</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-gray-100">
|
|
{users.map((user) => {
|
|
const avatarUrl =
|
|
userAvatars[user.id] || 'https://randomuser.me/api/portraits/lego/1.jpg';
|
|
return (
|
|
<tr key={user.id} className="hover:bg-gray-50">
|
|
<td className="p-3 font-semibold text-gray-800">
|
|
<div className="flex items-center space-x-3">
|
|
<img
|
|
src={avatarUrl}
|
|
alt={user.name}
|
|
className="w-10 h-10 rounded-full object-cover"
|
|
/>
|
|
<span>{user.name}</span>
|
|
</div>
|
|
</td>
|
|
<td className="p-3 text-gray-600 align-middle">{user.email}</td>
|
|
<td className="p-3 text-gray-600 align-middle">{findGroupName(user.groupId)}</td>
|
|
<td className="p-3 align-middle">
|
|
<span className="bg-blue-100 text-blue-700 px-2 py-0.5 rounded-full font-medium">
|
|
{findRoleName(user.roleId)}
|
|
</span>
|
|
</td>
|
|
<td className="p-3 text-right align-middle">
|
|
<button
|
|
onClick={() => handleEdit(user)}
|
|
className="text-gray-500 hover:text-blue-600 mr-4"
|
|
>
|
|
<i className="fa-solid fa-pencil"></i> Edit
|
|
</button>
|
|
<button
|
|
onClick={() => onDeleteUser(user.id)}
|
|
className="text-gray-500 hover:text-red-600"
|
|
>
|
|
<i className="fa-solid fa-trash"></i> Hapus
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
);
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
{isFormOpen && (
|
|
<UserForm
|
|
isOpen={isFormOpen}
|
|
onClose={() => setIsFormOpen(false)}
|
|
onSave={handleSave}
|
|
userToEdit={editingUser}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default UserTab;
|