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 (

Settings

{/* Appearance Tab */} {activeTab === "appearance" && (

Appearance

Auto mode follows your system preferences

Show more items on screen with reduced spacing

)} {/* List Display Tab */} {activeTab === "list" && (

List Display

Your preferred sorting method when opening the list

Display items bought in the last 24 hours

{settings.showRecentlyBought && ( <>
handleNumberChange("recentlyBoughtCount", e.target.value)} className="settings-range" />

Number of items to show initially (5-50)

Start with the section collapsed

)}
)} {/* Behavior Tab */} {activeTab === "behavior" && (

Behavior

Show confirmation modal when marking items as bought

handleNumberChange("autoReloadInterval", e.target.value)} className="settings-range" />

Automatically refresh the list every X minutes (0 = disabled)

Vibrate on long-press and other interactions

)} {/* Account Tab */} {activeTab === "account" && (

Account Management

{accountMessage.text && (
{accountMessage.text}
)} {/* Display Name Section */}

Display Name

setDisplayName(e.target.value)} maxLength={100} className="form-input" placeholder="Your display name" />

{displayName.length}/100 characters


{/* Password Change Section */}

Change Password

setCurrentPassword(e.target.value)} className="form-input" required />
setNewPassword(e.target.value)} className="form-input" minLength={6} required />

Minimum 6 characters

setConfirmPassword(e.target.value)} className="form-input" minLength={6} required />
)}
); }