Fix settings page loading bug and add error handling

This commit is contained in:
AyrisAI
2026-05-14 22:43:47 +03:00
parent 1098668dc4
commit 7f1a81977f

View File

@@ -9,6 +9,7 @@ export default function SettingsPage() {
const [profile, setProfile] = useState<any>(null); const [profile, setProfile] = useState<any>(null);
const [waStatus, setWaStatus] = useState<any>(null); const [waStatus, setWaStatus] = useState<any>(null);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [saving, setSaving] = useState(false); const [saving, setSaving] = useState(false);
const dict = useDictionary(); const dict = useDictionary();
@@ -16,7 +17,6 @@ export default function SettingsPage() {
fetchProfile(); fetchProfile();
fetchWaStatus(); fetchWaStatus();
// Check WA status every 5 seconds if not connected
const interval = setInterval(() => { const interval = setInterval(() => {
fetchWaStatus(); fetchWaStatus();
}, 5000); }, 5000);
@@ -25,10 +25,21 @@ export default function SettingsPage() {
}, []); }, []);
const fetchProfile = async () => { const fetchProfile = async () => {
const res = await fetch("/api/users/profile"); try {
const data = await res.json(); const res = await fetch("/api/users/profile");
setProfile(data); const data = await res.json();
setLoading(false); if (data.error) {
setError(data.error);
} else if (!data) {
setError("Kullanıcı profili bulunamadı.");
} else {
setProfile(data);
}
} catch (e: any) {
setError(e.message);
} finally {
setLoading(false);
}
}; };
const fetchWaStatus = async () => { const fetchWaStatus = async () => {
@@ -45,20 +56,27 @@ export default function SettingsPage() {
e.preventDefault(); e.preventDefault();
setSaving(true); setSaving(true);
try { try {
await fetch("/api/users/profile", { const res = await fetch("/api/users/profile", {
method: "PATCH", method: "PATCH",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify(profile) body: JSON.stringify(profile)
}); });
alert("Ayarlar kaydedildi!"); if (res.ok) {
} catch (e) { alert("Ayarlar kaydedildi!");
console.error(e); } else {
const data = await res.json();
alert("Hata: " + data.error);
}
} catch (e: any) {
alert("Hata: " + e.message);
} finally { } finally {
setSaving(false); setSaving(false);
} }
}; };
if (loading) return <div className="page-body"><span className="spinner" /></div>; if (loading) return <div className="page-body"><span className="spinner" /></div>;
if (error) return <div className="page-body"><div className="card" style={{ color: "var(--error)" }}>Hata: {error}</div></div>;
if (!profile) return <div className="page-body">Profil yüklenemedi.</div>;
return ( return (
<> <>