413 lines
16 KiB
TypeScript
413 lines
16 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="p-6">
|
|
<div className="mb-6 flex flex-col items-start justify-between gap-4 sm:flex-row sm:items-center">
|
|
<div>
|
|
<h2 className="text-xl font-bold text-slate-900 dark:text-white">Manajemen Pengguna</h2>
|
|
<p className="mt-1 text-xs text-slate-500">Kelola data seluruh pengguna TitipIn.</p>
|
|
</div>
|
|
<Button
|
|
onClick={() => setIsCreateOpen(true)}
|
|
className="h-10 rounded-xl bg-[#1B2CC1] px-4 text-xs font-bold text-white shadow-sm hover:bg-[#15229E]"
|
|
>
|
|
<Plus className="mr-1.5 h-4 w-4" /> Tambah User
|
|
</Button>
|
|
</div>
|
|
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-left text-sm">
|
|
<thead className="border-b border-slate-100 bg-slate-50/50 text-xs font-bold text-slate-500 uppercase dark:border-slate-800 dark:bg-slate-800/50">
|
|
<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="mx-auto h-6 w-6 animate-spin 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="transition-colors hover:bg-slate-50/50 dark:hover:bg-slate-800/20"
|
|
>
|
|
<td className="px-6 py-4">
|
|
<div className="flex items-center gap-3">
|
|
{u.photo ? (
|
|
<img
|
|
src={u.photo}
|
|
alt={u.name}
|
|
className="h-9 w-9 shrink-0 rounded-full object-cover"
|
|
/>
|
|
) : (
|
|
<div className="flex h-9 w-9 shrink-0 items-center justify-center rounded-full bg-blue-100 text-xs font-bold text-blue-700 uppercase">
|
|
{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 rounded-md bg-purple-100 px-2 py-1 text-[10px] font-bold text-purple-700 uppercase">
|
|
<ShieldCheck className="h-3 w-3" /> Superadmin
|
|
</span>
|
|
) : (
|
|
<span className="inline-flex items-center gap-1 rounded-md bg-slate-100 px-2 py-1 text-[10px] font-bold text-slate-700 uppercase">
|
|
<User className="h-3 w-3" /> User
|
|
</span>
|
|
)}
|
|
</td>
|
|
<td className="px-6 py-4">
|
|
{u.is_active ? (
|
|
<span className="inline-flex items-center gap-1 rounded-md bg-emerald-100 px-2 py-1 text-[10px] font-bold text-emerald-700 uppercase">
|
|
<CheckCircle2 className="h-3 w-3" /> Aktif
|
|
</span>
|
|
) : (
|
|
<span className="inline-flex items-center gap-1 rounded-md bg-red-100 px-2 py-1 text-[10px] font-bold text-red-700 uppercase">
|
|
<Ban className="h-3 w-3" /> Nonaktif
|
|
</span>
|
|
)}
|
|
</td>
|
|
<td className="px-6 py-4 text-xs text-slate-500">
|
|
{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 rounded-lg bg-slate-100 px-2 font-medium text-slate-700 hover:bg-slate-200 hover:text-slate-900"
|
|
title="Ubah Password"
|
|
>
|
|
<KeyRound className="h-4 w-4" />
|
|
</Button>
|
|
|
|
{u.role !== 'superadmin' && (
|
|
<>
|
|
<Button
|
|
size="sm"
|
|
variant="ghost"
|
|
onClick={() => handleToggleActive(u.id, u.is_active)}
|
|
className={`h-8 rounded-lg px-2 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="h-4 w-4" />
|
|
) : (
|
|
<CheckCircle2 className="h-4 w-4" />
|
|
)}
|
|
</Button>
|
|
<Button
|
|
size="sm"
|
|
variant="ghost"
|
|
onClick={() => handleDelete(u.id)}
|
|
className="h-8 rounded-lg bg-rose-100 px-2 font-medium text-rose-700 hover:bg-rose-200 hover:text-rose-900"
|
|
title="Hapus"
|
|
>
|
|
<Trash2 className="h-4 w-4" />
|
|
</Button>
|
|
</>
|
|
)}
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
))
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
{totalPages > 1 && (
|
|
<div className="flex items-center justify-between border-t border-slate-100 bg-slate-50/50 p-4 dark:border-slate-800">
|
|
<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>
|
|
)}
|
|
|
|
{/* CREATE MODAL */}
|
|
<Dialog open={isCreateOpen} onOpenChange={setIsCreateOpen}>
|
|
<DialogContent className="overflow-hidden rounded-3xl border-0 p-0 sm:max-w-md">
|
|
<div className="border-b border-slate-100 px-6 pt-6 pb-4">
|
|
<DialogTitle className="text-xl font-black">Tambah User Baru</DialogTitle>
|
|
</div>
|
|
<form onSubmit={handleCreateUser} className="space-y-4 px-6 py-4">
|
|
<div className="space-y-1.5">
|
|
<Label className="text-xs font-bold text-slate-700 uppercase">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 text-slate-700 uppercase">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 text-slate-700 uppercase">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 text-slate-700 uppercase">Role Akses</Label>
|
|
<div className="grid grid-cols-2 gap-3">
|
|
<button
|
|
type="button"
|
|
onClick={() => setRole('user')}
|
|
className={`flex h-11 items-center justify-center gap-2 rounded-xl border 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="h-4 w-4" /> User Biasa
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => setRole('superadmin')}
|
|
className={`flex h-11 items-center justify-center gap-2 rounded-xl border 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="h-4 w-4" /> Superadmin
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div className="flex gap-3 pt-4">
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => setIsCreateOpen(false)}
|
|
className="h-11 flex-1 rounded-xl"
|
|
>
|
|
Batal
|
|
</Button>
|
|
<Button
|
|
type="submit"
|
|
disabled={actionLoading}
|
|
className="h-11 flex-1 rounded-xl bg-[#1B2CC1] text-white hover:bg-[#15229E]"
|
|
>
|
|
{actionLoading ? <Loader2 className="h-5 w-5 animate-spin" /> : 'Simpan User'}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
|
|
{/* RESET PASSWORD MODAL */}
|
|
<Dialog open={isPassOpen} onOpenChange={setIsPassOpen}>
|
|
<DialogContent className="overflow-hidden rounded-3xl border-0 p-0 sm:max-w-md">
|
|
<div className="border-b border-slate-100 px-6 pt-6 pb-4">
|
|
<DialogTitle className="text-xl font-black">Ubah Password</DialogTitle>
|
|
<p className="mt-1 text-xs text-slate-500">
|
|
Ubah password untuk user{' '}
|
|
<span className="font-bold text-slate-900">@{selectedUser?.username}</span>
|
|
</p>
|
|
</div>
|
|
<form onSubmit={handleResetPassword} className="space-y-4 px-6 py-4">
|
|
<div className="space-y-1.5">
|
|
<Label className="text-xs font-bold text-slate-700 uppercase">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="flex gap-3 pt-4">
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => setIsPassOpen(false)}
|
|
className="h-11 flex-1 rounded-xl"
|
|
>
|
|
Batal
|
|
</Button>
|
|
<Button
|
|
type="submit"
|
|
disabled={actionLoading}
|
|
className="h-11 flex-1 rounded-xl bg-slate-900 text-white hover:bg-slate-800"
|
|
>
|
|
{actionLoading ? <Loader2 className="h-5 w-5 animate-spin" /> : 'Simpan Password'}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</div>
|
|
)
|
|
}
|