Update Fix
This commit is contained in:
189
resources/js/admin/tailadmin/pages/Pos/Profile.tsx
Normal file
189
resources/js/admin/tailadmin/pages/Pos/Profile.tsx
Normal file
@@ -0,0 +1,189 @@
|
||||
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<string | null>(null);
|
||||
const [msg, setMsg] = useState<string | null>(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 (
|
||||
<>
|
||||
<PageMeta title="Profil" description="Profil pengguna" />
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<h1 className="text-2xl font-semibold text-gray-800 dark:text-white/90">
|
||||
Profil
|
||||
</h1>
|
||||
</div>
|
||||
|
||||
{msg && (
|
||||
<div className="rounded-xl bg-emerald-50 px-4 py-3 text-sm text-emerald-700 dark:bg-emerald-500/10 dark:text-emerald-300">
|
||||
{msg}
|
||||
</div>
|
||||
)}
|
||||
{error && (
|
||||
<div className="rounded-xl bg-red-50 px-4 py-3 text-sm text-red-700 dark:bg-red-500/10 dark:text-red-300">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{loading ? (
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-5 text-sm text-gray-600 shadow-sm dark:border-gray-800 dark:bg-gray-900 dark:text-gray-400">
|
||||
Memuat profil...
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid gap-4 lg:grid-cols-2">
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="text-sm font-semibold text-gray-800 dark:text-white/90">
|
||||
Informasi Profil
|
||||
</div>
|
||||
<div className="mt-4 grid gap-3">
|
||||
<input
|
||||
value={form.name}
|
||||
onChange={(e) => 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"
|
||||
/>
|
||||
<input
|
||||
value={form.email}
|
||||
onChange={(e) => 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"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={saveProfile}
|
||||
disabled={savingProfile}
|
||||
className="h-10 w-[180px] rounded-xl bg-blue-600 text-sm font-semibold text-white hover:bg-blue-700 disabled:opacity-60"
|
||||
>
|
||||
{savingProfile ? "Menyimpan..." : "Simpan Profil"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="text-sm font-semibold text-gray-800 dark:text-white/90">
|
||||
Ganti Password
|
||||
</div>
|
||||
<div className="mt-4 grid gap-3">
|
||||
<input
|
||||
value={pass.current_password}
|
||||
onChange={(e) =>
|
||||
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"
|
||||
/>
|
||||
<input
|
||||
value={pass.password}
|
||||
onChange={(e) => 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"
|
||||
/>
|
||||
<input
|
||||
value={pass.password_confirmation}
|
||||
onChange={(e) =>
|
||||
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"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={savePassword}
|
||||
disabled={savingPass}
|
||||
className="h-10 w-[200px] rounded-xl bg-blue-600 text-sm font-semibold text-white hover:bg-blue-700 disabled:opacity-60"
|
||||
>
|
||||
{savingPass ? "Menyimpan..." : "Ganti Password"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user