GrantSelector.jsx 8.9 KB

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