From 69f2576ee6d3d7b7f3325922a88656d511e3ac88 Mon Sep 17 00:00:00 2001 From: Sabir Hassan Date: Thu, 26 Jan 2023 22:27:43 +0500 Subject: [PATCH] fix: show loading spinner on login screen while request is in process --- web/src/components/login.tsx | 100 +++++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 39 deletions(-) diff --git a/web/src/components/login.tsx b/web/src/components/login.tsx index 7ba10f5..0b40419 100644 --- a/web/src/components/login.tsx +++ b/web/src/components/login.tsx @@ -2,7 +2,14 @@ import axios from 'axios' import React, { useState, useContext } from 'react' import PropTypes from 'prop-types' -import { CssBaseline, Box, TextField, Button } from '@mui/material' +import { + Backdrop, + CircularProgress, + CssBaseline, + Box, + TextField, + Button +} from '@mui/material' import { AppContext } from '../context/appContext' const login = async (payload: { username: string; password: string }) => @@ -10,21 +17,27 @@ const login = async (payload: { username: string; password: string }) => const Login = () => { const appContext = useContext(AppContext) + const [isLoading, setIsLoading] = useState(false) const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [errorMessage, setErrorMessage] = useState('') const handleSubmit = async (e: any) => { + setIsLoading(true) setErrorMessage('') e.preventDefault() const { loggedIn, user } = await login({ username, password - }).catch((err: any) => { - setErrorMessage(err.response?.data || err.toString()) - return {} }) + .catch((err: any) => { + setErrorMessage(err.response?.data || err.toString()) + return {} + }) + .finally(() => { + setIsLoading(false) + }) if (loggedIn) { appContext.setUserId?.(user.id) @@ -37,42 +50,51 @@ const Login = () => { } return ( - :not(style)': { m: 1, width: '25ch' } - }} - > - -
-

Welcome to SASjs Server!

- setUsername(e.target.value)} - required - /> - setPassword(e.target.value)} - required - /> - {errorMessage && {errorMessage}} - -
+ + + + :not(style)': { m: 1, width: '25ch' } + }} + > + +
+

Welcome to SASjs Server!

+ setUsername(e.target.value)} + required + /> + setPassword(e.target.value)} + required + /> + {errorMessage && {errorMessage}} + +
+ ) }