Fix settings page loading bug and add error handling
This commit is contained in:
@@ -9,6 +9,7 @@ export default function SettingsPage() {
|
||||
const [profile, setProfile] = useState<any>(null);
|
||||
const [waStatus, setWaStatus] = useState<any>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const dict = useDictionary();
|
||||
|
||||
@@ -16,7 +17,6 @@ export default function SettingsPage() {
|
||||
fetchProfile();
|
||||
fetchWaStatus();
|
||||
|
||||
// Check WA status every 5 seconds if not connected
|
||||
const interval = setInterval(() => {
|
||||
fetchWaStatus();
|
||||
}, 5000);
|
||||
@@ -25,10 +25,21 @@ export default function SettingsPage() {
|
||||
}, []);
|
||||
|
||||
const fetchProfile = async () => {
|
||||
const res = await fetch("/api/users/profile");
|
||||
const data = await res.json();
|
||||
setProfile(data);
|
||||
setLoading(false);
|
||||
try {
|
||||
const res = await fetch("/api/users/profile");
|
||||
const data = await res.json();
|
||||
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 () => {
|
||||
@@ -45,20 +56,27 @@ export default function SettingsPage() {
|
||||
e.preventDefault();
|
||||
setSaving(true);
|
||||
try {
|
||||
await fetch("/api/users/profile", {
|
||||
const res = await fetch("/api/users/profile", {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(profile)
|
||||
});
|
||||
alert("Ayarlar kaydedildi!");
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
if (res.ok) {
|
||||
alert("Ayarlar kaydedildi!");
|
||||
} else {
|
||||
const data = await res.json();
|
||||
alert("Hata: " + data.error);
|
||||
}
|
||||
} catch (e: any) {
|
||||
alert("Hata: " + e.message);
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
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 (
|
||||
<>
|
||||
|
||||
Reference in New Issue
Block a user