-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #595 from actiontech/feature/issue-2208
Feature/issue 2208
- Loading branch information
Showing
45 changed files
with
3,474 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/base/src/page/Account/PersonalSMS/components/ConfigField.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useTranslation } from 'react-i18next'; | ||
import { FormItemLabel } from '@actiontech/shared/lib/components/CustomForm'; | ||
import { VerificationCodeInput } from '@actiontech/shared'; | ||
import { ConfigFieldProps } from '../index.type'; | ||
import { SMSService } from '@actiontech/shared/lib/api'; | ||
|
||
const ConfigField: React.FC<ConfigFieldProps> = ({ userPhone, username }) => { | ||
const { t } = useTranslation(); | ||
|
||
const onSendCode = () => { | ||
return SMSService.SendSmsCode({ username }); | ||
}; | ||
|
||
return ( | ||
<> | ||
<FormItemLabel label={t('common.phone')}>{userPhone}</FormItemLabel> | ||
<FormItemLabel | ||
label={t('dmsAccount.sms.verificationCode')} | ||
name="code" | ||
rules={[{ required: true }]} | ||
> | ||
<VerificationCodeInput onSendCode={onSendCode} /> | ||
</FormItemLabel> | ||
</> | ||
); | ||
}; | ||
|
||
export default ConfigField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const switchFieldName = 'enabled'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { useTranslation } from 'react-i18next'; | ||
import { useBoolean } from 'ahooks'; | ||
import { useMemo, useEffect } from 'react'; | ||
import { Spin, message } from 'antd'; | ||
import { CustomLabelContent } from '@actiontech/shared/lib/components/CustomForm'; | ||
import ConfigField from './components/ConfigField'; | ||
import { ResponseCode } from '@actiontech/shared/lib/enum'; | ||
import { FormFields } from './index.type'; | ||
import { switchFieldName } from './index.data'; | ||
import { | ||
useConfigRender, | ||
ConfigSwitch, | ||
ConfigSubmitButtonField, | ||
useConfigSwitchControls | ||
} from '@actiontech/shared'; | ||
import { PersonalSMSProps } from './index.type'; | ||
import { UserService, SMSService } from '@actiontech/shared/lib/api'; | ||
|
||
const PersonalSMS: React.FC<PersonalSMSProps> = ({ | ||
userBaseInfo, | ||
getUserInfo, | ||
loading | ||
}) => { | ||
const { t } = useTranslation(); | ||
|
||
const [messageApi, contextHolder] = message.useMessage(); | ||
|
||
const [submitLoading, { setTrue: startSubmit, setFalse: submitFinish }] = | ||
useBoolean(); | ||
|
||
const { | ||
form, | ||
renderConfigForm, | ||
startModify, | ||
modifyFinish, | ||
modifyFlag, | ||
enabled | ||
} = useConfigRender<FormFields>({ | ||
switchFieldName, | ||
switchFieldLabel: ( | ||
<CustomLabelContent title={t('dmsAccount.sms.title')} tips="" /> | ||
) | ||
}); | ||
|
||
useEffect(() => { | ||
if (!!userBaseInfo) { | ||
form.setFieldsValue({ | ||
[switchFieldName]: !!userBaseInfo.two_factor_enabled | ||
}); | ||
} | ||
}, [userBaseInfo, form]); | ||
|
||
const isConfigClosed = useMemo(() => { | ||
return !userBaseInfo?.two_factor_enabled; | ||
}, [userBaseInfo]); | ||
|
||
const handleClickModify = () => { | ||
startModify(); | ||
}; | ||
const handleClickCancel = () => { | ||
if (isConfigClosed) form.setFieldValue(switchFieldName, false); | ||
modifyFinish(); | ||
}; | ||
|
||
const onSubmit = async (isCodingEnabled: boolean, values?: FormFields) => { | ||
startSubmit(); | ||
if (isCodingEnabled) { | ||
const verificationRes = await SMSService.VerifySmsCode({ | ||
code: values?.code, | ||
username: userBaseInfo?.name | ||
}); | ||
if (verificationRes.data.data?.verify_error_message) { | ||
messageApi.error(verificationRes.data.data?.verify_error_message); | ||
} | ||
if ( | ||
verificationRes.data.code !== ResponseCode.SUCCESS || | ||
!verificationRes.data.data?.is_verify_sent_normally | ||
) { | ||
submitFinish(); | ||
return; | ||
} | ||
} | ||
UserService.UpdateCurrentUser({ | ||
current_user: { | ||
two_factor_enabled: isCodingEnabled | ||
} | ||
}) | ||
.then((res) => { | ||
if (res.data.code === ResponseCode.SUCCESS) { | ||
messageApi.success(t('dmsAccount.sms.updateSuccessTips')); | ||
modifyFinish(); | ||
form.resetFields(); | ||
getUserInfo(); | ||
} | ||
}) | ||
.finally(() => { | ||
submitFinish(); | ||
}); | ||
}; | ||
|
||
const onConfigSwitchPopoverConfirm = () => { | ||
if (!userBaseInfo?.two_factor_enabled && modifyFlag) { | ||
handleClickCancel(); | ||
hiddenConfigSwitchPopover(); | ||
} else { | ||
onSubmit(false); | ||
} | ||
}; | ||
|
||
const { | ||
configSwitchPopoverOpenState, | ||
generateConfigSwitchPopoverTitle, | ||
onConfigSwitchPopoverOpen, | ||
handleConfigSwitchChange, | ||
hiddenConfigSwitchPopover | ||
} = useConfigSwitchControls(form, switchFieldName); | ||
|
||
return ( | ||
<div> | ||
{contextHolder} | ||
<Spin spinning={loading}> | ||
{renderConfigForm({ | ||
data: userBaseInfo ?? {}, | ||
columns: [], | ||
configExtraButtons: null, | ||
configSwitchNode: ( | ||
<ConfigSwitch | ||
title={generateConfigSwitchPopoverTitle(modifyFlag)} | ||
switchFieldName={switchFieldName} | ||
submitLoading={submitLoading} | ||
popoverVisible={configSwitchPopoverOpenState} | ||
onConfirm={onConfigSwitchPopoverConfirm} | ||
onSwitchChange={(open) => { | ||
if (open && !userBaseInfo?.phone) { | ||
messageApi.error(t('dmsAccount.sms.noPhoneNumbersTips')); | ||
form.setFieldValue(switchFieldName, false); | ||
return; | ||
} | ||
handleConfigSwitchChange(open, handleClickModify); | ||
}} | ||
onSwitchPopoverOpen={onConfigSwitchPopoverOpen} | ||
/> | ||
), | ||
configField: ( | ||
<ConfigField | ||
userPhone={userBaseInfo?.phone} | ||
username={userBaseInfo?.name} | ||
/> | ||
), | ||
submitButtonField: ( | ||
<ConfigSubmitButtonField | ||
submitLoading={submitLoading} | ||
handleClickCancel={handleClickCancel} | ||
/> | ||
), | ||
onSubmit: (values) => { | ||
onSubmit(enabled, values); | ||
} | ||
})} | ||
</Spin> | ||
</div> | ||
); | ||
}; | ||
|
||
export default PersonalSMS; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { IGetUser } from '@actiontech/shared/lib/api/base/service/common'; | ||
|
||
export type FormFields = { | ||
enabled: boolean; | ||
code: string; | ||
}; | ||
|
||
export type PersonalSMSProps = { | ||
userBaseInfo?: IGetUser; | ||
getUserInfo: () => void; | ||
loading: boolean; | ||
}; | ||
|
||
export type ConfigFieldProps = { | ||
userPhone?: string; | ||
username?: string; | ||
}; |
Oops, something went wrong.