1
0
mirror of https://github.com/sasjs/server.git synced 2025-12-11 19:44:35 +00:00

fix: show loading spinner on login screen while request is in process

This commit is contained in:
2023-01-26 22:27:43 +05:00
parent 305077f36e
commit 69f2576ee6

View File

@@ -2,7 +2,14 @@ import axios from 'axios'
import React, { useState, useContext } from 'react' import React, { useState, useContext } from 'react'
import PropTypes from 'prop-types' 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' import { AppContext } from '../context/appContext'
const login = async (payload: { username: string; password: string }) => const login = async (payload: { username: string; password: string }) =>
@@ -10,21 +17,27 @@ const login = async (payload: { username: string; password: string }) =>
const Login = () => { const Login = () => {
const appContext = useContext(AppContext) const appContext = useContext(AppContext)
const [isLoading, setIsLoading] = useState(false)
const [username, setUsername] = useState('') const [username, setUsername] = useState('')
const [password, setPassword] = useState('') const [password, setPassword] = useState('')
const [errorMessage, setErrorMessage] = useState('') const [errorMessage, setErrorMessage] = useState('')
const handleSubmit = async (e: any) => { const handleSubmit = async (e: any) => {
setIsLoading(true)
setErrorMessage('') setErrorMessage('')
e.preventDefault() e.preventDefault()
const { loggedIn, user } = await login({ const { loggedIn, user } = await login({
username, username,
password password
}).catch((err: any) => { })
.catch((err: any) => {
setErrorMessage(err.response?.data || err.toString()) setErrorMessage(err.response?.data || err.toString())
return {} return {}
}) })
.finally(() => {
setIsLoading(false)
})
if (loggedIn) { if (loggedIn) {
appContext.setUserId?.(user.id) appContext.setUserId?.(user.id)
@@ -37,6 +50,14 @@ const Login = () => {
} }
return ( return (
<>
<Backdrop
sx={{ color: '#fff', zIndex: (theme) => theme.zIndex.drawer + 1 }}
open={isLoading}
>
<CircularProgress color="inherit" />
</Backdrop>
<Box <Box
className="container" className="container"
component="form" component="form"
@@ -73,6 +94,7 @@ const Login = () => {
Submit Submit
</Button> </Button>
</Box> </Box>
</>
) )
} }