GrantSelector.js 8.6 KB

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