import React from 'react' import { useForm } from 'react-hook-form' import { useMutation, useQuery } from '@tanstack/react-query' import { UserAPI } from './api/index.ts' import type { User } from '../share/types.ts' import type { UserResponse } from './api/index.ts' export default function SettingsPage() { const { data: userResponse, isLoading } = useQuery({ queryKey: ['currentUser'], queryFn: () => UserAPI.getCurrentUser(), select: (response) => response.data }) const { register, handleSubmit, formState: { errors }, reset } = useForm<{ nickname: string email: string phone?: string password?: string }>() const { mutate: updateUser, isPending } = useMutation({ mutationFn: async (data: { nickname: string email: string phone?: string password?: string }) => { return UserAPI.updateCurrentUser({ nickname: data.nickname, email: data.email, phone: data.phone, ...(data.password ? { password: data.password } : {}) }) }, onSuccess: () => alert('更新成功'), onError: (error) => { console.error('更新失败:', error) alert('更新失败') } }) const onSubmit = (data: { nickname: string email: string phone?: string password?: string }) => { updateUser(data) } React.useEffect(() => { if (userResponse) { reset({ nickname: userResponse.nickname || '', email: userResponse.email || '', phone: userResponse.phone || '' }) } }, [userResponse, reset]) if (isLoading) { return (

设置

加载中...

) } return (

设置

编辑个人信息

{errors.nickname &&

{errors.nickname.message}

}
{errors.email &&

{errors.email.message}

}
) }