GrantSelector.jsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import {
  5. UncontrolledDropdown,
  6. DropdownToggle, DropdownMenu, DropdownItem,
  7. Modal, ModalHeader, ModalBody,
  8. } from 'reactstrap';
  9. import AppContainer from '../../services/AppContainer';
  10. import { withUnstatedContainers } from '../UnstatedUtils';
  11. /**
  12. * Page grant select component
  13. *
  14. * @export
  15. * @class GrantSelector
  16. * @extends {React.Component}
  17. */
  18. class GrantSelector extends React.Component {
  19. constructor(props) {
  20. super(props);
  21. this.availableGrants = [
  22. {
  23. grant: 1, iconClass: 'icon-people', btnStyleClass: 'outline-info', label: 'Public',
  24. },
  25. {
  26. grant: 2, iconClass: 'icon-link', btnStyleClass: 'outline-teal', label: 'Anyone with the link',
  27. },
  28. // { grant: 3, iconClass: '', label: 'Specified users only' },
  29. {
  30. grant: 4, iconClass: 'icon-lock', btnStyleClass: 'outline-danger', label: 'Only me',
  31. },
  32. {
  33. grant: 5, iconClass: 'icon-options', btnStyleClass: 'outline-purple', label: 'Only inside the group', reselectLabel: 'Reselect the group',
  34. },
  35. ];
  36. this.state = {
  37. userRelatedGroups: [],
  38. isSelectGroupModalShown: false,
  39. grant: this.props.grant,
  40. grantGroup: null,
  41. };
  42. if (this.props.grantGroupId != null) {
  43. this.state.grantGroup = {
  44. _id: this.props.grantGroupId,
  45. name: this.props.grantGroupName,
  46. };
  47. }
  48. // retrieve xss library from window
  49. this.xss = window.xss;
  50. this.showSelectGroupModal = this.showSelectGroupModal.bind(this);
  51. this.hideSelectGroupModal = this.hideSelectGroupModal.bind(this);
  52. this.getGroupName = this.getGroupName.bind(this);
  53. this.changeGrantHandler = this.changeGrantHandler.bind(this);
  54. this.groupListItemClickHandler = this.groupListItemClickHandler.bind(this);
  55. }
  56. showSelectGroupModal() {
  57. this.retrieveUserGroupRelations();
  58. this.setState({ isSelectGroupModalShown: true });
  59. }
  60. hideSelectGroupModal() {
  61. this.setState({ isSelectGroupModalShown: false });
  62. }
  63. getGroupName() {
  64. const grantGroup = this.state.grantGroup;
  65. return grantGroup ? this.xss.process(grantGroup.name) : '';
  66. }
  67. /**
  68. * Retrieve user-group-relations data from backend
  69. */
  70. retrieveUserGroupRelations() {
  71. this.props.appContainer.apiGet('/me/user-group-relations')
  72. .then((res) => {
  73. return res.userGroupRelations;
  74. })
  75. .then((userGroupRelations) => {
  76. const userRelatedGroups = userGroupRelations.map((relation) => {
  77. return relation.relatedGroup;
  78. });
  79. this.setState({ userRelatedGroups });
  80. });
  81. }
  82. /**
  83. * change event handler for grant selector
  84. */
  85. changeGrantHandler(grant) {
  86. // select group
  87. if (grant === 5) {
  88. this.showSelectGroupModal();
  89. return;
  90. }
  91. this.setState({ grant, grantGroup: null });
  92. if (this.props.onUpdateGrant != null) {
  93. this.props.onUpdateGrant({ grant, grantGroupId: null, grantGroupName: null });
  94. }
  95. }
  96. groupListItemClickHandler(grantGroup) {
  97. this.setState({ grant: 5, grantGroup });
  98. if (this.props.onUpdateGrant != null) {
  99. this.props.onUpdateGrant({ grant: 5, grantGroupId: grantGroup._id, grantGroupName: grantGroup.name });
  100. }
  101. // hide modal
  102. this.hideSelectGroupModal();
  103. }
  104. /**
  105. * Render grant selector DOM.
  106. * @returns
  107. * @memberof GrantSelector
  108. */
  109. renderGrantSelector() {
  110. const { t } = this.props;
  111. const { grant: currentGrant, grantGroup } = this.state;
  112. let dropdownToggleBtnColor = null;
  113. let dropdownToggleLabelElm = null;
  114. const dropdownMenuElems = this.availableGrants.map((opt) => {
  115. const label = (opt.grant === 5 && grantGroup != null)
  116. ? opt.reselectLabel // when grantGroup is selected
  117. : opt.label;
  118. const labelElm = (
  119. <span>
  120. <i className={`icon icon-fw ${opt.iconClass}`}></i>
  121. <span className="label">{t(label)}</span>
  122. </span>
  123. );
  124. // set dropdownToggleBtnColor, dropdownToggleLabelElm
  125. if (opt.grant === 1 || opt.grant === currentGrant) {
  126. dropdownToggleBtnColor = opt.btnStyleClass;
  127. dropdownToggleLabelElm = labelElm;
  128. }
  129. return <DropdownItem key={opt.grant} onClick={() => this.changeGrantHandler(opt.grant)}>{labelElm}</DropdownItem>;
  130. });
  131. // add specified group option
  132. if (grantGroup != null) {
  133. const labelElm = (
  134. <span>
  135. <i className="icon icon-fw icon-organization"></i>
  136. <span className="label">{this.getGroupName()}</span>
  137. </span>
  138. );
  139. // set dropdownToggleLabelElm
  140. dropdownToggleLabelElm = labelElm;
  141. dropdownMenuElems.push(<DropdownItem key="groupSelected">{labelElm}</DropdownItem>);
  142. }
  143. return (
  144. <div className="form-group grw-grant-selector mb-0">
  145. <UncontrolledDropdown direction="up">
  146. <DropdownToggle color={dropdownToggleBtnColor} caret className="d-flex justify-content-between align-items-center" disabled={this.props.disabled}>
  147. {dropdownToggleLabelElm}
  148. </DropdownToggle>
  149. <DropdownMenu>
  150. {dropdownMenuElems}
  151. </DropdownMenu>
  152. </UncontrolledDropdown>
  153. </div>
  154. );
  155. }
  156. /**
  157. * Render select grantgroup modal.
  158. *
  159. * @returns
  160. * @memberof GrantSelector
  161. */
  162. renderSelectGroupModal() {
  163. const generateGroupListItems = () => {
  164. return this.state.userRelatedGroups.map((group) => {
  165. return (
  166. <button key={group._id} type="button" className="list-group-item list-group-item-action" onClick={() => { this.groupListItemClickHandler(group) }}>
  167. <h5>{group.name}</h5>
  168. <div className="small">(TBD) List group members</div>
  169. </button>
  170. );
  171. });
  172. };
  173. const content = this.state.userRelatedGroups.length === 0
  174. ? (
  175. <div>
  176. <h4>There is no group to which you belong.</h4>
  177. { this.props.appContainer.isAdmin
  178. && <p><a href="/admin/user-groups"><i className="icon icon-fw icon-login"></i> Manage Groups</a></p>
  179. }
  180. </div>
  181. )
  182. : (
  183. <div className="list-group">
  184. {generateGroupListItems()}
  185. </div>
  186. );
  187. return (
  188. <Modal
  189. className="select-grant-group"
  190. container={this}
  191. isOpen={this.state.isSelectGroupModalShown}
  192. toggle={this.hideSelectGroupModal}
  193. >
  194. <ModalHeader tag="h4" toggle={this.hideSelectGroupModal} className="bg-purple text-light">
  195. Select a Group
  196. </ModalHeader>
  197. <ModalBody>
  198. {content}
  199. </ModalBody>
  200. </Modal>
  201. );
  202. }
  203. render() {
  204. return (
  205. <React.Fragment>
  206. { this.renderGrantSelector() }
  207. { !this.props.disabled && this.renderSelectGroupModal() }
  208. </React.Fragment>
  209. );
  210. }
  211. }
  212. /**
  213. * Wrapper component for using unstated
  214. */
  215. const GrantSelectorWrapper = withUnstatedContainers(GrantSelector, [AppContainer]);
  216. GrantSelector.propTypes = {
  217. t: PropTypes.func.isRequired, // i18next
  218. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  219. disabled: PropTypes.bool,
  220. grant: PropTypes.number.isRequired,
  221. grantGroupId: PropTypes.string,
  222. grantGroupName: PropTypes.string,
  223. onUpdateGrant: PropTypes.func,
  224. };
  225. export default withTranslation()(GrantSelectorWrapper);