317 lines
15 KiB
TypeScript
317 lines
15 KiB
TypeScript
'use client'
|
|
|
|
import { useEffect, useState } from 'react'
|
|
import { getUsers, createUserByAdmin, toggleUserActive, deleteUser, resetUserPassword } from '@/app/actions'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Card, CardContent, CardHeader } from '@/components/ui/card'
|
|
import { Input } from '@/components/ui/input'
|
|
import { Loader2, Plus, KeyRound, Ban, Trash2, CheckCircle2, ShieldCheck, User } from 'lucide-react'
|
|
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
|
import { Label } from '@/components/ui/label'
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
|
|
|
export default function UsersPage() {
|
|
const [users, setUsers] = useState<any[]>([])
|
|
const [total, setTotal] = useState(0)
|
|
const [totalPages, setTotalPages] = useState(1)
|
|
const [page, setPage] = useState(1)
|
|
const [loading, setLoading] = useState(true)
|
|
|
|
// Modals
|
|
const [isCreateOpen, setIsCreateOpen] = useState(false)
|
|
const [isPassOpen, setIsPassOpen] = useState(false)
|
|
const [selectedUser, setSelectedUser] = useState<any>(null)
|
|
|
|
// Forms
|
|
const [name, setName] = useState('')
|
|
const [username, setUsername] = useState('')
|
|
const [password, setPassword] = useState('')
|
|
const [role, setRole] = useState('user')
|
|
const [newPass, setNewPass] = useState('')
|
|
|
|
const [actionLoading, setActionLoading] = useState(false)
|
|
|
|
useEffect(() => {
|
|
loadUsers(page)
|
|
}, [page])
|
|
|
|
const loadUsers = async (p: number) => {
|
|
setLoading(true)
|
|
const res = await getUsers(p, 10)
|
|
setUsers(res.users)
|
|
setTotal(res.total)
|
|
setTotalPages(res.totalPages)
|
|
setLoading(false)
|
|
}
|
|
|
|
const handleCreateUser = async (e: React.FormEvent) => {
|
|
e.preventDefault()
|
|
if (!name || !username || !password) return alert('Lengkapi semua field')
|
|
|
|
setActionLoading(true)
|
|
const res = await createUserByAdmin(name, username, password, role)
|
|
if (res.success) {
|
|
setIsCreateOpen(false)
|
|
setName('')
|
|
setUsername('')
|
|
setPassword('')
|
|
setRole('user')
|
|
loadUsers(page)
|
|
} else {
|
|
alert(res.error)
|
|
}
|
|
setActionLoading(false)
|
|
}
|
|
|
|
const handleResetPassword = async (e: React.FormEvent) => {
|
|
e.preventDefault()
|
|
if (!newPass || newPass.length < 6) return alert('Password minimal 6 karakter')
|
|
|
|
setActionLoading(true)
|
|
const res = await resetUserPassword(selectedUser.id, newPass)
|
|
if (res.success) {
|
|
setIsPassOpen(false)
|
|
setNewPass('')
|
|
alert('Password berhasil diubah')
|
|
} else {
|
|
alert(res.error)
|
|
}
|
|
setActionLoading(false)
|
|
}
|
|
|
|
const handleToggleActive = async (id: string, currentStatus: boolean) => {
|
|
if (!confirm(`Yakin ingin ${currentStatus ? 'menonaktifkan' : 'mengaktifkan'} user ini?`)) return
|
|
const res = await toggleUserActive(id, !currentStatus)
|
|
if (res.success) {
|
|
loadUsers(page)
|
|
} else {
|
|
alert(res.error)
|
|
}
|
|
}
|
|
|
|
const handleDelete = async (id: string) => {
|
|
if (!confirm('Yakin ingin menghapus user ini secara permanen?')) return
|
|
const res = await deleteUser(id)
|
|
if (res.success) {
|
|
loadUsers(page)
|
|
} else {
|
|
alert(res.error)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-6 animate-in fade-in duration-500 pb-12">
|
|
<div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4">
|
|
<div>
|
|
<h1 className="text-2xl font-black text-slate-900 dark:text-white">Manajemen Pengguna</h1>
|
|
<p className="text-sm text-slate-500 mt-1">Kelola data seluruh pengguna TitipIn.</p>
|
|
</div>
|
|
<Button onClick={() => setIsCreateOpen(true)} className="bg-[#1B2CC1] hover:bg-[#15229E] text-white rounded-xl h-11 px-5 shadow-md">
|
|
<Plus className="w-4 h-4 mr-2" /> Tambah User
|
|
</Button>
|
|
</div>
|
|
|
|
<Card className="rounded-3xl border border-slate-200/90 dark:border-slate-800 bg-white dark:bg-slate-900 overflow-hidden shadow-sm">
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-sm text-left">
|
|
<thead className="bg-slate-50/50 dark:bg-slate-800/50 border-b border-slate-100 dark:border-slate-800 text-xs uppercase font-bold text-slate-500">
|
|
<tr>
|
|
<th className="px-6 py-4">Pengguna</th>
|
|
<th className="px-6 py-4">Role</th>
|
|
<th className="px-6 py-4">Status</th>
|
|
<th className="px-6 py-4">Terdaftar</th>
|
|
<th className="px-6 py-4 text-right">Aksi</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-slate-100 dark:divide-slate-800">
|
|
{loading ? (
|
|
<tr>
|
|
<td colSpan={5} className="px-6 py-12 text-center">
|
|
<Loader2 className="w-6 h-6 animate-spin mx-auto text-[#1B2CC1]" />
|
|
</td>
|
|
</tr>
|
|
) : users.length === 0 ? (
|
|
<tr>
|
|
<td colSpan={5} className="px-6 py-12 text-center text-slate-500">Belum ada pengguna lain.</td>
|
|
</tr>
|
|
) : (
|
|
users.map(u => (
|
|
<tr key={u.id} className="hover:bg-slate-50/50 dark:hover:bg-slate-800/20 transition-colors">
|
|
<td className="px-6 py-4">
|
|
<div className="flex items-center gap-3">
|
|
{u.photo ? (
|
|
<img src={u.photo} alt={u.name} className="w-9 h-9 rounded-full object-cover shrink-0" />
|
|
) : (
|
|
<div className="w-9 h-9 rounded-full bg-blue-100 text-blue-700 flex items-center justify-center font-bold text-xs uppercase shrink-0">
|
|
{u.name.charAt(0)}
|
|
</div>
|
|
)}
|
|
<div>
|
|
<div className="font-bold text-slate-900 dark:text-slate-100">{u.name}</div>
|
|
<div className="text-xs text-slate-500">@{u.username}</div>
|
|
</div>
|
|
</div>
|
|
</td>
|
|
<td className="px-6 py-4">
|
|
{u.role === 'superadmin' ? (
|
|
<span className="inline-flex items-center gap-1 text-[10px] font-bold px-2 py-1 rounded-md bg-purple-100 text-purple-700 uppercase">
|
|
<ShieldCheck className="w-3 h-3" /> Superadmin
|
|
</span>
|
|
) : (
|
|
<span className="inline-flex items-center gap-1 text-[10px] font-bold px-2 py-1 rounded-md bg-slate-100 text-slate-700 uppercase">
|
|
<User className="w-3 h-3" /> User
|
|
</span>
|
|
)}
|
|
</td>
|
|
<td className="px-6 py-4">
|
|
{u.is_active ? (
|
|
<span className="inline-flex items-center gap-1 text-[10px] font-bold px-2 py-1 rounded-md bg-emerald-100 text-emerald-700 uppercase">
|
|
<CheckCircle2 className="w-3 h-3" /> Aktif
|
|
</span>
|
|
) : (
|
|
<span className="inline-flex items-center gap-1 text-[10px] font-bold px-2 py-1 rounded-md bg-red-100 text-red-700 uppercase">
|
|
<Ban className="w-3 h-3" /> Nonaktif
|
|
</span>
|
|
)}
|
|
</td>
|
|
<td className="px-6 py-4 text-slate-500 text-xs">
|
|
{new Date(u.created_at).toLocaleDateString('id-ID')}
|
|
</td>
|
|
<td className="px-6 py-4 text-right">
|
|
<div className="flex justify-end gap-2">
|
|
<Button
|
|
size="sm"
|
|
variant="ghost"
|
|
onClick={() => {
|
|
setSelectedUser(u)
|
|
setIsPassOpen(true)
|
|
}}
|
|
className="h-8 px-2 rounded-lg bg-slate-100 text-slate-700 hover:bg-slate-200 hover:text-slate-900 font-medium"
|
|
title="Ubah Password"
|
|
>
|
|
<KeyRound className="w-4 h-4" />
|
|
</Button>
|
|
|
|
{u.role !== 'superadmin' && (
|
|
<>
|
|
<Button
|
|
size="sm"
|
|
variant="ghost"
|
|
onClick={() => handleToggleActive(u.id, u.is_active)}
|
|
className={`h-8 px-2 rounded-lg font-medium ${u.is_active ? 'bg-amber-100 text-amber-700 hover:bg-amber-200 hover:text-amber-900' : 'bg-emerald-100 text-emerald-700 hover:bg-emerald-200 hover:text-emerald-900'}`}
|
|
title={u.is_active ? 'Nonaktifkan' : 'Aktifkan'}
|
|
>
|
|
{u.is_active ? <Ban className="w-4 h-4" /> : <CheckCircle2 className="w-4 h-4" />}
|
|
</Button>
|
|
<Button
|
|
size="sm"
|
|
variant="ghost"
|
|
onClick={() => handleDelete(u.id)}
|
|
className="h-8 px-2 rounded-lg bg-rose-100 text-rose-700 hover:bg-rose-200 hover:text-rose-900 font-medium"
|
|
title="Hapus"
|
|
>
|
|
<Trash2 className="w-4 h-4" />
|
|
</Button>
|
|
</>
|
|
)}
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
))
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
{totalPages > 1 && (
|
|
<div className="p-4 border-t border-slate-100 dark:border-slate-800 flex justify-between items-center bg-slate-50/50">
|
|
<span className="text-xs text-slate-500">Halaman {page} dari {totalPages}</span>
|
|
<div className="flex gap-2">
|
|
<Button disabled={page === 1} onClick={() => setPage(p => p - 1)} variant="outline" size="sm" className="h-8 rounded-lg text-xs">Sebelumnya</Button>
|
|
<Button disabled={page === totalPages} onClick={() => setPage(p => p + 1)} variant="outline" size="sm" className="h-8 rounded-lg text-xs">Selanjutnya</Button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</Card>
|
|
|
|
{/* CREATE MODAL */}
|
|
<Dialog open={isCreateOpen} onOpenChange={setIsCreateOpen}>
|
|
<DialogContent className="sm:max-w-md rounded-3xl p-0 overflow-hidden border-0">
|
|
<div className="px-6 pt-6 pb-4 border-b border-slate-100">
|
|
<DialogTitle className="text-xl font-black">Tambah User Baru</DialogTitle>
|
|
</div>
|
|
<form onSubmit={handleCreateUser} className="px-6 py-4 space-y-4">
|
|
<div className="space-y-1.5">
|
|
<Label className="text-xs font-bold uppercase text-slate-700">Nama Lengkap</Label>
|
|
<Input required value={name} onChange={e => setName(e.target.value)} className="h-11 rounded-xl" placeholder="Masukkan nama lengkap" />
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label className="text-xs font-bold uppercase text-slate-700">Username</Label>
|
|
<Input required value={username} onChange={e => setUsername(e.target.value)} className="h-11 rounded-xl" placeholder="Masukkan username unik" />
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label className="text-xs font-bold uppercase text-slate-700">Password</Label>
|
|
<Input required type="password" value={password} onChange={e => setPassword(e.target.value)} className="h-11 rounded-xl" placeholder="Buat kata sandi (min. 6 karakter)" />
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Label className="text-xs font-bold uppercase text-slate-700">Role Akses</Label>
|
|
<div className="grid grid-cols-2 gap-3">
|
|
<button
|
|
type="button"
|
|
onClick={() => setRole('user')}
|
|
className={`h-11 rounded-xl border flex items-center justify-center gap-2 text-sm font-bold transition-all ${
|
|
role === 'user'
|
|
? 'border-[#1B2CC1] bg-[#1B2CC1]/5 text-[#1B2CC1] ring-1 ring-[#1B2CC1]/20'
|
|
: 'border-slate-200 text-slate-500 hover:bg-slate-50'
|
|
}`}
|
|
>
|
|
<User className="w-4 h-4" /> User Biasa
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => setRole('superadmin')}
|
|
className={`h-11 rounded-xl border flex items-center justify-center gap-2 text-sm font-bold transition-all ${
|
|
role === 'superadmin'
|
|
? 'border-purple-500 bg-purple-50 text-purple-700 ring-1 ring-purple-500/20'
|
|
: 'border-slate-200 text-slate-500 hover:bg-slate-50'
|
|
}`}
|
|
>
|
|
<ShieldCheck className="w-4 h-4" /> Superadmin
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div className="pt-4 flex gap-3">
|
|
<Button type="button" variant="outline" onClick={() => setIsCreateOpen(false)} className="flex-1 h-11 rounded-xl">Batal</Button>
|
|
<Button type="submit" disabled={actionLoading} className="flex-1 h-11 rounded-xl bg-[#1B2CC1] hover:bg-[#15229E] text-white">
|
|
{actionLoading ? <Loader2 className="w-5 h-5 animate-spin" /> : 'Simpan User'}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
|
|
{/* RESET PASSWORD MODAL */}
|
|
<Dialog open={isPassOpen} onOpenChange={setIsPassOpen}>
|
|
<DialogContent className="sm:max-w-md rounded-3xl p-0 overflow-hidden border-0">
|
|
<div className="px-6 pt-6 pb-4 border-b border-slate-100">
|
|
<DialogTitle className="text-xl font-black">Ubah Password</DialogTitle>
|
|
<p className="text-xs text-slate-500 mt-1">Ubah password untuk user <span className="font-bold text-slate-900">@{selectedUser?.username}</span></p>
|
|
</div>
|
|
<form onSubmit={handleResetPassword} className="px-6 py-4 space-y-4">
|
|
<div className="space-y-1.5">
|
|
<Label className="text-xs font-bold uppercase text-slate-700">Password Baru</Label>
|
|
<Input required type="password" value={newPass} onChange={e => setNewPass(e.target.value)} className="h-11 rounded-xl" placeholder="Minimal 6 karakter" />
|
|
</div>
|
|
<div className="pt-4 flex gap-3">
|
|
<Button type="button" variant="outline" onClick={() => setIsPassOpen(false)} className="flex-1 h-11 rounded-xl">Batal</Button>
|
|
<Button type="submit" disabled={actionLoading} className="flex-1 h-11 rounded-xl bg-slate-900 hover:bg-slate-800 text-white">
|
|
{actionLoading ? <Loader2 className="w-5 h-5 animate-spin" /> : 'Simpan Password'}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</div>
|
|
)
|
|
}
|