GrantSelector.jsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 { createSubscribedElement } 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', styleClass: '', label: 'Public',
  24. },
  25. {
  26. grant: 2, iconClass: 'icon-link', styleClass: 'text-info', label: 'Anyone with the link',
  27. },
  28. // { grant: 3, iconClass: '', label: 'Specified users only' },
  29. {
  30. grant: 4, iconClass: 'icon-lock', styleClass: 'text-danger', label: 'Just me',
  31. },
  32. {
  33. grant: 5, iconClass: 'icon-options', styleClass: '', 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 dropdownToggleLabelElm = null;
  113. const dropdownMenuElems = this.availableGrants.map((opt) => {
  114. const label = (opt.grant === 5 && grantGroup != null)
  115. ? opt.reselectLabel // when grantGroup is selected
  116. : opt.label;
  117. const labelElm = <span><i className={`icon icon-fw ${opt.iconClass} ${opt.styleClass}`}></i> <span className={opt.styleClass}>{t(label)}</span></span>;
  118. // set dropdownToggleLabelElm
  119. if (opt.grant === 1 || opt.grant === currentGrant) {
  120. dropdownToggleLabelElm = labelElm;
  121. }
  122. return <DropdownItem key={opt.grant} onClick={() => this.changeGrantHandler(opt.grant)}>{labelElm}</DropdownItem>;
  123. });
  124. // add specified group option
  125. if (grantGroup != null) {
  126. const labelElm = <span><i className="icon icon-fw icon-organization text-success"></i> <span className="text-success">{this.getGroupName()}</span></span>;
  127. // set dropdownToggleLabelElm
  128. dropdownToggleLabelElm = labelElm;
  129. dropdownMenuElems.push(<DropdownItem key="groupSelected">{labelElm}</DropdownItem>);
  130. }
  131. return (
  132. <div className="form-group grw-grant-selector mb-0">
  133. <UncontrolledDropdown direction="up" size="sm">
  134. <DropdownToggle color="light" caret className="d-flex justify-content-between align-items-center" disabled={this.props.disabled}>
  135. {dropdownToggleLabelElm}
  136. </DropdownToggle>
  137. <DropdownMenu>
  138. {dropdownMenuElems}
  139. </DropdownMenu>
  140. </UncontrolledDropdown>
  141. </div>
  142. );
  143. }
  144. /**
  145. * Render select grantgroup modal.
  146. *
  147. * @returns
  148. * @memberof GrantSelector
  149. */
  150. renderSelectGroupModal() {
  151. const generateGroupListItems = () => {
  152. return this.state.userRelatedGroups.map((group) => {
  153. return (
  154. <button key={group._id} type="button" className="list-group-item list-group-item-action" onClick={() => { this.groupListItemClickHandler(group) }}>
  155. <h5>{group.name}</h5>
  156. <div className="small">(TBD) List group members</div>
  157. </button>
  158. );
  159. });
  160. };
  161. const content = this.state.userRelatedGroups.length === 0
  162. ? (
  163. <div>
  164. <h4>There is no group to which you belong.</h4>
  165. { this.props.appContainer.isAdmin
  166. && <p><a href="/admin/user-groups"><i className="icon icon-fw icon-login"></i> Manage Groups</a></p>
  167. }
  168. </div>
  169. )
  170. : (
  171. <div className="list-group">
  172. {generateGroupListItems()}
  173. </div>
  174. );
  175. return (
  176. <Modal
  177. className="select-grant-group"
  178. container={this}
  179. isOpen={this.state.isSelectGroupModalShown}
  180. toggle={this.hideSelectGroupModal}
  181. >
  182. <ModalHeader tag="h4" toggle={this.hideSelectGroupModal}>
  183. Select a Group
  184. </ModalHeader>
  185. <ModalBody>
  186. {content}
  187. </ModalBody>
  188. </Modal>
  189. );
  190. }
  191. render() {
  192. return (
  193. <React.Fragment>
  194. { this.renderGrantSelector() }
  195. { !this.props.disabled && this.renderSelectGroupModal() }
  196. </React.Fragment>
  197. );
  198. }
  199. }
  200. /**
  201. * Wrapper component for using unstated
  202. */
  203. const GrantSelectorWrapper = (props) => {
  204. return createSubscribedElement(GrantSelector, props, [AppContainer]);
  205. };
  206. GrantSelector.propTypes = {
  207. t: PropTypes.func.isRequired, // i18next
  208. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  209. disabled: PropTypes.bool,
  210. grant: PropTypes.number.isRequired,
  211. grantGroupId: PropTypes.string,
  212. grantGroupName: PropTypes.string,
  213. onUpdateGrant: PropTypes.func,
  214. };
  215. export default withTranslation()(GrantSelectorWrapper);