import React from 'react'; import PropTypes from 'prop-types'; import { withTranslation } from 'react-i18next'; import { UncontrolledDropdown, DropdownToggle, DropdownMenu, DropdownItem, Modal, ModalHeader, ModalBody, } from 'reactstrap'; import AppContainer from '../../services/AppContainer'; import { withUnstatedContainers } from '../UnstatedUtils'; /** * Page grant select component * * @export * @class GrantSelector * @extends {React.Component} */ class GrantSelector extends React.Component { constructor(props) { super(props); this.availableGrants = [ { grant: 1, iconClass: 'icon-people', btnStyleClass: 'outline-info', label: 'Public', }, { grant: 2, iconClass: 'icon-link', btnStyleClass: 'outline-teal', label: 'Anyone with the link', }, // { grant: 3, iconClass: '', label: 'Specified users only' }, { grant: 4, iconClass: 'icon-lock', btnStyleClass: 'outline-danger', label: 'Only me', }, { grant: 5, iconClass: 'icon-options', btnStyleClass: 'outline-purple', label: 'Only inside the group', reselectLabel: 'Reselect the group', }, ]; this.state = { userRelatedGroups: [], isSelectGroupModalShown: false, }; this.showSelectGroupModal = this.showSelectGroupModal.bind(this); this.hideSelectGroupModal = this.hideSelectGroupModal.bind(this); this.changeGrantHandler = this.changeGrantHandler.bind(this); this.groupListItemClickHandler = this.groupListItemClickHandler.bind(this); } showSelectGroupModal() { this.retrieveUserGroupRelations(); this.setState({ isSelectGroupModalShown: true }); } hideSelectGroupModal() { this.setState({ isSelectGroupModalShown: false }); } /** * Retrieve user-group-relations data from backend */ retrieveUserGroupRelations() { this.props.appContainer.apiGet('/me/user-group-relations') .then((res) => { return res.userGroupRelations; }) .then((userGroupRelations) => { const userRelatedGroups = userGroupRelations.map((relation) => { return relation.relatedGroup; }); this.setState({ userRelatedGroups }); }); } /** * change event handler for grant selector */ changeGrantHandler(grant) { // select group if (grant === 5) { this.showSelectGroupModal(); return; } if (this.props.onUpdateGrant != null) { this.props.onUpdateGrant({ grant, grantGroupId: null, grantGroupName: null }); } } groupListItemClickHandler(grantGroup) { if (this.props.onUpdateGrant != null) { this.props.onUpdateGrant({ grant: 5, grantGroupId: grantGroup._id, grantGroupName: grantGroup.name }); } // hide modal this.hideSelectGroupModal(); } /** * Render grant selector DOM. * @returns * @memberof GrantSelector */ renderGrantSelector() { const { t } = this.props; const { grant: currentGrant, grantGroupId } = this.props; let dropdownToggleBtnColor = null; let dropdownToggleLabelElm = null; const dropdownMenuElems = this.availableGrants.map((opt) => { const label = (opt.grant === 5 && grantGroupId != null) ? opt.reselectLabel // when grantGroup is selected : opt.label; const labelElm = ( {t(label)} ); // set dropdownToggleBtnColor, dropdownToggleLabelElm if (opt.grant === 1 || opt.grant === currentGrant) { dropdownToggleBtnColor = opt.btnStyleClass; dropdownToggleLabelElm = labelElm; } return this.changeGrantHandler(opt.grant)}>{labelElm}; }); // add specified group option if (grantGroupId != null) { const labelElm = ( {this.props.grantGroupName} ); // set dropdownToggleLabelElm dropdownToggleLabelElm = labelElm; dropdownMenuElems.push({labelElm}); } return (
{dropdownToggleLabelElm} {dropdownMenuElems}
); } /** * Render select grantgroup modal. * * @returns * @memberof GrantSelector */ renderSelectGroupModal() { const generateGroupListItems = () => { return this.state.userRelatedGroups.map((group) => { return ( ); }); }; const content = this.state.userRelatedGroups.length === 0 ? (

There is no group to which you belong.

{ this.props.appContainer.isAdmin &&

Manage Groups

}
) : (
{generateGroupListItems()}
); return ( Select a Group {content} ); } render() { return ( { this.renderGrantSelector() } { !this.props.disabled && this.renderSelectGroupModal() } ); } } /** * Wrapper component for using unstated */ const GrantSelectorWrapper = withUnstatedContainers(GrantSelector, [AppContainer]); GrantSelector.propTypes = { t: PropTypes.func.isRequired, // i18next appContainer: PropTypes.instanceOf(AppContainer).isRequired, disabled: PropTypes.bool, grant: PropTypes.number.isRequired, grantGroupId: PropTypes.string, grantGroupName: PropTypes.string, onUpdateGrant: PropTypes.func, }; export default withTranslation()(GrantSelectorWrapper);