Commit efe9e1e3 authored by Marjolaine Bodin's avatar Marjolaine Bodin
Browse files

#522 wip switch affiliation

parent 81fc6aed
......@@ -31,6 +31,7 @@ export function doSignIn(plugin, username, password) {
isAdministrator,
isInstrumentScientist,
lifeTimeMinutes,
affiliations,
} = data;
dispatch({
type: LOGGED_IN,
......@@ -40,6 +41,7 @@ export function doSignIn(plugin, username, password) {
isAdministrator,
isInstrumentScientist,
lifeTimeMinutes,
affiliations,
});
})
.catch((error) => {
......@@ -69,6 +71,7 @@ export function doSilentRefreshFromSSO() {
isAdministrator,
isInstrumentScientist,
lifeTimeMinutes,
affiliations,
} = data;
dispatch({
type: LOGGED_IN,
......@@ -78,6 +81,7 @@ export function doSilentRefreshFromSSO() {
isAdministrator,
isInstrumentScientist,
lifeTimeMinutes,
affiliations,
});
})
.catch((error) => {
......@@ -86,3 +90,44 @@ export function doSilentRefreshFromSSO() {
});
};
}
export function switchAffiliation(name) {
return (dispatch) => {
axios
.post(`${ICATPLUS.server}/session`, {
plugin: ICAT.authentication.sso.plugin,
username: null,
password: keycloak.idToken,
smisPk: name.split('-')[1],
})
.then(({ data }) => {
if (!data || !data.sessionId) {
throw new Error('Invalid switch authentication response');
}
const {
sessionId,
name,
fullName,
isAdministrator,
isInstrumentScientist,
lifeTimeMinutes,
affiliations,
} = data;
dispatch({
type: LOGGED_IN,
sessionId,
name,
fullName,
isAdministrator,
isInstrumentScientist,
lifeTimeMinutes,
affiliations,
});
})
.catch((error) => {
console.error(error);
dispatch({ type: LOGIN_ERROR, error: 'Switch authentication failed' });
});
};
}
import React from 'react';
import { MenuItem, NavDropdown } from 'react-bootstrap';
import { useSelector } from 'react-redux';
import { dateFormatter } from '../Investigation/utils';
import { switchAffiliation } from '../../actions/login';
function AffiliationMenu() {
const { affiliations } = useSelector((state) => state.user);
function getAffiliationName(createTime) {
return dateFormatter(createTime, 'DD/MM/YYYY', false);
}
function handleAffiliation(name) {
switchAffiliation(name);
}
return (
<>
{affiliations && affiliations.length > 0 && (
<NavDropdown
eventKey="switchAffiliation"
title="Switch Affiliation"
id="basic-nav-dropdown"
>
{affiliations.map(({ name, createTime }) => (
<MenuItem eventKey={name} onClick={handleAffiliation(name)}>
{getAffiliationName(createTime)}
</MenuItem>
))}
</NavDropdown>
)}
;
</>
);
}
export default AffiliationMenu;
......@@ -11,6 +11,7 @@ import { useSelector, useDispatch } from 'react-redux';
import { doLogOut } from '../../actions/login';
import styles from './Menu.module.css';
import LoadingBoundary from '../LoadingBoundary';
import AffiliationMenu from './AffiliationMenu';
function Menu() {
const user = useSelector((state) => state.user);
......@@ -142,6 +143,7 @@ function Menu() {
</Nav>
<Nav pullRight>
<AffiliationMenu></AffiliationMenu>
<NavItem eventKey="logout" onClick={handleLogout}>
<Glyphicon glyph="log-out" style={{ marginRight: '1rem' }} />
Log out <strong>{fullName}</strong>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment