-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathModifyPassword.tsx
More file actions
90 lines (83 loc) · 2.2 KB
/
Copy pathModifyPassword.tsx
File metadata and controls
90 lines (83 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import React from 'react';
import {
modifyUserPassword,
showToasts,
t,
useAsyncRequest,
} from 'tailchat-shared';
import { ModalWrapper } from '../Modal';
import {
createMetaFormSchema,
MetaFormFieldMeta,
metaFormFieldSchema,
WebMetaForm,
} from 'tailchat-design';
interface Values {
oldPassword: string;
newPassword: string;
newPasswordRepeat: string;
}
const fields: MetaFormFieldMeta[] = [
{
type: 'password',
name: 'oldPassword',
label: t('旧密码'),
},
{
type: 'password',
name: 'newPassword',
label: t('新密码'),
},
{
type: 'password',
name: 'newPasswordRepeat',
label: t('重复密码'),
},
];
const schema = createMetaFormSchema({
oldPassword: metaFormFieldSchema
.string()
.min(6, t('密码不能低于6位'))
.required(t('密码不能为空')),
newPassword: metaFormFieldSchema
.string()
.min(6, t('密码不能低于6位'))
.matches(/[A-Z]/, '密码必须包含大写字母')
.matches(/[a-z]/, '密码必须包含小写字母')
.matches(/\d/, '密码必须包含数字')
.matches(/[`~!@#$%^&*()_+./,;':"*]/, '密码必须包含符号')
.required(t('密码不能为空')),
newPasswordRepeat: metaFormFieldSchema
.string()
.min(6, t('密码不能低于6位'))
.required(t('密码不能为空')),
});
interface ModifyPasswordProps {
onSuccess?: () => void;
}
export const ModifyPassword: React.FC<ModifyPasswordProps> = React.memo(
(props) => {
const [{}, handleModifyPassword] = useAsyncRequest(
async (values: Values) => {
if (values.newPassword !== values.newPasswordRepeat) {
showToasts(t('新旧密码不匹配'), 'warning');
return;
}
await modifyUserPassword(values.oldPassword, values.newPassword);
showToasts(t('密码修改成功'), 'success');
typeof props.onSuccess === 'function' && props.onSuccess();
},
[props.onSuccess]
);
return (
<ModalWrapper title={t('修改密码')}>
<WebMetaForm
schema={schema}
fields={fields}
onSubmit={handleModifyPassword}
/>
</ModalWrapper>
);
}
);
ModifyPassword.displayName = 'ModifyPassword';