import { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Card, CardContent, CardHeader, CardTitle, CardDescription, CardFooter } from "@/components/ui/card"; import { Save, Loader2 } from "lucide-react"; import { api } from "@/lib/api"; import { useAuthStore } from "@/store/authStore"; export function Settings() { const { t } = useTranslation(); const { user, updateUser } = useAuthStore(); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [isSaving, setIsSaving] = useState(false); const [error, setError] = useState(''); const [success, setSuccess] = useState(''); useEffect(() => { if (user) { setEmail(user.email || ''); } }, [user]); const isPasswordMismatch = password !== '' && confirmPassword !== '' && password !== confirmPassword; const handleSave = async () => { setError(''); setSuccess(''); if (isPasswordMismatch) { setError(t('passwordsDoNotMatch')); return; } setIsSaving(true); try { const updateData: any = { email: email }; if (password) { updateData.password = password; } if (user && user.id) { const response = await api.put(`/api/v1/users/${user.id}`, updateData); let successMsg = t('personalSettingsSaved'); if (password) { successMsg = t('personalSettingsAndPasswordSaved'); } setSuccess(successMsg); setPassword(''); setConfirmPassword(''); // Update global state with new email updateUser({ email: response.email }); } } catch (error: any) { console.error("Failed to save settings", error); setError(error.message || t('failedToSaveSettings')); } finally { setIsSaving(false); } }; return (
个人设置
{error &&
{error}
} {success &&
{success}
} {t('accountInfo')} {t('modifyLoginEmailAndPassword')}

{t('usernameCannotBeModified')}

setEmail(e.target.value)} />
{ setPassword(e.target.value); setError(''); }} />
{ setConfirmPassword(e.target.value); setError(''); }} /> {isPasswordMismatch &&

{t('passwordsDoNotMatch')}

}
); }