import { useEffect, useState } from "react"; import PageMeta from "../../components/common/PageMeta"; import { apiGet, apiPut } from "../../lib/api"; import { useToastOnError } from "../../hooks/useToastOnError"; type Me = { id: number; name: string; email: string; }; export default function Profile() { const [loading, setLoading] = useState(true); const [savingProfile, setSavingProfile] = useState(false); const [savingPass, setSavingPass] = useState(false); const [error, setError] = useState(null); const [msg, setMsg] = useState(null); useToastOnError(error); const [form, setForm] = useState({ name: "", email: "", }); const [pass, setPass] = useState({ current_password: "", password: "", password_confirmation: "", }); const load = async () => { setLoading(true); setError(null); try { const res = await apiGet<{ data: Me }>("/admin-api/me"); setForm({ name: res.data.name || "", email: res.data.email || "", }); } catch (e: any) { setError(e?.message || "Gagal memuat profil"); } finally { setLoading(false); } }; useEffect(() => { load(); }, []); const saveProfile = async () => { setSavingProfile(true); setError(null); setMsg(null); try { await apiPut("/admin-api/me", { name: form.name, email: form.email, }); setMsg("Profil berhasil disimpan."); } catch (e: any) { setError(e?.message || "Gagal menyimpan profil"); } finally { setSavingProfile(false); } }; const savePassword = async () => { setSavingPass(true); setError(null); setMsg(null); try { await apiPut("/admin-api/me/password", pass); setMsg("Password berhasil diperbarui."); setPass({ current_password: "", password: "", password_confirmation: "", }); } catch (e: any) { setError(e?.message || "Gagal mengganti password"); } finally { setSavingPass(false); } }; return ( <>

Profil

{msg && (
{msg}
)} {error && (
{error}
)} {loading ? (
Memuat profil...
) : (
Informasi Profil
setForm((f) => ({ ...f, name: e.target.value }))} placeholder="Nama" className="h-11 rounded-xl border border-gray-200 bg-white px-4 text-sm dark:border-gray-800 dark:bg-gray-950" /> setForm((f) => ({ ...f, email: e.target.value }))} placeholder="Email" type="email" className="h-11 rounded-xl border border-gray-200 bg-white px-4 text-sm dark:border-gray-800 dark:bg-gray-950" />
Ganti Password
setPass((p) => ({ ...p, current_password: e.target.value })) } placeholder="Password sekarang" type="password" className="h-11 rounded-xl border border-gray-200 bg-white px-4 text-sm dark:border-gray-800 dark:bg-gray-950" /> setPass((p) => ({ ...p, password: e.target.value }))} placeholder="Password baru" type="password" className="h-11 rounded-xl border border-gray-200 bg-white px-4 text-sm dark:border-gray-800 dark:bg-gray-950" /> setPass((p) => ({ ...p, password_confirmation: e.target.value })) } placeholder="Konfirmasi password baru" type="password" className="h-11 rounded-xl border border-gray-200 bg-white px-4 text-sm dark:border-gray-800 dark:bg-gray-950" />
)}
); }