import React from 'react'; import { Fa } from '../components'; import { app } from '../core/app'; import { Label } from 'react-bootstrap'; export default class SessionUtils { /** * Redirect to the home page of the application * @return {[type]} [description] */ static gotoHome() { window.location.hash = SessionUtils.homeHash(); } /** * Return the URL hat must be used in the home link of the toolbar * @return {[type]} [description] */ static homeHash() { if (app.storage.state.app.clientMode) { return SessionUtils.unitHash(); } const session = app.getState().session; if (session.unitsCount === 1) { return SessionUtils.unitHash(); } switch (session.view) { case 'COUNTRY': return SessionUtils.workspaceHash(); case 'ADMINUNIT': return '#/sys/adminunit?id=' + session.adminUnit.id; // default is the unit page default: return SessionUtils.unitHash(); } } static workspaceHash(defaultView) { return '#/sys/workspace' + (defaultView ? defaultView : ''); } static unitHash(unitId, defaultView) { const url = '#/sys/unit' + (defaultView ? defaultView : '') + '?id='; return url + (unitId ? unitId : app.getState().session.unitId); } static adminUnitHash(auId, defaultView) { return '#/sys/adminunit' + (defaultView ? defaultView : '') + '?id=' + auId; } static caseHash(caseId) { return '#/sys/case?id=' + caseId; } /** * Generate a node component to display the full name of an administrative unit * followed by its links. If addWorkspace is true, a second line is included with the workspace name * @param {[type]} adminUnit The object containing data about an administrative unit * @param {[type]} addWorkspace If true, include the workspace under the name of the workspace * @return {[type]} [description] */ static adminUnitLink(adminUnit, addWorkspace, addMain, defaultView) { if (app.storage.state.app.clientMode) { return this.adminUnitDisplay(adminUnit, addWorkspace, addMain); } const lst = []; // admin unit was informed ? if (adminUnit) { // add link of main item if (addMain) { lst.push({adminUnit.name}); } // add links of parents for (var k = 3; k >= 0; k--) { const p = adminUnit['p' + k]; if (p) { if (lst.length > 0) { lst.push({', '}); } const hash = SessionUtils.adminUnitHash(p.id); lst.push({p.name}); } } } return ( {lst} { addWorkspace && } ); } /** * Generate a node component to display the full name of an administrative unit * If addWorkspace is true, a second line is included with the workspace name * @param {[type]} adminUnit [description] * @param {[type]} addWorkspace [description] * @return {[type]} [description] */ static adminUnitDisplay(adminUnit, addWorkspace, addMain) { let unitName; // admin unit was informed ? if (adminUnit) { const lst = [adminUnit.name]; // add links of parents for (var k = 3; k >= 0; k--) { const p = adminUnit['p' + k]; if (p && p.name) { lst.push(p.name); } } // add link of main item if (addMain) { lst.push(adminUnit.name); } unitName = lst.join(', '); } return (