Преглед изворни кода

fix double click on login button

web_access_fix
Jason Chuang пре 11 часа
родитељ
комит
a783b7a895
2 измењених фајлова са 19 додато и 2 уклоњено
  1. +11
    -2
      src/pages/authentication/auth-forms/AuthLogin.js
  2. +8
    -0
      src/pages/authentication/auth-forms/AuthLoginCustom.js

+ 11
- 2
src/pages/authentication/auth-forms/AuthLogin.js Прегледај датотеку

@@ -1,5 +1,5 @@
import React, { import React, {
useEffect, useState} from 'react';
useEffect, useState, useRef} from 'react';
import {useNavigate} from 'react-router-dom'; import {useNavigate} from 'react-router-dom';


// material-ui // material-ui
@@ -46,6 +46,8 @@ const AuthLogin = () => {
let [posts, setPosts] = useState([]); let [posts, setPosts] = useState([]);
let [userName, setUserName] = useState(""); let [userName, setUserName] = useState("");
let [userPassword, setUserPassword] = useState(""); let [userPassword, setUserPassword] = useState("");
const [loginRequestPending, setLoginRequestPending] = useState(false);
const loginInFlightRef = useRef(false);


useEffect(() => { useEffect(() => {
//console.log("POST: " + posts.accessToken); //console.log("POST: " + posts.accessToken);
@@ -56,6 +58,11 @@ const AuthLogin = () => {
}; };


const tryLogin = () => { const tryLogin = () => {
if (loginInFlightRef.current) {
return;
}
loginInFlightRef.current = true;
setLoginRequestPending(true);
axios.post(`${apiPath}${POST_LOGIN}`, axios.post(`${apiPath}${POST_LOGIN}`,
{ {
"username": userName, "username": userName,
@@ -90,6 +97,8 @@ const AuthLogin = () => {
}) })
.catch(error => { .catch(error => {
console.error(error); console.error(error);
loginInFlightRef.current = false;
setLoginRequestPending(false);
}); });
} }


@@ -213,7 +222,7 @@ const AuthLogin = () => {
<Grid item xs={12}> <Grid item xs={12}>
<AnimateButton> <AnimateButton>
<Button disableElevation onClick={tryLogin} <Button disableElevation onClick={tryLogin}
disabled={isSubmitting} fullWidth size="large" type="submit" variant="contained" color="primary">
disabled={isSubmitting || loginRequestPending} fullWidth size="large" type="submit" variant="contained" color="primary">
<FormattedMessage id="login"/> <FormattedMessage id="login"/>
</Button> </Button>
</AnimateButton> </AnimateButton>


+ 8
- 0
src/pages/authentication/auth-forms/AuthLoginCustom.js Прегледај датотеку

@@ -1,6 +1,7 @@
import { import {
useEffect, useEffect,
useState, useState,
useRef,
lazy, lazy,
useContext useContext
} from 'react'; } from 'react';
@@ -71,6 +72,8 @@ const AuthLoginCustom = () => {
const [isButtonDisabled, setIsButtonDisabled] = useState(true); const [isButtonDisabled, setIsButtonDisabled] = useState(true);
const [errorMassage, setErrorMassage] = useState(''); const [errorMassage, setErrorMassage] = useState('');
const [onLogin, setOnLogin] = useState(false); const [onLogin, setOnLogin] = useState(false);
/** Blocks a second login before React re-renders (double-click / rapid taps). */
const loginInFlightRef = useRef(false);


const handleMouseDownPassword = (event) => { const handleMouseDownPassword = (event) => {
event.preventDefault(); event.preventDefault();
@@ -78,6 +81,10 @@ const AuthLoginCustom = () => {


const tryLogin = () => { const tryLogin = () => {
if (isValid) { if (isValid) {
if (loginInFlightRef.current) {
return;
}
loginInFlightRef.current = true;
dispatch(handleLogoutFunction()); dispatch(handleLogoutFunction());
setOnLogin(true) setOnLogin(true)
useJwt useJwt
@@ -127,6 +134,7 @@ const AuthLoginCustom = () => {
.catch((error) => { .catch((error) => {
// console.log(error.response) // console.log(error.response)
// setSuccess(false) // setSuccess(false)
loginInFlightRef.current = false;
setOnLogin(false) setOnLogin(false)
if (error.response != undefined) { if (error.response != undefined) {
setErrorMassage(error.response.data.error) setErrorMassage(error.response.data.error)


Loading…
Откажи
Сачувај