"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(null); const [preview, setPreview] = useState(null); const [removeAvatar, setRemoveAvatar] = useState(false); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); useEffect(() => () => { if (preview) URL.revokeObjectURL(preview); }, [preview]); async function save(event: React.FormEvent) { 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

โปรไฟล์ส่วนตัว

JPG, PNG หรือ WebP ไม่เกิน 2 MB ระบบตัดรูปเป็นสี่เหลี่ยมจัตุรัส

{!removeAvatar && (initial.avatarUrl || file) && }