import { isValidStr, registerWithEmail, t, useAsyncFn } from 'tailchat-shared'; import React, { useState } from 'react'; import { Spinner } from '../../components/Spinner'; import { string } from 'yup'; import { Icon } from '@/components/Icon'; import { useHistory } from 'react-router'; import { setUserJWT } from '../../utils/jwt-helper'; import { setGlobalUserLoginInfo } from '../../utils/user-helper'; import { useSearchParam } from '@/hooks/useSearchParam'; import { useNavToView } from './utils'; /** * 注册视图 */ export const RegisterView: React.FC = React.memo(() => { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const history = useHistory(); const navRedirect = useSearchParam('redirect'); const [{ loading, error }, handleRegister] = useAsyncFn(async () => { await string() .email(t('邮箱格式不正确')) .required(t('邮箱不能为空')) .validate(email); await string() .min(6, t('密码不能低于6位')) .required(t('密码不能为空')) .validate(password); const data = await registerWithEmail(email, password); setGlobalUserLoginInfo(data); await setUserJWT(data.token); if (isValidStr(navRedirect)) { history.push(decodeURIComponent(navRedirect)); } else { history.push('/main'); } }, [email, password, navRedirect]); const navToView = useNavToView(); return (
{error.message}
}