From 6e91f55b815eabb605ae6e569a00071ad36e231f Mon Sep 17 00:00:00 2001 From: Bilal Meddah Date: Mon, 19 Feb 2024 11:05:15 +0100 Subject: [PATCH 1/3] update: pass only the needed param to authManager --- src/authManager.tsx | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/authManager.tsx b/src/authManager.tsx index ac53e9ea1..e8c053f2b 100644 --- a/src/authManager.tsx +++ b/src/authManager.tsx @@ -15,7 +15,9 @@ import { fetchIdentitiesFulfilledAction } from 'shared/store/actions/auth'; const userManagerCache: Map = new Map(); -export const getUserManager = (state: RootState): UserManager | undefined => { +export const getUserManager = ( + state: Pick +): UserManager | undefined => { const { auth: { realms }, config: { clientId, redirectHostName, preferredRealm }, @@ -62,10 +64,6 @@ export const setupUserSession = async (userManager: UserManager) => { userManager.events.addUserLoaded(async user => { loadUser(store, userManager); localStorage.setItem('nexus__token', user.access_token); - const identities = await ( - await fetch(`${store.getState().config.apiEndpoint}/identities`) - ).json(); - store.dispatch(fetchIdentitiesFulfilledAction(identities)); }); // Raised prior to the access token expiring. From 98d4c590a703fa7182fd4e8e67e1f1710ef35f38 Mon Sep 17 00:00:00 2001 From: Bilal Meddah Date: Mon, 19 Feb 2024 11:05:37 +0100 Subject: [PATCH 2/3] update: fetch identities at app loading --- src/shared/App.tsx | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/shared/App.tsx b/src/shared/App.tsx index cec24e286..7b729db2c 100644 --- a/src/shared/App.tsx +++ b/src/shared/App.tsx @@ -1,7 +1,10 @@ import { useSelector } from 'react-redux'; import { ReactQueryDevtools } from 'react-query/devtools'; -import { useQuery } from 'react-query'; +import { useQueries } from 'react-query'; import { useNexusContext } from '@bbp/react-nexus'; +import { ConfigProvider } from 'antd'; +import { antdTheme } from 'theme/antd'; +import { IdentityList } from '@bbp/nexus-sdk/es/types'; import GalleryView from './views/GalleryView'; import routes from '../shared/routes'; import FusionMainLayout from './layouts/FusionMainLayout'; @@ -16,10 +19,9 @@ import { RootState } from './store/reducers'; import DataPanel from './organisms/DataPanel/DataPanel'; import AppInfo from './modals/AppInfo/AppInfo'; import EntityCreation from './modals'; - +import { fetchIdentitiesFulfilledAction } from './store/actions/auth'; +import store from '../store'; import './App.scss'; -import { ConfigProvider } from 'antd'; -import { antdTheme } from 'theme/antd'; const App: React.FC = () => { const nexus = useNexusContext(); @@ -29,15 +31,26 @@ const App: React.FC = () => { const notificationData: NotificationContextType = getNotificationContextValue(); const routesWithSubApps = [...routes, ...subAppRoutes]; - const { data: nexusEcosystem } = useQuery({ - queryKey: ['nexus-ecosystem'], - queryFn: () => - nexus.httpGet({ - path: `${config.apiEndpoint}/version`, - context: { as: 'json' }, - }), - refetchOnWindowFocus: false, - }); + const [{ data: nexusEcosystem }] = useQueries([ + { + queryKey: ['nexus-ecosystem'], + queryFn: () => + nexus.httpGet({ + path: `${config.apiEndpoint}/version`, + context: { as: 'json' }, + }), + refetchOnWindowFocus: false, + }, + { + queryKey: ['nexus-identities'], + queryFn: () => nexus.Identity.list(), + refetchOnWindowFocus: false, + onSuccess: (data: IdentityList) => { + console.log('@@onSuccess', data); + store.dispatch(fetchIdentitiesFulfilledAction(data)); + }, + }, + ]); return ( From fe27f75b89afa62a33bbbb8fe31355e9884a4d4b Mon Sep 17 00:00:00 2001 From: Bilal Meddah Date: Mon, 19 Feb 2024 11:05:55 +0100 Subject: [PATCH 3/3] update: use store --- src/shared/App.tsx | 1 - src/shared/layouts/FusionMainLayout.tsx | 5 +++-- src/shared/modals/CreateProject/CreateProject.tsx | 2 ++ src/shared/store/actions/auth.ts | 1 - 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/shared/App.tsx b/src/shared/App.tsx index 7b729db2c..d81723940 100644 --- a/src/shared/App.tsx +++ b/src/shared/App.tsx @@ -46,7 +46,6 @@ const App: React.FC = () => { queryFn: () => nexus.Identity.list(), refetchOnWindowFocus: false, onSuccess: (data: IdentityList) => { - console.log('@@onSuccess', data); store.dispatch(fetchIdentitiesFulfilledAction(data)); }, }, diff --git a/src/shared/layouts/FusionMainLayout.tsx b/src/shared/layouts/FusionMainLayout.tsx index 532f7ceeb..c9c9181e3 100644 --- a/src/shared/layouts/FusionMainLayout.tsx +++ b/src/shared/layouts/FusionMainLayout.tsx @@ -36,7 +36,7 @@ const FusionMainLayout: React.FC<{ const [consent, setConsent] = useLocalStorage( 'consentToTracking' ); - const state = useSelector((state: RootState) => state); + const auth = useSelector((state: RootState) => state.auth); const oidc = useSelector((state: RootState) => state.oidc); const config = useSelector((state: RootState) => state.config); @@ -44,7 +44,7 @@ const FusionMainLayout: React.FC<{ const token = oidc && oidc.user && !!oidc.user.access_token; const name = oidc.user && oidc.user.profile && oidc.user.profile.preferred_username; - const userManager = getUserManager(state); + const userManager = getUserManager({ config, auth }); const authenticated = !isEmpty(oidc.user); const handleLogout: MenuItemProps['onClick'] = e => { @@ -52,6 +52,7 @@ const FusionMainLayout: React.FC<{ localStorage.removeItem('nexus__state'); userManager && userManager.signoutRedirect(); }; + return ( <> diff --git a/src/shared/modals/CreateProject/CreateProject.tsx b/src/shared/modals/CreateProject/CreateProject.tsx index 1ca9b6a74..4dbf4e083 100644 --- a/src/shared/modals/CreateProject/CreateProject.tsx +++ b/src/shared/modals/CreateProject/CreateProject.tsx @@ -117,6 +117,7 @@ const CreateProject: React.FC<{}> = ({}) => { const nexus = useNexusContext(); const history = useHistory(); const { identities } = useSelector((state: RootState) => state.auth); + const userUri = identities?.data?.identities.find( t => t['@type'] === 'User' )?.['@id']; @@ -162,6 +163,7 @@ const CreateProject: React.FC<{}> = ({}) => { deprecated: false, }), }); + const { mutateAsync, status } = useMutation(createProjectMutation); const handleSubmit = ({ organization, diff --git a/src/shared/store/actions/auth.ts b/src/shared/store/actions/auth.ts index ca365f76e..9fe90edc7 100644 --- a/src/shared/store/actions/auth.ts +++ b/src/shared/store/actions/auth.ts @@ -4,7 +4,6 @@ import { getUserManager } from '../../../authManager'; import { RootState } from '../reducers'; import { ThunkAction } from '..'; import { FetchAction, FetchFulfilledAction, FetchFailedAction } from './utils'; -import { TLocationState } from '../../../pages/IdentityPage/IdentityPage'; export enum AuthActionTypes { IDENTITY_FETCHING = '@@nexus/AUTH_IDENTITY_FETCHING',