UNPKG

d2-ui

Version:
132 lines (115 loc) 4.49 kB
import log from 'loglevel'; import { Observable } from 'rxjs'; import { getInstance, config } from 'd2/lib/d2'; import getBaseUrlFromD2ApiUrl from '../utils/getBaseUrlFromD2ApiUrl'; // Profile menu config.i18n.strings.add('settings'); config.i18n.strings.add('profile'); config.i18n.strings.add('account'); config.i18n.strings.add('help'); config.i18n.strings.add('log_out'); config.i18n.strings.add('about_dhis2'); const profileMenuData = [ { name: 'settings', namespace: '/dhis-web-user-profile', defaultAction: '/dhis-web-user-profile/#/settings', icon: '/icons/usersettings.png', description: '', }, { name: 'profile', namespace: '/dhis-web-user-profile', defaultAction: '/dhis-web-user-profile/#/profile', icon: '/icons/function-profile.png', description: '', }, { name: 'account', namespace: '/dhis-web-user-profile', defaultAction: '/dhis-web-user-profile/#/account', icon: '/icons/function-account.png', description: '', }, { name: 'help', namespace: '/dhis-web-commons-about', defaultAction: 'https://docs.dhis2.org/master/en/user/html/dhis2_user_manual_en.html', icon: '/icons/function-account.png', description: '', }, { name: 'about_dhis2', namespace: '/dhis-web-commons-about', defaultAction: '/dhis-web-commons-about/about.action', icon: '/icons/function-about-dhis2.png', description: '', }, ]; const addHelpLinkToProfileData = () => getInstance() .then(d2 => d2.system.settings.get('helpPageLink')) // When the request for the system setting fails we return false to not set the help link .catch(() => false) .then(helpPageLink => profileMenuData .map((profileMenuItem) => { // Override the defaultAction with the helpPageLink when one was found. if (helpPageLink && profileMenuItem.name === 'help') { return Object.assign({}, profileMenuItem, { defaultAction: helpPageLink }); } return profileMenuItem; }), ); export const profileSource$ = Observable.fromPromise(addHelpLinkToProfileData(profileMenuData)); // TODO: Remove this when we have proper support for `displayName` from the getModules.action. const getTranslationsForMenuItems = ({ modules }) => getInstance() .then((d2) => { const api = d2.Api.getApi(); const moduleNames = modules.map(module => module.name); return api.post('i18n', moduleNames); }) .then((translations) => { const translatedModules = modules.map(module => Object.assign({ ...module }, { displayName: translations[module.name] || module.name })); return { modules: translatedModules }; }) .catch(() => { log.warn('Could not load translations for modules, defaulting back to English'); return { modules }; }); /** * Module management is available though the More Apps button. We therefore do not display it in the menu as a separate item. * * @param modules * @returns {{modules: [module]}} */ const removeMenuManagementModule = ({ modules }) => ({ modules: modules.filter(module => module.name !== 'dhis-web-menu-management'), }); const loadMenuItems = () => getInstance() .then((d2) => { const api = d2.Api.getApi(); const baseUrl = getBaseUrlFromD2ApiUrl(d2); // This path is only correct when the manifest has '..' as the baseUrl and a versioned api endpoint is used // TODO: This should have a proper API endpoint return api.get(`${baseUrl}/dhis-web-commons/menu/getModules.action?_=${(new Date()).getTime()}`); }) .then(getTranslationsForMenuItems) .then(removeMenuManagementModule) .then(({ modules }) => modules); const loadNotifications = () => getInstance() .then((d2) => { const api = d2.Api.getApi(); return api.get('me/dashboard'); }); export const appsMenuSource$ = Observable .fromPromise(loadMenuItems()) .catch(Observable.of([])); export const notifications$ = Observable .fromPromise(loadNotifications()) .catch(Observable.of({ unreadInterpretations: 0, unreadMessageConversations: 0, }));