GrantSelector.js 8.1 KB

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