Add loading and countdown status to the verification code sending button

This commit is contained in:
jianmaikj
2021-12-21 12:58:01 +08:00
parent 7cfece3019
commit 782b9e229a
3 changed files with 70 additions and 60 deletions

View File

@ -30,14 +30,21 @@ export const CountDownInput = (props) => {
const [captchaImg, setCaptchaImg] = React.useState(""); const [captchaImg, setCaptchaImg] = React.useState("");
const [checkType, setCheckType] = React.useState(""); const [checkType, setCheckType] = React.useState("");
const [checkId, setCheckId] = React.useState(""); const [checkId, setCheckId] = React.useState("");
const [buttonDisabled, setButtonDisabled] = React.useState(false); const [buttonLeftTime, setButtonLeftTime] = React.useState(0);
const [buttonLoading, setButtonLoading] = React.useState(false);
const countDown = (leftTime) => { const handleCountDown = (leftTime = 60) => {
if (leftTime === 0) { let leftTimeSecond = leftTime
setButtonDisabled(false); setButtonLeftTime(leftTimeSecond)
return; const countDown = () => {
leftTimeSecond--;
setButtonLeftTime(leftTimeSecond)
if (leftTimeSecond === 0) {
return;
}
setTimeout(countDown, 1000);
} }
setTimeout(() => countDown(leftTime - 1), 1000); setTimeout(countDown, 1000);
} }
const handleOk = () => { const handleOk = () => {
@ -50,11 +57,12 @@ export const CountDownInput = (props) => {
Util.showMessage("error", i18next.t("login:Invalid Email or phone")) Util.showMessage("error", i18next.t("login:Invalid Email or phone"))
return; return;
} }
setButtonLoading(true)
UserBackend.sendCode(checkType, checkId, key, ...onButtonClickArgs).then(res => { UserBackend.sendCode(checkType, checkId, key, ...onButtonClickArgs).then(res => {
setKey(""); setKey("");
setButtonLoading(false)
if (res) { if (res) {
setButtonDisabled(true) handleCountDown(60);
countDown(60);
} }
}) })
} }
@ -81,22 +89,23 @@ export const CountDownInput = (props) => {
const renderCaptcha = () => { const renderCaptcha = () => {
return ( return (
<Col> <Col>
<Row <Row
style={{ style={{
backgroundImage: `url('data:image/png;base64,${captchaImg}')`, backgroundImage: `url('data:image/png;base64,${captchaImg}')`,
backgroundRepeat: "no-repeat", backgroundRepeat: "no-repeat",
height: "80px", height: "80px",
width: "200px", width: "200px",
borderRadius: "3px", borderRadius: "3px",
border: "1px solid #ccc", border: "1px solid #ccc",
marginBottom: 10 marginBottom: 10
}} }}
/> />
<Row> <Row>
<Input autoFocus value={key} prefix={<SafetyOutlined />} placeholder={i18next.t("general:Captcha")} onPressEnter={handleOk} onChange={e => setKey(e.target.value)} /> <Input autoFocus value={key} prefix={<SafetyOutlined/>} placeholder={i18next.t("general:Captcha")}
</Row> onPressEnter={handleOk} onChange={e => setKey(e.target.value)}/>
</Col> </Row>
</Col>
) )
} }
@ -106,39 +115,38 @@ export const CountDownInput = (props) => {
} }
return ( return (
<React.Fragment> <React.Fragment>
<Search <Search
addonBefore={textBefore} addonBefore={textBefore}
disabled={disabled} disabled={disabled}
prefix={<SafetyOutlined />} prefix={<SafetyOutlined/>}
placeholder={i18next.t("code:Enter your code")} placeholder={i18next.t("code:Enter your code")}
onChange={e => onChange(e.target.value)} onChange={e => onChange(e.target.value)}
enterButton={ enterButton={
<Button type={"primary"} disabled={disabled || buttonDisabled}> <Button style={{fontSize: 14}} type={"primary"} disabled={disabled || buttonLeftTime > 0}
<div style={{fontSize: 14}}> loading={buttonLoading}>
{i18next.t("code:Send Code")} {buttonLeftTime > 0 ? `${buttonLeftTime} s` : buttonLoading ? i18next.t("code:Sending Code") : i18next.t("code:Send Code")}
</div> </Button>
</Button> }
} onSearch={loadHumanCheck}
onSearch={loadHumanCheck} />
/> <Modal
<Modal closable={false}
closable={false} maskClosable={false}
maskClosable={false} destroyOnClose={true}
destroyOnClose={true} title={i18next.t("general:Captcha")}
title={i18next.t("general:Captcha")} visible={visible}
visible={visible} okText={i18next.t("user:OK")}
okText={i18next.t("user:OK")} cancelText={i18next.t("user:Cancel")}
cancelText={i18next.t("user:Cancel")} onOk={handleOk}
onOk={handleOk} onCancel={handleCancel}
onCancel={handleCancel} okButtonProps={{disabled: key.length !== 5}}
okButtonProps={{disabled: key.length !== 5}} width={248}
width={248} >
> {
{ renderCheck()
renderCheck() }
} </Modal>
</Modal> </React.Fragment>
</React.Fragment>
); );
} }

View File

@ -40,7 +40,8 @@
"Please input your phone verification code!": "Please input your phone verification code!", "Please input your phone verification code!": "Please input your phone verification code!",
"Please input your verification code!": "Please input your verification code!", "Please input your verification code!": "Please input your verification code!",
"Send Code": "Send Code", "Send Code": "Send Code",
"Submit and complete": "Submit and complete" "Submit and complete": "Submit and complete",
"Sending Code": "Sending"
}, },
"forget": { "forget": {
"Account": "Account", "Account": "Account",

View File

@ -40,7 +40,8 @@
"Please input your phone verification code!": "请输入您的手机验证码!", "Please input your phone verification code!": "请输入您的手机验证码!",
"Please input your verification code!": "请输入您的验证码!", "Please input your verification code!": "请输入您的验证码!",
"Send Code": "发送验证码", "Send Code": "发送验证码",
"Submit and complete": "完成提交" "Submit and complete": "完成提交",
"Sending Code": "发送中"
}, },
"forget": { "forget": {
"Account": "账号", "Account": "账号",