Skip to content

Commit

Permalink
Merge pull request #142 from CybercentreCanada/hotfix/user_menu
Browse files Browse the repository at this point in the history
Get the apps list directly from the whoami api
  • Loading branch information
cccs-sgaron authored Sep 24, 2021
2 parents 63a379c + 77c437b commit e353dec
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 32 deletions.
46 changes: 15 additions & 31 deletions src/components/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const MyApp = () => {

const provider = getProvider();
const { t } = useTranslation();
const { setUser, setConfiguration, user, configuration } = useALContext();
const { setUser, setConfiguration, user } = useALContext();
const { setReady, setApps } = useAppLayout();
const { showErrorMessage } = useMySnackbar();

Expand All @@ -49,36 +49,6 @@ const MyApp = () => {
}
};

useEffect(() => {
if (user && configuration && configuration.ui.discover_url) {
const discoverOptions: RequestInit = {
method: 'GET',
headers: {
accept: 'application/json'
}
};

fetch(configuration.ui.discover_url, discoverOptions)
.then(res => res.json())
.catch(() => null)
.then(api_data => {
if (api_data) {
setApps(
api_data.applications.application.map(app => {
return {
alt: app.instance[0].metadata.alternateText,
name: app.name,
img_d: app.instance[0].metadata.imageDark,
img_l: app.instance[0].metadata.imageLight,
route: app.instance[0].hostName
};
})
);
}
});
}
}, [setApps, user, configuration]);

useEffect(() => {
if (user || provider) {
return;
Expand All @@ -102,24 +72,38 @@ const MyApp = () => {
.then(api_data => {
// eslint-disable-next-line no-prototype-builtins
if (api_data === undefined || !api_data.hasOwnProperty('api_response')) {
// We got no response
showErrorMessage(t('api.unreachable'), 30000);
switchRenderedApp('load');
} else if (api_data.api_status_code === 403) {
// User account is locked
setConfiguration(api_data.api_response);
switchRenderedApp('locked');
} else if (api_data.api_status_code === 401) {
// User is not logged in
localStorage.setItem('loginParams', JSON.stringify(api_data.api_response));
setLoginParams(api_data.api_response);
switchRenderedApp('login');
} else if (api_data.api_status_code === 200) {
// Set the current user
setUser(api_data.api_response);

//Set the app list?
if (api_data.api_response.configuration.ui.apps) {
setApps(api_data.api_response.configuration.ui.apps);
}

// Mark the interface ready
setReady(true);

// Render appropriate page
if (!api_data.api_response.agrees_with_tos && api_data.api_response.configuration.ui.tos) {
switchRenderedApp('tos');
} else {
switchRenderedApp('routes');
}
} else {
// We got an unexpected result
showErrorMessage(t('api.unreachable'), 30000);
switchRenderedApp('load');
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/hooks/useMyUser.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AppElement } from 'commons/components/layout/topnav/AppSwitcher';
import { UserContextProps, UserProfileProps, ValidatedProp } from 'commons/components/user/UserProvider';
import { ClassificationDefinition } from 'helpers/classificationParser';
import { useState } from 'react';
Expand Down Expand Up @@ -69,11 +70,11 @@ export type ConfigurationDefinition = {
ui: {
allow_malicious_hinting: boolean;
allow_url_submissions: boolean;
apps: AppElement[];
banner: {
[lang: string]: string;
};
banner_level: 'info' | 'warning' | 'error' | 'success';
discover_url: string;
read_only: boolean;
tos: boolean;
tos_lockout: boolean;
Expand Down

0 comments on commit e353dec

Please sign in to comment.