Skip to content

Commit

Permalink
Fix fetch identities to allow the user select projects
Browse files Browse the repository at this point in the history
Fix fetch identities to allow the user select projects
  • Loading branch information
bilalesi authored Feb 19, 2024
2 parents 6203676 + fe27f75 commit 4578c2e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
8 changes: 3 additions & 5 deletions src/authManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { fetchIdentitiesFulfilledAction } from 'shared/store/actions/auth';

const userManagerCache: Map<string, UserManager> = new Map();

export const getUserManager = (state: RootState): UserManager | undefined => {
export const getUserManager = (
state: Pick<RootState, 'config' | 'auth'>
): UserManager | undefined => {
const {
auth: { realms },
config: { clientId, redirectHostName, preferredRealm },
Expand Down Expand Up @@ -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.
Expand Down
38 changes: 25 additions & 13 deletions src/shared/App.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();
Expand All @@ -29,15 +31,25 @@ 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) => {
store.dispatch(fetchIdentitiesFulfilledAction(data));
},
},
]);

return (
<ConfigProvider theme={antdTheme}>
Expand Down
5 changes: 3 additions & 2 deletions src/shared/layouts/FusionMainLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,23 @@ const FusionMainLayout: React.FC<{
const [consent, setConsent] = useLocalStorage<ConsentType>(
'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);

const { layoutSettings } = config;
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 => {
e.domEvent.preventDefault();
localStorage.removeItem('nexus__state');
userManager && userManager.signoutRedirect();
};

return (
<>
<SeoHeaders />
Expand Down
2 changes: 2 additions & 0 deletions src/shared/modals/CreateProject/CreateProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -162,6 +163,7 @@ const CreateProject: React.FC<{}> = ({}) => {
deprecated: false,
}),
});

const { mutateAsync, status } = useMutation(createProjectMutation);
const handleSubmit = ({
organization,
Expand Down
1 change: 0 additions & 1 deletion src/shared/store/actions/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 4578c2e

Please sign in to comment.