import { useState, useEffect } from 'react'; 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 { 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("两次输入的密码不一致"); 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 = "个人设置保存成功!"; if (password) { successMsg = "个人设置及密码修改成功!"; } 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 || "保存设置失败"); } finally { setIsSaving(false); } }; return (
个人设置
{error &&
{error}
} {success &&
{success}
} 账号信息 修改您的登录邮箱和密码

用户名不可修改

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

两次输入的密码不一致

}
); }