150 lines
5.0 KiB
TypeScript
150 lines
5.0 KiB
TypeScript
import React, { useState, useEffect } from 'react';
|
|
import { User } from '../../types';
|
|
import { useAppContext } from '../../context/AppContext';
|
|
|
|
interface UserFormProps {
|
|
isOpen: boolean;
|
|
onClose: () => void;
|
|
onSave: (userData: Omit<User, 'id'>, id?: string) => void;
|
|
userToEdit: User | null;
|
|
}
|
|
|
|
const UserForm: React.FC<UserFormProps> = ({ isOpen, onClose, onSave, userToEdit }) => {
|
|
const { groups, roles } = useAppContext();
|
|
const [name, setName] = useState('');
|
|
const [email, setEmail] = useState('');
|
|
const [groupId, setGroupId] = useState(groups[0]?.id || '');
|
|
const [roleId, setRoleId] = useState(roles[0]?.id || '');
|
|
|
|
useEffect(() => {
|
|
if (userToEdit) {
|
|
setName(userToEdit.name);
|
|
setEmail(userToEdit.email);
|
|
setGroupId(userToEdit.groupId);
|
|
setRoleId(userToEdit.roleId);
|
|
} else {
|
|
setName('');
|
|
setEmail('');
|
|
setGroupId(groups[0]?.id || '');
|
|
setRoleId(roles[0]?.id || '');
|
|
}
|
|
}, [userToEdit, groups, roles]);
|
|
|
|
const handleSubmit = (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
onSave({ name, email, groupId, roleId }, userToEdit?.id);
|
|
};
|
|
|
|
if (!isOpen) return null;
|
|
|
|
return (
|
|
<div
|
|
className="fixed inset-0 bg-black bg-opacity-60 flex justify-center items-center z-50 p-4"
|
|
onClick={onClose}
|
|
>
|
|
<div
|
|
className="bg-white rounded-xl shadow-2xl w-full max-w-lg"
|
|
onClick={(e) => e.stopPropagation()}
|
|
>
|
|
<form onSubmit={handleSubmit}>
|
|
<div className="p-6">
|
|
<div className="flex justify-between items-center mb-6">
|
|
<h2 className="text-2xl font-bold text-gray-800">
|
|
{userToEdit ? 'Edit Pengguna' : 'Tambah Pengguna Baru'}
|
|
</h2>
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
className="text-gray-400 hover:text-gray-600 text-2xl"
|
|
>
|
|
×
|
|
</button>
|
|
</div>
|
|
<div className="space-y-4">
|
|
<div>
|
|
<label htmlFor="name" className="block text-sm font-medium text-gray-700 mb-1">
|
|
Nama Lengkap
|
|
</label>
|
|
<input
|
|
type="text"
|
|
id="name"
|
|
value={name}
|
|
onChange={(e) => setName(e.target.value)}
|
|
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm"
|
|
required
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label htmlFor="email" className="block text-sm font-medium text-gray-700 mb-1">
|
|
Alamat Email
|
|
</label>
|
|
<input
|
|
type="email"
|
|
id="email"
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm"
|
|
required
|
|
/>
|
|
</div>
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
<div>
|
|
<label htmlFor="group" className="block text-sm font-medium text-gray-700 mb-1">
|
|
Grup
|
|
</label>
|
|
<select
|
|
id="group"
|
|
value={groupId}
|
|
onChange={(e) => setGroupId(e.target.value)}
|
|
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm bg-white"
|
|
>
|
|
{groups.map((g) => (
|
|
<option key={g.id} value={g.id}>
|
|
{g.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
<div>
|
|
<label htmlFor="role" className="block text-sm font-medium text-gray-700 mb-1">
|
|
Peran
|
|
</label>
|
|
<select
|
|
id="role"
|
|
value={roleId}
|
|
onChange={(e) => setRoleId(e.target.value)}
|
|
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm bg-white"
|
|
>
|
|
{roles.map((r) => (
|
|
<option key={r.id} value={r.id}>
|
|
{r.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div className="bg-gray-50 px-6 py-4 rounded-b-xl flex justify-end space-x-3">
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
className="bg-gray-200 text-gray-700 px-4 py-2 rounded-lg hover:bg-gray-300 font-medium"
|
|
>
|
|
Batal
|
|
</button>
|
|
<button
|
|
type="submit"
|
|
className="bg-red-600 text-white px-4 py-2 rounded-lg hover:bg-red-700 font-medium shadow-sm"
|
|
>
|
|
Simpan
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default UserForm;
|