cspace-ui
Version:
CollectionSpace user interface for browsers
63 lines (53 loc) • 1.85 kB
JavaScript
import Immutable from 'immutable';
import get from 'lodash/get';
import { getPermissions } from '../helpers/permissionHelpers';
import {
CSPACE_CONFIGURED,
ACCOUNT_PERMS_READ_FULFILLED,
SET_ACCOUNT_PERMS,
AUTH_RENEW_FULFILLED,
LOGIN_FULFILLED,
LOGOUT_FULFILLED,
} from '../constants/actionCodes';
const handleAccountPermsReadFulfilled = (state, action) => {
const {
config,
} = action.meta;
const {
data,
} = action.payload;
const account = Immutable.fromJS(get(data, ['ns2:account_permission', 'account']));
const accountTenantId = account.get('tenantId');
const perms = (accountTenantId === config.tenantId)
? getPermissions(action.meta.config, data)
: Immutable.Map();
return (
state
.set('account', account)
.set('perms', perms)
);
};
export default (state = Immutable.Map(), action) => {
switch (action.type) {
case ACCOUNT_PERMS_READ_FULFILLED:
return handleAccountPermsReadFulfilled(state, action);
case AUTH_RENEW_FULFILLED:
return handleAccountPermsReadFulfilled(state, action);
case CSPACE_CONFIGURED:
return state.set('username', action.payload.username);
case LOGIN_FULFILLED:
return state.set('username', action.meta.username);
case LOGOUT_FULFILLED:
return state.clear();
case SET_ACCOUNT_PERMS:
// There is no action creator that creates this action, but it's useful for testing via Redux
// dev tools.
return state.set('perms', state.get('perms').mergeDeep(Immutable.fromJS(action.payload)));
default:
return state;
}
};
export const getUsername = state => state.get('username');
export const getScreenName = state => state.getIn(['account', 'screenName']);
export const getUserId = state => state.getIn(['account', 'userId']);
export const getPerms = state => state.get('perms');