Skip to content

Commit

Permalink
fix: ensure current user available in store when reloading app (#5595)
Browse files Browse the repository at this point in the history
(cherry picked from commit 9c0d261)
  • Loading branch information
mapmeld authored and dzhu committed Dec 20, 2022
1 parent 440880f commit 45ef5f9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
6 changes: 4 additions & 2 deletions webui/react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import usePolling from 'shared/hooks/usePolling';
import { StoreContext } from 'stores';
import { useAuth } from 'stores/auth';
import { initInfo, useDeterminedInfo, useEnsureInfoFetched } from 'stores/determinedInfo';
import { useFetchUsers } from 'stores/users';
import { useEnsureCurrentUserFetched, useFetchUsers } from 'stores/users';
import { correctViewportHeight, refreshPage } from 'utils/browser';
import { Loadable } from 'utils/loadable';

Expand Down Expand Up @@ -53,6 +53,7 @@ const AppView: React.FC = () => {

const fetchInfo = useEnsureInfoFetched(canceler);
const fetchUsers = useFetchUsers(canceler);
const fetchCurrentUser = useEnsureCurrentUserFetched(canceler);

useEffect(() => {
if (isServerReachable) checkAuth();
Expand All @@ -69,8 +70,9 @@ const AppView: React.FC = () => {
useEffect(() => {
if (isAuthenticated) {
fetchUsers();
fetchCurrentUser();
}
}, [isAuthenticated, fetchUsers]);
}, [isAuthenticated, fetchCurrentUser, fetchUsers]);

useEffect(() => {
/*
Expand Down
36 changes: 29 additions & 7 deletions webui/react/src/stores/users.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { createContext, PropsWithChildren, useCallback, useContext, useState } from 'react';

import { getUsers } from 'services/api';
import { getCurrentUser, getUsers } from 'services/api';
import { V1GetUsersRequestSortBy } from 'services/api-ts-sdk';
import { isEqual } from 'shared/utils/data';
import { DetailedUser } from 'types';
Expand Down Expand Up @@ -69,6 +69,28 @@ export const useFetchUsers = (canceler: AbortController): (() => Promise<void>)
);
};

export const useEnsureCurrentUserFetched = (canceler: AbortController): (() => Promise<void>) => {
const context = useContext(UsersContext);

if (context === null) {
throw new Error('Attempted to use useEnsureCurrentUserFetched outside of Users Context');
}

const { updateCurrentUser, currentUser } = context;

return useCallback(async (): Promise<void> => {
if (currentUser !== NotLoaded) return;

try {
const response = await getCurrentUser({ signal: canceler.signal });

updateCurrentUser(() => Loaded(response));
} catch (e) {
handleError(e);
}
}, [canceler, updateCurrentUser, currentUser]);
};

export const useEnsureUsersFetched = (canceler: AbortController): (() => Promise<void>) => {
const context = useContext(UsersContext);

Expand Down Expand Up @@ -111,14 +133,14 @@ export const useCurrentUsers = (): UseCurentUserReturn => {
if (context === null) {
throw new Error('Attempted to use useCurrentUser outside of User Context');
}
const { currentUser, updateCurrentUser: updateContextUser, updateUsers } = context;
const { currentUser, updateCurrentUser, updateUsers } = context;

const updateCurrentUser = useCallback(
const userUpdateCallback = useCallback(
(user: DetailedUser, users: DetailedUser[] = []) => {
const usersArray = [...users];

updateContextUser(() => {
const userIdx = usersArray.findIndex((user) => user.id === user.id);
updateCurrentUser(() => {
const userIdx = usersArray.findIndex((changeUser) => changeUser.id === user.id);

if (userIdx > -1) usersArray[userIdx] = { ...usersArray[userIdx], ...user };

Expand All @@ -133,11 +155,11 @@ export const useCurrentUsers = (): UseCurentUserReturn => {
return Loaded(usersArray);
});
},
[updateContextUser, updateUsers],
[updateCurrentUser, updateUsers],
);

return {
currentUser,
updateCurrentUser,
updateCurrentUser: userUpdateCallback,
};
};

0 comments on commit 45ef5f9

Please sign in to comment.