mirror of https://github.com/msgbyte/tailchat
feat: 访客登录提示与访客账号认领
parent
086458bb60
commit
446c70a42b
@ -0,0 +1,45 @@
|
||||
import { openModal } from '@/plugin/common';
|
||||
import { Button } from 'antd';
|
||||
import React, { useCallback } from 'react';
|
||||
import { t, Trans, useUserInfo } from 'tailchat-shared';
|
||||
import { closeModal } from './Modal';
|
||||
import { ClaimTemporaryUser } from './modals/ClaimTemporaryUser';
|
||||
|
||||
/**
|
||||
* 访客账号提示
|
||||
*/
|
||||
export const GlobalTemporaryTip: React.FC = React.memo(() => {
|
||||
const userInfo = useUserInfo();
|
||||
const show = userInfo?.temporary === true;
|
||||
|
||||
const handleClaim = useCallback(() => {
|
||||
if (!userInfo?._id) {
|
||||
return;
|
||||
}
|
||||
|
||||
const key = openModal(
|
||||
<ClaimTemporaryUser
|
||||
userId={userInfo._id}
|
||||
onSuccess={() => closeModal(key)}
|
||||
/>
|
||||
);
|
||||
}, [userInfo?._id]);
|
||||
|
||||
return show ? (
|
||||
<div className="text-center bg-indigo-400 text-white">
|
||||
<Trans>
|
||||
当前使用的是一个临时账号,{' '}
|
||||
<Button
|
||||
type="link"
|
||||
className="text-indigo-700 font-bold"
|
||||
size="small"
|
||||
onClick={handleClaim}
|
||||
>
|
||||
立即认领
|
||||
</Button>
|
||||
</Trans>
|
||||
{t('')}
|
||||
</div>
|
||||
) : null;
|
||||
});
|
||||
GlobalTemporaryTip.displayName = 'GlobalTemporaryTip';
|
@ -0,0 +1,76 @@
|
||||
import { setUserJWT } from '@/utils/jwt-helper';
|
||||
import { setGlobalUserLoginInfo } from '@/utils/user-helper';
|
||||
import React from 'react';
|
||||
import { useDispatch } from 'react-redux';
|
||||
import {
|
||||
claimTemporaryUser,
|
||||
createFastFormSchema,
|
||||
FastFormFieldMeta,
|
||||
fieldSchema,
|
||||
t,
|
||||
useAsyncRequest,
|
||||
userActions,
|
||||
} from 'tailchat-shared';
|
||||
import { ModalWrapper } from '../Modal';
|
||||
import { WebFastForm } from '../WebFastForm';
|
||||
|
||||
interface Values {
|
||||
email: string;
|
||||
password: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
const fields: FastFormFieldMeta[] = [
|
||||
{ type: 'text', name: 'email', label: t('邮箱') },
|
||||
{
|
||||
type: 'password',
|
||||
name: 'password',
|
||||
label: t('密码'),
|
||||
},
|
||||
];
|
||||
|
||||
const schema = createFastFormSchema({
|
||||
email: fieldSchema
|
||||
.string()
|
||||
.required(t('邮箱不能为空'))
|
||||
.email(t('邮箱格式不正确')),
|
||||
password: fieldSchema
|
||||
.string()
|
||||
.min(6, t('密码不能低于6位'))
|
||||
.required(t('密码不能为空')),
|
||||
});
|
||||
|
||||
interface ClaimTemporaryUserProps {
|
||||
userId: string;
|
||||
onSuccess: () => void;
|
||||
}
|
||||
export const ClaimTemporaryUser: React.FC<ClaimTemporaryUserProps> = React.memo(
|
||||
(props) => {
|
||||
const userId = props.userId;
|
||||
const dispatch = useDispatch();
|
||||
|
||||
const [{}, handleClaim] = useAsyncRequest(
|
||||
async (values: Values) => {
|
||||
const data = await claimTemporaryUser(
|
||||
userId,
|
||||
values.email,
|
||||
values.password
|
||||
);
|
||||
|
||||
setGlobalUserLoginInfo(data);
|
||||
await setUserJWT(data.token);
|
||||
dispatch(userActions.setUserInfo(data));
|
||||
|
||||
typeof props.onSuccess === 'function' && props.onSuccess();
|
||||
},
|
||||
[, userId, props.onSuccess]
|
||||
);
|
||||
|
||||
return (
|
||||
<ModalWrapper title={t('认领账号')}>
|
||||
<WebFastForm schema={schema} fields={fields} onSubmit={handleClaim} />
|
||||
</ModalWrapper>
|
||||
);
|
||||
}
|
||||
);
|
||||
ClaimTemporaryUser.displayName = 'ClaimTemporaryUser';
|
Loading…
Reference in New Issue