Files
PAGIVA/src/components/AccountForm.tsx
T
Loma Bot Ops 4a673ebb28 Support sub-path deploy via NEXT_PUBLIC_BASE_PATH (loma.chat/pagiva)
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>
2026-11-22 16:48:42 +07:00

71 lines
4.6 KiB
TypeScript

"use client";
import { withBase } from "@/lib/base-path";
import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import { PageAvatar } from "./PageAvatar";
import { Notice } from "./ui";
import { useToast } from "./Toast";
export function AccountForm({ initial }: { initial: { name: string; email: string; bio: string; avatarUrl: string | null } }) {
const router = useRouter();
const toast = useToast();
const [name, setName] = useState(initial.name);
const [bio, setBio] = useState(initial.bio);
const [file, setFile] = useState<File | null>(null);
const [preview, setPreview] = useState<string | null>(null);
const [removeAvatar, setRemoveAvatar] = useState(false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
useEffect(() => () => { if (preview) URL.revokeObjectURL(preview); }, [preview]);
async function save(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
setBusy(true);
setError(null);
const form = new FormData();
form.set("name", name);
form.set("bio", bio);
form.set("removeAvatar", String(removeAvatar));
if (file) form.set("avatar", file);
try {
const response = await fetch(withBase("/api/account"), { method: "PUT", body: form });
const json = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(json.error ?? "บันทึกไม่สำเร็จ");
toast.show("บันทึกโปรไฟล์แล้ว");
router.refresh();
} catch (error) {
setError(error instanceof Error ? error.message : "เชื่อมต่อไม่สำเร็จ ลองใหม่อีกครั้ง");
} finally { setBusy(false); }
}
return <form className="card max-w-[680px]" onSubmit={save}>
<div className="card-head"><h2>โปรไฟล์ส่วนตัว</h2></div>
<fieldset disabled={busy} className="card-body flex flex-col gap-5">
<div className="flex flex-wrap items-center gap-4">
<PageAvatar name={name || initial.email} src={removeAvatar ? null : preview ?? initial.avatarUrl} size={80} />
<div className="flex-1 min-w-[200px]">
<label className="field"><span>รูปโปรไฟล์</span>
<input type="file" accept="image/jpeg,image/png,image/webp" aria-describedby="avatar-hint" onChange={(e) => {
const next = e.target.files?.[0] ?? null;
if (!next) return;
if (!["image/jpeg", "image/png", "image/webp"].includes(next.type) || next.size > 2 * 1024 * 1024) {
setError("ใช้รูป JPG, PNG หรือ WebP ขนาดไม่เกิน 2 MB"); e.target.value = ""; return;
}
setError(null); setFile(next); setPreview(URL.createObjectURL(next)); setRemoveAvatar(false); e.target.value = "";
}} />
</label>
<p id="avatar-hint" className="text-[12px] text-ink-3 mt-1">JPG, PNG หรือ WebP ไม่เกิน 2 MB ระบบตัดรูปเป็นสี่เหลี่ยมจัตุรัส</p>
{!removeAvatar && (initial.avatarUrl || file) && <button type="button" className="btn btn-ghost btn-sm mt-2" onClick={() => { setFile(null); setPreview(null); setRemoveAvatar(true); }}>ลบรูปโปรไฟล์</button>}
</div>
</div>
<label className="field"><span>ชื่อที่แสดง</span><input className="input" autoComplete="name" required maxLength={100} value={name} onChange={(e) => setName(e.target.value)} /></label>
<label className="field"><span>อีเมล</span><input className="input" type="email" value={initial.email} readOnly aria-describedby="email-hint" /><span id="email-hint" className="text-ink-3">ใช้เข้าสู่ระบบ ติดต่อผู้ดูแลระบบหากต้องเปลี่ยน</span></label>
<label className="field"><span>แนะนำตัว</span><textarea className="textarea" rows={4} maxLength={500} value={bio} onChange={(e) => setBio(e.target.value)} placeholder="เช่น ดูแลคอนเทนต์และตอบแชทของร้าน" /><span className="text-ink-3">{bio.length}/500 ตัวอักษร</span></label>
{error && <Notice tone="red">{error}</Notice>}
<div><button className="btn btn-primary" disabled={busy}>{busy ? "กำลังบันทึก…" : "บันทึกโปรไฟล์"}</button></div>
</fieldset>
</form>;
}