import { useContext, useEffect, useState } from "react"; import { changePassword, getCurrentUser, updateCurrentUser } from "../api/users"; import { SettingsContext } from "../context/SettingsContext"; import "../styles/pages/Settings.css"; export default function Settings() { const { settings, updateSettings, resetSettings } = useContext(SettingsContext); const [activeTab, setActiveTab] = useState("appearance"); // Account management state const [displayName, setDisplayName] = useState(""); const [currentPassword, setCurrentPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [accountMessage, setAccountMessage] = useState({ type: "", text: "" }); const [loadingProfile, setLoadingProfile] = useState(false); const [loadingPassword, setLoadingPassword] = useState(false); // Load user profile useEffect(() => { const loadProfile = async () => { try { const response = await getCurrentUser(); setDisplayName(response.data.display_name || response.data.name || ""); } catch (error) { console.error("Failed to load profile:", error); } }; loadProfile(); }, []); const handleThemeChange = (theme) => { updateSettings({ theme }); }; const handleUpdateDisplayName = async (e) => { e.preventDefault(); setLoadingProfile(true); setAccountMessage({ type: "", text: "" }); try { await updateCurrentUser(displayName); setAccountMessage({ type: "success", text: "Display name updated successfully!" }); } catch (error) { setAccountMessage({ type: "error", text: error.response?.data?.error || "Failed to update display name" }); } finally { setLoadingProfile(false); } }; const handleChangePassword = async (e) => { e.preventDefault(); setLoadingPassword(true); setAccountMessage({ type: "", text: "" }); if (newPassword !== confirmPassword) { setAccountMessage({ type: "error", text: "New passwords don't match" }); setLoadingPassword(false); return; } if (newPassword.length < 6) { setAccountMessage({ type: "error", text: "Password must be at least 6 characters" }); setLoadingPassword(false); return; } try { await changePassword(currentPassword, newPassword); setAccountMessage({ type: "success", text: "Password changed successfully!" }); setCurrentPassword(""); setNewPassword(""); setConfirmPassword(""); } catch (error) { setAccountMessage({ type: "error", text: error.response?.data?.error || "Failed to change password" }); } finally { setLoadingPassword(false); } }; const handleToggle = (key) => { updateSettings({ [key]: !settings[key] }); }; const handleNumberChange = (key, value) => { updateSettings({ [key]: parseInt(value, 10) }); }; const handleSelectChange = (key, value) => { updateSettings({ [key]: value }); }; const handleReset = () => { if (window.confirm("Reset all settings to defaults?")) { resetSettings(); } }; return (
Auto mode follows your system preferences
Show more items on screen with reduced spacing
Your preferred sorting method when opening the list
Display items bought in the last 24 hours
Number of items to show initially (5-50)
Start with the section collapsed
Show confirmation modal when marking items as bought
Automatically refresh the list every X minutes (0 = disabled)
Vibrate on long-press and other interactions