import React, { useCallback, useEffect, useState } from 'react'; import { api, errorMessage } from '../services/apiClient.ts'; import { useAuth } from '../context/AuthContext.tsx'; import type { User, UserStatus } from '../types/api.ts'; import { Banner, PageHeader, Spinner } from './ui/Feedback.tsx'; const inputClass = 'border border-gray-300 rounded-md px-3 py-2 text-sm w-full focus:ring-red-500 focus:border-red-500'; const ROLE_LABELS: Record = { inactive: 'Inactive', gm: 'GM', gm_admin: 'GM Admin', buh: 'BUH', bu_admin: 'BU Admin', director: 'Director', director_admin: 'Director Admin', account_admin: 'Account Admin', superuser: 'Superuser', }; const ASSIGNABLE_STATUSES: UserStatus[] = [ 'gm', 'gm_admin', 'buh', 'bu_admin', 'director', 'director_admin', 'account_admin', 'inactive', ]; const UserManagementPage: React.FC = () => { const { user: currentUser } = useAuth(); const [users, setUsers] = useState([]); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [busyId, setBusyId] = useState(null); const [error, setError] = useState(null); const [notice, setNotice] = useState(null); const [userLogin, setUserLogin] = useState(''); const [userName, setUserName] = useState(''); const [status, setStatus] = useState('gm'); const [password, setPassword] = useState(''); const [editId, setEditId] = useState(null); const [editStatus, setEditStatus] = useState('gm'); const [resetId, setResetId] = useState(null); const [resetPassword, setResetPassword] = useState(''); const reload = useCallback(async () => { setLoading(true); setError(null); try { setUsers(await api.users.list()); } catch (err) { setError(errorMessage(err)); } finally { setLoading(false); } }, []); useEffect(() => { void reload(); }, [reload]); const wrap = async (fn: () => Promise) => { setError(null); setNotice(null); try { await fn(); } catch (err) { setError(errorMessage(err)); } }; const onCreate = (e: React.FormEvent) => { e.preventDefault(); void wrap(async () => { setSaving(true); try { await api.users.create({ user_name: userLogin.trim(), display_name: userName.trim(), status, password, }); setNotice( `Akun "${userName.trim()}" dibuat. Bagikan password sementara secara aman (di luar sistem).` ); setUserLogin(''); setUserName(''); setPassword(''); setStatus('gm'); await reload(); } finally { setSaving(false); } }); }; const onSaveStatus = (id: number) => { void wrap(async () => { setBusyId(id); try { await api.users.update(id, { status: editStatus }); setNotice('Status pengguna diperbarui.'); setEditId(null); await reload(); } finally { setBusyId(null); } }); }; const onDeactivate = (user: User) => { if (!window.confirm(`Nonaktifkan akun "${user.display_name}"?`)) return; void wrap(async () => { setBusyId(user.id); try { await api.users.update(user.id, { status: 'inactive' }); setNotice(`Akun "${user.display_name}" dinonaktifkan.`); await reload(); } finally { setBusyId(null); } }); }; const onResetPassword = (id: number) => { void wrap(async () => { setBusyId(id); try { await api.users.setPassword(id, resetPassword); setNotice('Password direset. Bagikan password baru secara aman (di luar sistem).'); setResetId(null); setResetPassword(''); await reload(); } finally { setBusyId(null); } }); }; return (
{error && {error}} {notice && {notice}}

Buat pengguna baru

Password tidak dikirim lewat email. Bagikan secara terpisah ke pengguna.

Daftar pengguna

{loading ? (
) : (
{users.map((user) => { const isSelf = currentUser?.id === user.id; return ( ); })}
Nama Role Managed by Aksi
{user.display_name} {isSelf ? ( (Anda) ) : null}
{user.user_name}
{!isSelf && editId === user.id ? ( ) : ( ROLE_LABELS[user.status] ?? user.status )} {user.managed_by_name ?? '—'}
{!isSelf && editId === user.id ? ( <> ) : null} {!isSelf && editId !== user.id ? ( ) : null} {!isSelf && user.status !== 'inactive' ? ( ) : null} {isSelf ? ( Role & status akun sendiri tidak bisa diubah di sini. ) : null} {resetId === user.id ? (
setResetPassword(e.target.value)} autoComplete="new-password" />
) : ( )}
{users.length === 0 && (

Belum ada pengguna.

)}
)}
); }; export default UserManagementPage;