import React, { useState, Dispatch, useCallback } from 'react'; import { FormControl, Box, Input, Button, FormErrorMessage, Flex } from '@chakra-ui/react'; import { useForm } from 'react-hook-form'; import { PageTypeEnum } from '../../../constants/user'; import { postFindPassword } from '@/api/user'; import { useSendCode } from '@/hooks/useSendCode'; import type { ResLogin } from '@/api/response/user'; import { useScreen } from '@/hooks/useScreen'; import { useToast } from '@/hooks/useToast'; interface Props { setPageType: Dispatch<`${PageTypeEnum}`>; loginSuccess: (e: ResLogin) => void; } interface RegisterType { username: string; code: string; password: string; password2: string; } const RegisterForm = ({ setPageType, loginSuccess }: Props) => { const { toast } = useToast(); const { mediaLgMd } = useScreen(); const { register, handleSubmit, getValues, trigger, formState: { errors } } = useForm({ mode: 'onBlur' }); const { codeSending, sendCodeText, sendCode, codeCountDown } = useSendCode(); const onclickSendCode = useCallback(async () => { const check = await trigger('username'); if (!check) return; sendCode({ username: getValues('username'), type: 'findPassword' }); }, [getValues, sendCode, trigger]); const [requesting, setRequesting] = useState(false); const onclickFindPassword = useCallback( async ({ username, code, password }: RegisterType) => { setRequesting(true); try { loginSuccess( await postFindPassword({ username, code, password }) ); toast({ title: `密码已找回`, status: 'success' }); } catch (error: any) { toast({ title: error.message || '修改密码异常', status: 'error' }); } setRequesting(false); }, [loginSuccess, toast] ); return ( <> 找回 FastGPT 账号
{!!errors.username && errors.username.message} {!!errors.code && errors.code.message} {!!errors.password && errors.password.message} (getValues('password') === val ? true : '两次密码不一致') })} > {!!errors.password2 && errors.password2.message} setPageType('login')} > 去登录
); }; export default RegisterForm;