Skip to content

Commit db2b334

Browse files
Merge pull request #38 from DonOmalVindula/feat/staple.com-fixes
chore(react): Fix loading state issues in the React SDK
2 parents 2870636 + 0362e93 commit db2b334

File tree

6 files changed

+33
-14
lines changed

6 files changed

+33
-14
lines changed

.changeset/brave-colts-double.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@asgardeo/react": patch
3+
---
4+
5+
Fix loading state issues in the React SDK

packages/react/src/components/SignIn/SignIn.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ const SignIn: FC<SignInProps> = (props: SignInProps): ReactElement => {
7474
totpChildren,
7575
} = props;
7676

77-
const [isComponentLoading, setIsComponentLoading] = useState<boolean>(true);
7877
const [alert, setAlert] = useState<AlertType>();
7978
const [showSelfSignUp, setShowSelfSignUp] = useState<boolean>(showSignUp);
8079
const [componentBranding, setComponentBranding] = useState<Branding>();
@@ -104,12 +103,13 @@ const SignIn: FC<SignInProps> = (props: SignInProps): ReactElement => {
104103
authorize()
105104
.then((response: AuthApiResponse) => {
106105
authContext?.setAuthResponse(response);
107-
setIsComponentLoading(false);
108106
})
109107
.catch((error: Error) => {
110108
setAlert({alertType: {error: true}, key: keys.common.error});
111-
setIsComponentLoading(false);
112109
throw new AsgardeoUIException('REACT_UI-SIGN_IN-SI-SE01', 'Authorization failed', error.stack);
110+
})
111+
.finally(() => {
112+
authContext?.setIsComponentLoading(false);
113113
});
114114
}, []);
115115

@@ -125,7 +125,7 @@ const SignIn: FC<SignInProps> = (props: SignInProps): ReactElement => {
125125
throw new AsgardeoUIException('REACT_UI-SIGN_IN-HA-IV02', 'Auth response is undefined.');
126126
}
127127

128-
authContext.setIsAuthLoading(true);
128+
authContext?.setIsAuthLoading(true);
129129

130130
const resp: AuthApiResponse = await authenticate({
131131
flowId: authContext?.authResponse.flowId,
@@ -135,7 +135,7 @@ const SignIn: FC<SignInProps> = (props: SignInProps): ReactElement => {
135135
},
136136
}).catch((authnError: Error) => {
137137
setAlert({alertType: {error: true}, key: keys.common.error});
138-
authContext.setIsAuthLoading(false);
138+
authContext?.setIsAuthLoading(false);
139139
throw new AsgardeoUIException('REACT_UI-SIGN_IN-HA-SE03', 'Authentication failed.', authnError.stack);
140140
});
141141

@@ -185,7 +185,7 @@ const SignIn: FC<SignInProps> = (props: SignInProps): ReactElement => {
185185

186186
await authInstance.requestAccessToken(resp.authData.code, resp.authData.session_state, state);
187187

188-
authContext.setAuthentication();
188+
authContext?.setAuthentication();
189189
} else if (resp.flowStatus === FlowStatus.FailIncomplete) {
190190
authContext?.setAuthResponse({
191191
...resp,
@@ -198,7 +198,7 @@ const SignIn: FC<SignInProps> = (props: SignInProps): ReactElement => {
198198
setShowSelfSignUp(false);
199199
}
200200

201-
authContext.setIsAuthLoading(false);
201+
authContext?.setIsAuthLoading(false);
202202
};
203203

204204
const renderLoginOptions = (authenticators: Authenticator[]): ReactElement[] => {
@@ -208,7 +208,7 @@ const SignIn: FC<SignInProps> = (props: SignInProps): ReactElement => {
208208
const displayName: string = authenticator.idp === 'LOCAL' ? authenticator.authenticator : authenticator.idp;
209209
LoginOptions.push(
210210
<LoginOptionsBox
211-
isAuthLoading={authContext.isAuthLoading}
211+
isAuthLoading={authContext?.isAuthLoading}
212212
socialName={authenticator.authenticator}
213213
displayName={`${t(keys.common.multiple.options.prefix)} ${displayName}`}
214214
handleOnClick={(): Promise<void> => handleAuthenticate(authenticator.authenticatorId)}
@@ -345,7 +345,7 @@ const SignIn: FC<SignInProps> = (props: SignInProps): ReactElement => {
345345
/**
346346
* Renders the circular progress component while the component or text is loading.
347347
*/
348-
if (isComponentLoading || isLoading || authContext.isBrandingLoading) {
348+
if (authContext?.isComponentLoading || isLoading || authContext?.isBrandingLoading) {
349349
return (
350350
<div className="Box-circularProgressHolder">
351351
<CircularProgress className="circular-progress" />
@@ -364,14 +364,14 @@ const SignIn: FC<SignInProps> = (props: SignInProps): ReactElement => {
364364
return (
365365
<ThemeProvider theme={generateThemeSignIn(componentBranding?.preference.theme)}>
366366
<UISignIn className="Box-asgardeoSignIn">
367-
{showLogo && !(isLoading || isComponentLoading) && (
367+
{showLogo && !(isLoading || authContext?.isComponentLoading) && (
368368
<UISignIn.Image className="asgardeo-sign-in-logo" src={imgUrl} />
369369
)}
370370
{authContext?.authResponse?.flowStatus !== FlowStatus.SuccessCompleted && !isAuthenticated && (
371371
<>
372372
{renderSignIn()}
373373

374-
{showFooter && !(isLoading || isComponentLoading) && (
374+
{showFooter && !(isLoading || authContext?.isComponentLoading) && (
375375
<UISignIn.Footer
376376
className="asgardeo-sign-in-footer"
377377
copyrights={{children: copyrightText}}

packages/react/src/components/SignIn/fragments/IdentifierFirst.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import {ScreenType, keys} from '@asgardeo/js';
2020
import {CircularProgress, Grid, Skeleton} from '@oxygen-ui/react';
21-
import {PropsWithChildren, ReactElement, useContext, useState} from 'react';
21+
import {PropsWithChildren, ReactElement, useContext} from 'react';
2222
import AsgardeoContext from '../../../contexts/asgardeo-context';
2323
import useTranslations from '../../../hooks/use-translations';
2424
import BasicAuthProps from '../../../models/basic-auth-props';

packages/react/src/hooks/use-translations.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {useContext, useEffect, useState} from 'react';
2020
import I18nContext from '../contexts/i18n-context';
2121
import {I18n, SetTranslationsProps} from '../models/i18n';
2222
import UseTranslations from '../models/use-translations';
23+
import AsgardeoContext from '../contexts/asgardeo-context';
2324

2425
/**
2526
* `useTranslations` is a custom hook that fetches translations.
@@ -38,15 +39,22 @@ const useTranslations = (props: SetTranslationsProps): UseTranslations => {
3839

3940
const [isLoading, setIsLoading] = useState<boolean>(true);
4041

42+
const {setIsTextLoading} = useContext(AsgardeoContext);
43+
4144
const contextValue: I18n = useContext(I18nContext);
4245
const {text, setTranslations} = contextValue;
4346

4447
useEffect(() => {
4548
setTranslations({componentLocaleOverride, componentTextOverrides, screen}).then((response: boolean) => {
4649
setIsLoading(!response);
50+
setIsTextLoading(!response);
4751
});
4852
}, [componentLocaleOverride, componentTextOverrides, screen, setTranslations]);
4953

54+
useEffect(() => {
55+
setIsTextLoading(isLoading);
56+
}, [isLoading]);
57+
5058
/**
5159
* `t` is a function that retrieves a specific translation from the fetched translations.
5260
* It takes a key as an argument, which is a string that represents a path to the desired translation.

packages/react/src/models/auth-context.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ interface AuthContext {
2525
isAuthLoading: boolean;
2626
isAuthenticated: boolean | undefined;
2727
isBrandingLoading: boolean;
28+
isComponentLoading: boolean;
2829
isGlobalLoading: boolean;
2930
isTextLoading: boolean;
3031
onSignOutRef: React.MutableRefObject<Function>;
3132
setAuthResponse: (response: AuthApiResponse) => void;
3233
setAuthentication: () => void;
3334
setIsAuthLoading: (value: boolean) => void;
3435
setIsBrandingLoading: (value: boolean) => void;
36+
setIsComponentLoading: (value: boolean) => void;
3537
setIsTextLoading: (value: boolean) => void;
3638
setOnSignIn: (response?: any) => void | Promise<void>;
3739
setOnSignOut: (response?: any) => void | Promise<void>;

packages/react/src/providers/AsgardeoProvider.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = (
5555
const [accessToken, setAccessToken] = useState<string>('');
5656
const [isAuthenticated, setIsAuthenticated] = useState<boolean>();
5757
const [user, setUser] = useState<MeAPIResponse>();
58-
5958
const [isBrandingLoading, setIsBrandingLoading] = useState<boolean>(true);
6059
const [isTextLoading, setIsTextLoading] = useState<boolean>(true);
6160
const [isAuthLoading, setIsAuthLoading] = useState<boolean>(false);
61+
const [isComponentLoading, setIsComponentLoading] = useState<boolean>(true);
6262
const [authResponse, setAuthResponse] = useState<AuthApiResponse>();
6363
const [username, setUsername] = useState<string>('');
6464

@@ -137,13 +137,15 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = (
137137
isAuthLoading,
138138
isAuthenticated,
139139
isBrandingLoading,
140-
isGlobalLoading: isBrandingLoading || isTextLoading || isAuthLoading,
140+
isComponentLoading,
141+
isGlobalLoading: isAuthLoading || isBrandingLoading || isComponentLoading || isTextLoading,
141142
isTextLoading,
142143
onSignOutRef,
143144
setAuthResponse,
144145
setAuthentication,
145146
setIsAuthLoading,
146147
setIsBrandingLoading,
148+
setIsComponentLoading,
147149
setIsTextLoading,
148150
setOnSignIn,
149151
setOnSignOut,
@@ -158,9 +160,11 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = (
158160
isAuthLoading,
159161
isAuthenticated,
160162
isBrandingLoading,
163+
isComponentLoading,
161164
isTextLoading,
162165
setAuthResponse,
163166
setAuthentication,
167+
setIsComponentLoading,
164168
setOnSignIn,
165169
setOnSignOut,
166170
setUsername,

0 commit comments

Comments
 (0)