Skip to content

Commit

Permalink
feat: fix spinner after logout (#5572)
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodallacqua-hpe authored Dec 9, 2022
1 parent e377d2f commit ead8c10
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 29 deletions.
5 changes: 1 addition & 4 deletions webui/react/src/components/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ const Router: React.FC<Props> = (props: Props) => {
Loaded: (auth) => auth.isAuthenticated,
NotLoaded: () => false,
});
const authChecked = Loadable.match(loadableAuth.auth, {
Loaded: (auth) => auth.checked,
NotLoaded: () => false,
});
const authChecked = loadableAuth.authChecked;
const [canceler] = useState(new AbortController());
const { actions: uiActions } = useUI();
const location = useLocation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ jest.mock('components/MonacoEditor', () => ({
}));

const ModalTrigger: React.FC = () => {
const { setAuth } = useAuth();
const { setAuth, setAuthCheck } = useAuth();
const { contextHolder, modalOpen } = useModalJupyterLab();

useEffect(() => {
setAuth({ isAuthenticated: true });
setAuthCheck();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down
3 changes: 2 additions & 1 deletion webui/react/src/hooks/useSettings.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ const extraConfig: hook.SettingsConfig<ExtraSettings> = {
};

const Container: React.FC<{ children: JSX.Element }> = ({ children }) => {
const { setAuth } = useAuth();
const { setAuth, setAuthCheck } = useAuth();

useEffect(() => {
setAuth({ isAuthenticated: true });
setAuthCheck();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down
5 changes: 1 addition & 4 deletions webui/react/src/hooks/useSettingsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ export const SettingsProvider: React.FC<React.PropsWithChildren> = ({ children }
Loaded: (auth) => auth.user,
NotLoaded: () => undefined,
});
const checked = Loadable.match(loadableAuth.auth, {
Loaded: (auth) => auth.checked,
NotLoaded: () => false,
});
const checked = loadableAuth.authChecked;
const [canceler] = useState(new AbortController());
const [isLoading, setIsLoading] = useState(true);
const querySettings = useRef('');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ const experimentIdMock = 123;
const user = userEvent.setup();

const Container: React.FC<Props> = (props) => {
const { setAuth } = useAuth();
const { setAuth, setAuthCheck } = useAuth();

useEffect(() => {
setAuth({ isAuthenticated: true });
setAuthCheck();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down
5 changes: 3 additions & 2 deletions webui/react/src/pages/Settings/UserManagement.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ const Container: React.FC = () => {
const { updateCurrentUser } = useCurrentUsers();
const [canceler] = useState(new AbortController());
const fetchUsers = useFetchUsers(canceler);
const { setAuth } = useAuth();
const { setAuth, setAuthCheck } = useAuth();

const loadUsers = useCallback(async () => {
await fetchUsers();
setAuth({ isAuthenticated: true });
setAuthCheck();
updateCurrentUser(currentUser);
}, [fetchUsers, updateCurrentUser, setAuth]);
}, [fetchUsers, setAuthCheck, updateCurrentUser, setAuth]);

useEffect(() => {
loadUsers();
Expand Down
5 changes: 1 addition & 4 deletions webui/react/src/pages/SignIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ const SignIn: React.FC = () => {
const { actions: uiActions } = useUI();
const location = useLocation();
const loadableAuth = useAuth();
const authChecked = Loadable.match(loadableAuth.auth, {
Loaded: (auth) => auth.checked,
NotLoaded: () => false,
});
const authChecked = loadableAuth.authChecked;
const isAuthenticated = Loadable.match(loadableAuth.auth, {
Loaded: (auth) => auth.isAuthenticated,
NotLoaded: () => false,
Expand Down
26 changes: 14 additions & 12 deletions webui/react/src/stores/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import { Auth } from 'types';
import { getCookie, setCookie } from 'utils/browser';
import { Loadable, Loaded, NotLoaded } from 'utils/loadable';

export type CurrentUser = Auth & { checked: boolean };
export type CurrentUser = Auth;

type AuthContext = {
auth: Loadable<CurrentUser>;
authChecked: boolean;
updateAuth: (fn: (users: Loadable<CurrentUser>) => Loadable<CurrentUser>) => void;
updateAuthChecked: (fn: (aChecked: boolean) => boolean) => void;
};

type UseAuthReturn = {
auth: Loadable<CurrentUser>;
authChecked: boolean;
resetAuth: () => void;
setAuth: (auth: Auth) => void;
setAuthCheck: () => void;
Expand All @@ -25,12 +28,15 @@ const AuthContext = createContext<AuthContext | null>(null);

export const AuthProvider: React.FC<PropsWithChildren> = ({ children }) => {
const [auth, setAuth] = useState<Loadable<CurrentUser>>(NotLoaded);
const [authChecked, setAuthChecked] = useState(false);

return (
<AuthContext.Provider
value={{
auth,
authChecked,
updateAuth: setAuth,
updateAuthChecked: setAuthChecked,
}}>
{children}
</AuthContext.Provider>
Expand All @@ -55,14 +61,15 @@ export const useAuth = (): UseAuthReturn => {
if (context === null) {
throw new Error('Attempted to use useAuth outside of Auth Context');
}
const { auth, updateAuth } = context;
const { auth, authChecked, updateAuth, updateAuthChecked } = context;

const resetAuth = useCallback(() => {
clearAuthCookie();
globalStorage.removeAuthToken();

updateAuth(() => NotLoaded);
}, [updateAuth]);
updateAuthChecked(() => false);
}, [updateAuth, updateAuthChecked]);

const setAuth = useCallback(
(auth: Auth) => {
Expand All @@ -82,19 +89,14 @@ export const useAuth = (): UseAuthReturn => {
);

const setAuthCheck = useCallback(() => {
updateAuth((prevState) => {
if (!Loadable.isLoaded(prevState)) return prevState;

const auth = prevState.data;

if (auth.checked) return prevState;

return Loaded({ ...auth, checked: true });
updateAuthChecked(() => {
return true;
});
}, [updateAuth]);
}, [updateAuthChecked]);

return {
auth,
authChecked,
resetAuth,
setAuth,
setAuthCheck,
Expand Down

0 comments on commit ead8c10

Please sign in to comment.