templates2.Base.Next/components/admin/UserActions.tsx
w4c-templates 219be916cb
Some checks are pending
CI / quality (push) Waiting to run
chore(template): publish Base.Next
2026-10-11 23:23:30 +03:00

45 lines
1.2 KiB
TypeScript

'use client';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
export function UserActions({ id, role }: { id: string; role: string }) {
const router = useRouter();
const [busy, setBusy] = useState(false);
async function update(nextRole: string) {
setBusy(true);
await fetch(`/api/admin/users/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ role: nextRole }),
});
setBusy(false);
router.refresh();
}
async function remove() {
if (!window.confirm('Delete this user? This cannot be undone.')) return;
setBusy(true);
await fetch(`/api/admin/users/${id}`, { method: 'DELETE' });
setBusy(false);
router.refresh();
}
return (
<div className="flex justify-end gap-2">
<button
type="button"
className="btn-secondary"
disabled={busy}
onClick={() => update(role === 'admin' ? 'user' : 'admin')}
>
{role === 'admin' ? 'Make user' : 'Make admin'}
</button>
<button type="button" className="btn-danger" disabled={busy} onClick={remove}>
Delete
</button>
</div>
);
}