4a673ebb28
Adds basePath config, withBase/appUrl helpers, base-path-aware cookies, OAuth redirects, manifest, service worker and cron runner. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
160 lines
7.8 KiB
TypeScript
160 lines
7.8 KiB
TypeScript
"use client";
|
|
import { withBase } from "@/lib/base-path";
|
|
|
|
import { useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import type { GlobalRole, PageRole } from "@prisma/client";
|
|
import { PAGE_ROLE_LABEL } from "@/lib/auth/roles";
|
|
import { MIN_PASSWORD_LENGTH } from "@/lib/auth/policy";
|
|
import { Icon } from "./Icon";
|
|
import { formatDateTime, Notice } from "./ui";
|
|
|
|
interface UserRow {
|
|
id: string;
|
|
name: string;
|
|
email: string;
|
|
globalRole: GlobalRole;
|
|
isActive: boolean;
|
|
lastLoginAt: string | null;
|
|
memberships: { role: PageRole; pageName: string }[];
|
|
}
|
|
|
|
export function UsersManager({ users, currentUserId }: { users: UserRow[]; currentUserId: string }) {
|
|
const router = useRouter();
|
|
const [form, setForm] = useState({ name: "", email: "", password: "", globalRole: "MEMBER" as GlobalRole });
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [flash, setFlash] = useState<string | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
async function call(url: string, method: string, body: unknown, ok: string) {
|
|
setBusy(true);
|
|
setError(null);
|
|
setFlash(null);
|
|
const res = await fetch(withBase(url), { method, headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) });
|
|
const data = await res.json().catch(() => ({}));
|
|
setBusy(false);
|
|
if (!res.ok) {
|
|
setError(data.error ?? "ทำรายการไม่สำเร็จ");
|
|
return false;
|
|
}
|
|
setFlash(ok);
|
|
router.refresh();
|
|
return true;
|
|
}
|
|
|
|
async function create(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
if (await call("/api/users", "POST", form, `สร้างบัญชี ${form.email} แล้ว — ผู้ใช้ต้องเปลี่ยนรหัสผ่านตอนเข้าครั้งแรก`)) {
|
|
setForm({ name: "", email: "", password: "", globalRole: "MEMBER" });
|
|
}
|
|
}
|
|
|
|
function resetPassword(u: UserRow) {
|
|
const password = window.prompt(`รหัสผ่านชั่วคราวใหม่ของ ${u.email} (อย่างน้อย ${MIN_PASSWORD_LENGTH} ตัวอักษร)`);
|
|
if (password) call(`/api/users/${u.id}`, "PATCH", { password }, `ตั้งรหัสผ่านชั่วคราวให้ ${u.email} แล้ว`);
|
|
}
|
|
|
|
return (
|
|
<div className="grid gap-5 grid-cols-[minmax(0,1fr)] xl:grid-cols-[minmax(0,1fr)_380px] items-start">
|
|
<section className="card">
|
|
<div className="card-head">
|
|
<h2>บัญชีผู้ใช้ ({users.length})</h2>
|
|
</div>
|
|
<div className="overflow-x-auto">
|
|
<table className="table">
|
|
<thead>
|
|
<tr>
|
|
<th>ผู้ใช้</th>
|
|
<th>สิทธิ์</th>
|
|
<th>เพจ</th>
|
|
<th>เข้าล่าสุด</th>
|
|
<th />
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{users.map((u) => (
|
|
<tr key={u.id} className={u.isActive ? "" : "opacity-55"}>
|
|
<td>
|
|
<div className="font-medium">{u.name}</div>
|
|
<div className="text-[12px] text-ink-3">{u.email}</div>
|
|
</td>
|
|
<td>
|
|
{u.globalRole === "SUPERADMIN" ? <span className="pill pill-blue">ผู้ดูแลระบบ</span> : <span className="pill pill-neutral">สมาชิก</span>}
|
|
{!u.isActive && <span className="pill pill-red ml-1">ปิดบัญชี</span>}
|
|
</td>
|
|
<td className="text-[12px] text-ink-2 max-w-[240px]">
|
|
{u.globalRole === "SUPERADMIN" ? "ทุกเพจ" : u.memberships.map((m) => `${m.pageName} (${PAGE_ROLE_LABEL[m.role]})`).join(", ") || "—"}
|
|
</td>
|
|
<td className="num text-[13px] text-ink-2 whitespace-nowrap">{formatDateTime(u.lastLoginAt)}</td>
|
|
<td>
|
|
{u.id !== currentUserId && (
|
|
<div className="flex gap-1 justify-end">
|
|
<button type="button" className="btn btn-ghost btn-sm" disabled={busy} onClick={() => resetPassword(u)}>
|
|
รีเซ็ตรหัส
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className="btn btn-ghost btn-sm"
|
|
disabled={busy}
|
|
onClick={() => call(`/api/users/${u.id}`, "PATCH", { globalRole: u.globalRole === "SUPERADMIN" ? "MEMBER" : "SUPERADMIN" }, "เปลี่ยนสิทธิ์แล้ว")}
|
|
>
|
|
{u.globalRole === "SUPERADMIN" ? "ลดเป็นสมาชิก" : "ตั้งเป็นผู้ดูแล"}
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className={`btn btn-sm ${u.isActive ? "btn-danger" : "btn-ghost"}`}
|
|
disabled={busy}
|
|
onClick={() => call(`/api/users/${u.id}`, "PATCH", { isActive: !u.isActive }, u.isActive ? "ปิดบัญชีแล้ว" : "เปิดบัญชีแล้ว")}
|
|
>
|
|
{u.isActive ? "ปิดบัญชี" : "เปิดบัญชี"}
|
|
</button>
|
|
</div>
|
|
)}
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</section>
|
|
|
|
<form className="card" onSubmit={create}>
|
|
<div className="card-head">
|
|
<h2>เพิ่มผู้ใช้</h2>
|
|
</div>
|
|
<div className="card-body flex flex-col gap-4">
|
|
<label className="field">
|
|
<span>ชื่อ</span>
|
|
<input className="input" required value={form.name} onChange={(e) => setForm({ ...form, name: e.target.value })} />
|
|
</label>
|
|
<label className="field">
|
|
<span>อีเมล</span>
|
|
<input className="input" type="email" required value={form.email} onChange={(e) => setForm({ ...form, email: e.target.value })} />
|
|
<small className="text-ink-3">ใช้อีเมลเดียวกับบัญชี Google หรือ Facebook ของผู้ใช้ เพื่อให้กดเข้าสู่ระบบด้วยบัญชีนั้นได้</small>
|
|
</label>
|
|
<label className="field">
|
|
<span>รหัสผ่านชั่วคราว (อย่างน้อย {MIN_PASSWORD_LENGTH} ตัว)</span>
|
|
<input className="input" type="text" required minLength={MIN_PASSWORD_LENGTH} autoComplete="off" value={form.password} onChange={(e) => setForm({ ...form, password: e.target.value })} />
|
|
</label>
|
|
<div className="field">
|
|
<span>สิทธิ์ระบบ</span>
|
|
<div className="flex gap-2">
|
|
<button type="button" className="chip" aria-pressed={form.globalRole === "MEMBER"} onClick={() => setForm({ ...form, globalRole: "MEMBER" })}>
|
|
สมาชิก
|
|
</button>
|
|
<button type="button" className="chip" aria-pressed={form.globalRole === "SUPERADMIN"} onClick={() => setForm({ ...form, globalRole: "SUPERADMIN" })}>
|
|
ผู้ดูแลระบบ
|
|
</button>
|
|
</div>
|
|
</div>
|
|
{error && <Notice tone="red">{error}</Notice>}
|
|
{flash && <Notice tone="blue">{flash}</Notice>}
|
|
<button className="btn btn-primary" disabled={busy}>
|
|
<Icon name="plus" size={16} /> สร้างบัญชี
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|