import { setUserJWT } from '@/utils/jwt-helper'; import { setGlobalUserLoginInfo } from '@/utils/user-helper'; import React, { useMemo, useState } from 'react'; import { claimTemporaryUser, model, showErrorToasts, t, useAppDispatch, useAsyncRequest, userActions, getGlobalConfig, } from 'tailchat-shared'; import { createMetaFormSchema, MetaFormFieldMeta, metaFormFieldSchema, WebMetaForm, FastifyFormFieldProps, useFastifyFormContext, } from 'tailchat-design'; import { ModalWrapper } from '../Modal'; import { Button, Input } from 'antd'; import _compact from 'lodash/compact'; interface Values { email: string; password: string; emailOTP?: string; [key: string]: unknown; } const getFields = (): MetaFormFieldMeta[] => _compact([ { type: 'text', name: 'email', label: t('邮箱') }, getGlobalConfig().emailVerification && { type: 'custom', name: 'emailOTP', label: t('邮箱校验码'), render: (props: FastifyFormFieldProps) => { const context = useFastifyFormContext(); const email = context?.values?.['email']; const [sended, setSended] = useState(false); const [{ loading }, handleVerifyEmail] = useAsyncRequest(async () => { if (!email) { showErrorToasts(t('邮箱不能为空')); return; } await model.user.verifyEmail(email); setSended(true); }, [email]); return ( props.onChange(e.target.value)} /> {!sended && ( )} ); }, }, { type: 'password', name: 'password', label: t('密码'), }, ]); const schema = createMetaFormSchema({ email: metaFormFieldSchema .string() .required(t('邮箱不能为空')) .email(t('邮箱格式不正确')) .max(40, t('邮箱最长限制40个字符')), password: metaFormFieldSchema .string() .required(t('密码不能为空')) .min(6, t('密码不能低于6位')) .max(40, t('密码最长限制40个字符')), emailOTP: metaFormFieldSchema.string().length(6, t('校验码为6位')), }); interface ClaimTemporaryUserProps { userId: string; onSuccess?: () => void; } export const ClaimTemporaryUser: React.FC = React.memo( (props) => { const userId = props.userId; const dispatch = useAppDispatch(); const fields = useMemo(() => getFields(), []); const [{}, handleClaim] = useAsyncRequest( async (values: Values) => { const data = await claimTemporaryUser( userId, values.email, values.password, values.emailOTP ); setGlobalUserLoginInfo(data); await setUserJWT(data.token); dispatch(userActions.setUserInfo(data)); typeof props.onSuccess === 'function' && props.onSuccess(); }, [userId, props.onSuccess] ); return ( ); } ); ClaimTemporaryUser.displayName = 'ClaimTemporaryUser';