From 782b9e229acf2cced9848f137f1b714b0be1df63 Mon Sep 17 00:00:00 2001 From: jianmaikj <75167751+jianmaikj@users.noreply.github.com> Date: Tue, 21 Dec 2021 12:58:01 +0800 Subject: [PATCH] Add loading and countdown status to the verification code sending button --- web/src/component/CountDownInput.js | 124 +++++++++++++++------------- web/src/locales/en/data.json | 3 +- web/src/locales/zh/data.json | 3 +- 3 files changed, 70 insertions(+), 60 deletions(-) diff --git a/web/src/component/CountDownInput.js b/web/src/component/CountDownInput.js index ade81940..943e0cc8 100644 --- a/web/src/component/CountDownInput.js +++ b/web/src/component/CountDownInput.js @@ -30,14 +30,21 @@ export const CountDownInput = (props) => { const [captchaImg, setCaptchaImg] = React.useState(""); const [checkType, setCheckType] = 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) => { - if (leftTime === 0) { - setButtonDisabled(false); - return; + const handleCountDown = (leftTime = 60) => { + let leftTimeSecond = leftTime + setButtonLeftTime(leftTimeSecond) + const countDown = () => { + leftTimeSecond--; + setButtonLeftTime(leftTimeSecond) + if (leftTimeSecond === 0) { + return; + } + setTimeout(countDown, 1000); } - setTimeout(() => countDown(leftTime - 1), 1000); + setTimeout(countDown, 1000); } const handleOk = () => { @@ -50,11 +57,12 @@ export const CountDownInput = (props) => { Util.showMessage("error", i18next.t("login:Invalid Email or phone")) return; } + setButtonLoading(true) UserBackend.sendCode(checkType, checkId, key, ...onButtonClickArgs).then(res => { setKey(""); + setButtonLoading(false) if (res) { - setButtonDisabled(true) - countDown(60); + handleCountDown(60); } }) } @@ -81,22 +89,23 @@ export const CountDownInput = (props) => { const renderCaptcha = () => { return ( -