140 lines
3.9 KiB
TypeScript
140 lines
3.9 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import {
|
|
suspendUserAction,
|
|
unsuspendUserAction,
|
|
} from "@/features/admin/actions";
|
|
|
|
interface SuspendUserButtonProps {
|
|
userId: string;
|
|
isSuspended: boolean;
|
|
}
|
|
|
|
export function SuspendUserButton({
|
|
userId,
|
|
isSuspended,
|
|
}: SuspendUserButtonProps) {
|
|
const router = useRouter();
|
|
const [open, setOpen] = useState(false);
|
|
const [reason, setReason] = useState("");
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState("");
|
|
|
|
async function handleSuspend() {
|
|
setLoading(true);
|
|
setError("");
|
|
const res = await suspendUserAction(userId, reason);
|
|
setLoading(false);
|
|
if ("error" in res && res.error) {
|
|
setError(res.error);
|
|
return;
|
|
}
|
|
setOpen(false);
|
|
setReason("");
|
|
router.refresh();
|
|
}
|
|
|
|
async function handleUnsuspend() {
|
|
if (!confirm("Buka kembali akun ini? User akan langsung bisa login.")) {
|
|
return;
|
|
}
|
|
setLoading(true);
|
|
setError("");
|
|
const res = await unsuspendUserAction(userId);
|
|
setLoading(false);
|
|
if ("error" in res && res.error) {
|
|
setError(res.error);
|
|
return;
|
|
}
|
|
router.refresh();
|
|
}
|
|
|
|
if (isSuspended) {
|
|
return (
|
|
<div className="space-y-2">
|
|
<button
|
|
type="button"
|
|
onClick={handleUnsuspend}
|
|
disabled={loading}
|
|
className="rounded-xl border border-emerald-300 bg-white px-4 py-2 text-sm font-bold text-emerald-700 hover:bg-emerald-50 disabled:opacity-50"
|
|
>
|
|
{loading ? "Memproses..." : "Buka Suspend"}
|
|
</button>
|
|
{error && (
|
|
<div className="rounded-lg bg-red-50 px-3 py-2 text-xs font-medium text-red-700">
|
|
{error}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (!open) {
|
|
return (
|
|
<button
|
|
type="button"
|
|
onClick={() => setOpen(true)}
|
|
className="rounded-xl bg-red-600 px-4 py-2 text-sm font-bold text-white shadow-sm hover:bg-red-700"
|
|
>
|
|
Suspend User
|
|
</button>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-3 rounded-xl border border-red-200 bg-red-50/60 p-4">
|
|
<div>
|
|
<label
|
|
htmlFor="suspend-reason"
|
|
className="mb-1 block text-xs font-semibold text-red-900"
|
|
>
|
|
Alasan suspend (wajib min 10 karakter — untuk audit)
|
|
</label>
|
|
<textarea
|
|
id="suspend-reason"
|
|
rows={3}
|
|
value={reason}
|
|
onChange={(e) => setReason(e.target.value)}
|
|
maxLength={500}
|
|
placeholder="contoh: User membuat 5 trip palsu dengan alias, lapor masuk dari peserta korban (ticket #123)."
|
|
className="w-full rounded-xl border border-red-200 bg-white px-3 py-2 text-sm text-neutral-800 placeholder:text-neutral-400 focus:border-red-400"
|
|
/>
|
|
<p className="mt-1 text-[11px] text-red-900/70">
|
|
{reason.trim().length}/500 karakter
|
|
</p>
|
|
</div>
|
|
|
|
{error && (
|
|
<div className="rounded-lg bg-red-100 px-3 py-2 text-xs font-medium text-red-700">
|
|
{error}
|
|
</div>
|
|
)}
|
|
|
|
<div className="flex gap-2">
|
|
<button
|
|
type="button"
|
|
onClick={handleSuspend}
|
|
disabled={loading || reason.trim().length < 10}
|
|
className="rounded-xl bg-red-600 px-4 py-2 text-sm font-bold text-white shadow-sm hover:bg-red-700 disabled:cursor-not-allowed disabled:opacity-50"
|
|
>
|
|
{loading ? "Memproses..." : "Konfirmasi Suspend"}
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
setOpen(false);
|
|
setReason("");
|
|
setError("");
|
|
}}
|
|
disabled={loading}
|
|
className="rounded-xl border border-neutral-200 bg-white px-4 py-2 text-sm font-semibold text-neutral-700 hover:bg-neutral-50 disabled:opacity-50"
|
|
>
|
|
Batal
|
|
</button>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|