Skip to content

Commit 418fcdf

Browse files
Merge pull request #110 from MobilityData/copilot/add-loading-state-to-sign-in
Add loading overlay to sign-in page during login
2 parents c66185b + b8d4475 commit 418fcdf

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

src/app/[locale]/sign-in/SignIn.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import { useFormik } from 'formik';
3030
import * as Yup from 'yup';
3131
import {
3232
Alert,
33+
Backdrop,
34+
CircularProgress,
3335
Divider,
3436
IconButton,
3537
InputAdornment,
@@ -59,8 +61,11 @@ export default function SignIn(): React.ReactElement {
5961
const [isSubmitted, setIsSubmitted] = React.useState(false);
6062
const [showPassword, setShowPassword] = React.useState(false);
6163
const [showNoEmailSnackbar, setShowNoEmailSnackbar] = React.useState(false);
64+
const [isOAuthLoading, setIsOAuthLoading] = React.useState(false);
6265
const searchParams = useSearchParams();
6366

67+
const isLoading = userProfileStatus === 'login_in' || isOAuthLoading;
68+
6469
const SignInSchema = Yup.object().shape({
6570
email: Yup.string()
6671
.email('Email format is invalid.')
@@ -92,6 +97,12 @@ export default function SignIn(): React.ReactElement {
9297
},
9398
});
9499

100+
React.useEffect(() => {
101+
if (emailLoginError != null) {
102+
setIsOAuthLoading(false);
103+
}
104+
}, [emailLoginError]);
105+
95106
React.useEffect(() => {
96107
if (userProfileStatus === 'registered') {
97108
if (searchParams.has('add_feed')) {
@@ -111,18 +122,21 @@ export default function SignIn(): React.ReactElement {
111122
const signInWithProvider = (oauthProvider: OauthProvider): void => {
112123
const auth = getAuth();
113124
const provider = oathProviders[oauthProvider];
125+
setIsOAuthLoading(true);
114126
signInWithPopup(auth, provider)
115127
.then((userCredential: UserCredential) => {
116128
if (!userCredential.user.emailVerified) {
117129
dispatch(verifyEmail());
118130
}
119131
if (userCredential.user.email == null) {
132+
setIsOAuthLoading(false);
120133
setShowNoEmailSnackbar(true);
121134
} else {
122135
dispatch(loginWithProvider({ oauthProvider, userCredential }));
123136
}
124137
})
125138
.catch((error) => {
139+
setIsOAuthLoading(false);
126140
dispatch(
127141
loginFail({
128142
code: error.code,
@@ -135,6 +149,13 @@ export default function SignIn(): React.ReactElement {
135149

136150
return (
137151
<Container component='main' maxWidth='xs'>
152+
<Backdrop
153+
open={isLoading}
154+
sx={{ zIndex: (theme) => theme.zIndex.drawer + 1 }}
155+
data-testid='signin-loading-overlay'
156+
>
157+
<CircularProgress aria-label='Signing in...' />
158+
</Backdrop>
138159
<Snackbar
139160
open={showNoEmailSnackbar}
140161
anchorOrigin={{ vertical: 'top', horizontal: 'center' }}

0 commit comments

Comments
 (0)