GrantSelector.jsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import FormGroup from 'react-bootstrap/es/FormGroup';
  5. import FormControl from 'react-bootstrap/es/FormControl';
  6. import ListGroup from 'react-bootstrap/es/ListGroup';
  7. import ListGroupItem from 'react-bootstrap/es/ListGroupItem';
  8. import Modal from 'react-bootstrap/es/Modal';
  9. import AppContainer from '../../services/AppContainer';
  10. import { createSubscribedElement } from '../UnstatedUtils';
  11. const SPECIFIED_GROUP_VALUE = 'specifiedGroup';
  12. /**
  13. * Page grant select component
  14. *
  15. * @export
  16. * @class GrantSelector
  17. * @extends {React.Component}
  18. */
  19. class GrantSelector extends React.Component {
  20. constructor(props) {
  21. super(props);
  22. this.availableGrants = [
  23. {
  24. grant: 1, iconClass: 'icon-people', styleClass: '', label: 'Public',
  25. },
  26. {
  27. grant: 2, iconClass: 'icon-link', styleClass: 'text-info', label: 'Anyone with the link',
  28. },
  29. // { grant: 3, iconClass: '', label: 'Specified users only' },
  30. {
  31. grant: 4, iconClass: 'icon-lock', styleClass: 'text-danger', label: 'Just me',
  32. },
  33. {
  34. grant: 5, iconClass: 'icon-options', styleClass: '', label: 'Only inside the group',
  35. }, // appeared only one of these 'grant: 5'
  36. {
  37. grant: 5, iconClass: 'icon-options', styleClass: '', label: 'Reselect the group',
  38. }, // appeared only one of these 'grant: 5'
  39. ];
  40. this.state = {
  41. userRelatedGroups: [],
  42. isSelectGroupModalShown: false,
  43. grant: this.props.grant,
  44. grantGroup: null,
  45. };
  46. if (this.props.grantGroupId != null) {
  47. this.state.grantGroup = {
  48. _id: this.props.grantGroupId,
  49. name: this.props.grantGroupName,
  50. };
  51. }
  52. // retrieve xss library from window
  53. this.xss = window.xss;
  54. this.showSelectGroupModal = this.showSelectGroupModal.bind(this);
  55. this.hideSelectGroupModal = this.hideSelectGroupModal.bind(this);
  56. this.getGroupName = this.getGroupName.bind(this);
  57. this.changeGrantHandler = this.changeGrantHandler.bind(this);
  58. this.groupListItemClickHandler = this.groupListItemClickHandler.bind(this);
  59. }
  60. componentDidUpdate(prevProps, prevState) {
  61. /*
  62. * set SPECIFIED_GROUP_VALUE to grant selector
  63. * cz: bootstrap-select input element has the defferent state to React component
  64. */
  65. if (this.state.grantGroup != null) {
  66. this.grantSelectorInputEl.value = SPECIFIED_GROUP_VALUE;
  67. }
  68. // refresh bootstrap-select
  69. // see https://silviomoreto.github.io/bootstrap-select/methods/#selectpickerrefresh
  70. $('.grant-selector .selectpicker').selectpicker('refresh');
  71. // // DIRTY HACK -- 2018.05.25 Yuki Takei
  72. // set group name to the bootstrap-select options
  73. // cz: .selectpicker('refresh') doesn't replace data-content
  74. $('.grant-selector .group-name').text(this.getGroupName());
  75. }
  76. showSelectGroupModal() {
  77. this.retrieveUserGroupRelations();
  78. this.setState({ isSelectGroupModalShown: true });
  79. }
  80. hideSelectGroupModal() {
  81. this.setState({ isSelectGroupModalShown: false });
  82. }
  83. getGroupName() {
  84. const grantGroup = this.state.grantGroup;
  85. return grantGroup ? this.xss.process(grantGroup.name) : '';
  86. }
  87. /**
  88. * Retrieve user-group-relations data from backend
  89. */
  90. retrieveUserGroupRelations() {
  91. this.props.appContainer.apiGet('/me/user-group-relations')
  92. .then((res) => {
  93. return res.userGroupRelations;
  94. })
  95. .then((userGroupRelations) => {
  96. const userRelatedGroups = userGroupRelations.map((relation) => {
  97. return relation.relatedGroup;
  98. });
  99. this.setState({ userRelatedGroups });
  100. });
  101. }
  102. /**
  103. * change event handler for grant selector
  104. */
  105. changeGrantHandler() {
  106. const grant = +this.grantSelectorInputEl.value;
  107. // select group
  108. if (grant === 5) {
  109. this.showSelectGroupModal();
  110. /*
  111. * reset grant selector to state
  112. */
  113. this.grantSelectorInputEl.value = this.state.grant;
  114. return;
  115. }
  116. this.setState({ grant, grantGroup: null });
  117. if (this.props.onUpdateGrant != null) {
  118. this.props.onUpdateGrant({ grant, grantGroupId: null, grantGroupName: null });
  119. }
  120. }
  121. groupListItemClickHandler(grantGroup) {
  122. this.setState({ grant: 5, grantGroup });
  123. if (this.props.onUpdateGrant != null) {
  124. this.props.onUpdateGrant({ grant: 5, grantGroupId: grantGroup._id, grantGroupName: grantGroup.name });
  125. }
  126. // hide modal
  127. this.hideSelectGroupModal();
  128. }
  129. /**
  130. * Render grant selector DOM.
  131. * @returns
  132. * @memberof GrantSelector
  133. */
  134. renderGrantSelector() {
  135. const { t } = this.props;
  136. let index = 0;
  137. let selectedValue = this.state.grant;
  138. const grantElems = this.availableGrants.map((opt) => {
  139. const dataContent = `<i class="icon icon-fw ${opt.iconClass} ${opt.styleClass}"></i> <span class="${opt.styleClass}">${t(opt.label)}</span>`;
  140. return <option key={index++} value={opt.grant} data-content={dataContent}>{t(opt.label)}</option>;
  141. });
  142. const grantGroup = this.state.grantGroup;
  143. if (grantGroup != null) {
  144. selectedValue = SPECIFIED_GROUP_VALUE;
  145. // DIRTY HACK -- 2018.05.25 Yuki Takei
  146. // remove 'Only inside the group' item
  147. // cz: .selectpicker('refresh') doesn't replace data-content
  148. grantElems.splice(3, 1);
  149. }
  150. else {
  151. // DIRTY HACK -- 2018.05.25 Yuki Takei
  152. // remove 'Reselect the group' item
  153. // cz: .selectpicker('refresh') doesn't replace data-content
  154. grantElems.splice(4, 1);
  155. }
  156. /*
  157. * react-bootstrap couldn't be rendered only with React feature.
  158. * see also 'componentDidUpdate'
  159. */
  160. // add specified group option
  161. grantElems.push(
  162. <option
  163. key="specifiedGroupKey"
  164. value={SPECIFIED_GROUP_VALUE}
  165. style={{ display: grantGroup ? 'inherit' : 'none' }}
  166. data-content={`<i class="icon icon-fw icon-organization text-success"></i> <span class="group-name text-success">${this.getGroupName()}</span>`}
  167. >
  168. {this.getGroupName()}
  169. </option>,
  170. );
  171. const bsClassName = 'form-control-dummy'; // set form-control* to shrink width
  172. return (
  173. <FormGroup className="grant-selector m-b-0">
  174. <FormControl
  175. disabled={this.props.disabled}
  176. componentClass="select"
  177. placeholder="select"
  178. defaultValue={selectedValue}
  179. bsClass={bsClassName}
  180. className="btn-group-sm selectpicker"
  181. onChange={this.changeGrantHandler}
  182. inputRef={(el) => { this.grantSelectorInputEl = el }}
  183. >
  184. {grantElems}
  185. </FormControl>
  186. </FormGroup>
  187. );
  188. }
  189. /**
  190. * Render select grantgroup modal.
  191. *
  192. * @returns
  193. * @memberof GrantSelector
  194. */
  195. renderSelectGroupModal() {
  196. const generateGroupListItems = () => {
  197. return this.state.userRelatedGroups.map((group) => {
  198. return (
  199. <ListGroupItem key={group._id} header={group.name} onClick={() => { this.groupListItemClickHandler(group) }}>
  200. (TBD) List group members
  201. </ListGroupItem>
  202. );
  203. });
  204. };
  205. const content = this.state.userRelatedGroups.length === 0
  206. ? (
  207. <div>
  208. <h4>There is no group to which you belong.</h4>
  209. { this.props.appContainer.isAdmin
  210. && <p><a href="/admin/user-groups"><i className="icon icon-fw icon-login"></i> Manage Groups</a></p>
  211. }
  212. </div>
  213. )
  214. : (
  215. <ListGroup>
  216. {generateGroupListItems()}
  217. </ListGroup>
  218. );
  219. return (
  220. <Modal
  221. className="select-grant-group"
  222. container={this}
  223. show={this.state.isSelectGroupModalShown}
  224. onHide={this.hideSelectGroupModal}
  225. >
  226. <Modal.Header closeButton>
  227. <Modal.Title>
  228. Select a Group
  229. </Modal.Title>
  230. </Modal.Header>
  231. <Modal.Body>
  232. {content}
  233. </Modal.Body>
  234. </Modal>
  235. );
  236. }
  237. render() {
  238. return (
  239. <React.Fragment>
  240. { this.renderGrantSelector() }
  241. { !this.props.disabled && this.renderSelectGroupModal() }
  242. </React.Fragment>
  243. );
  244. }
  245. }
  246. /**
  247. * Wrapper component for using unstated
  248. */
  249. const GrantSelectorWrapper = (props) => {
  250. return createSubscribedElement(GrantSelector, props, [AppContainer]);
  251. };
  252. GrantSelector.propTypes = {
  253. t: PropTypes.func.isRequired, // i18next
  254. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  255. disabled: PropTypes.bool,
  256. grant: PropTypes.number.isRequired,
  257. grantGroupId: PropTypes.string,
  258. grantGroupName: PropTypes.string,
  259. onUpdateGrant: PropTypes.func,
  260. };
  261. export default withTranslation()(GrantSelectorWrapper);