GrantSelector.jsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. { grant: 1, iconClass: 'icon-people', styleClass: '', label: 'Public' },
  22. { grant: 2, iconClass: 'icon-link', styleClass: 'text-info', label: 'Anyone with the link' },
  23. // { grant: 3, iconClass: '', label: 'Specified users only' },
  24. { grant: 4, iconClass: 'icon-lock', styleClass: 'text-danger', label: 'Just me' },
  25. { grant: 5, iconClass: 'icon-options', styleClass: '', label: 'Only inside the group' }, // appeared only one of these 'grant: 5'
  26. { grant: 5, iconClass: 'icon-options', styleClass: '', label: 'Reselect the group' }, // appeared only one of these 'grant: 5'
  27. ];
  28. this.state = {
  29. grant: this.props.grant || 1, // default: 1
  30. userRelatedGroups: [],
  31. isSelectGroupModalShown: false,
  32. };
  33. if (this.props.grantGroupId !== '') {
  34. this.state.grantGroup = {
  35. _id: this.props.grantGroupId,
  36. name: this.props.grantGroupName
  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.grantGroup != null) {
  54. this.grantSelectorInputEl.value = SPECIFIED_GROUP_VALUE;
  55. }
  56. // refresh bootstrap-select
  57. // see https://silviomoreto.github.io/bootstrap-select/methods/#selectpickerrefresh
  58. $('.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. $('.grant-selector .group-name').text(this.getGroupName());
  63. }
  64. getCurrentOptionsToSave() {
  65. const options = {
  66. grant: this.state.grant
  67. };
  68. if (this.state.grantGroup != null) {
  69. options.grantUserGroupId = this.state.grantGroup._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 grantGroup = this.state.grantGroup;
  82. return grantGroup ? this.xss.process(grantGroup.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 grant selector
  101. */
  102. changeGrantHandler() {
  103. const grant = +this.grantSelectorInputEl.value;
  104. // select group
  105. if (grant === 5) {
  106. this.showSelectGroupModal();
  107. /*
  108. * reset grant selector to state
  109. */
  110. this.grantSelectorInputEl.value = this.state.grant;
  111. return;
  112. }
  113. this.setState({ grant, grantGroup: null });
  114. }
  115. groupListItemClickHandler(grantGroup) {
  116. this.setState({ grant: 5, grantGroup });
  117. // hide modal
  118. this.hideSelectGroupModal();
  119. }
  120. /**
  121. * Render grant selector DOM.
  122. * @returns
  123. * @memberof GrantSelector
  124. */
  125. renderGrantSelector() {
  126. const { t } = this.props;
  127. let index = 0;
  128. let selectedValue = this.state.grant;
  129. const grantElems = this.availableGrants.map((opt) => {
  130. const dataContent = `<i class="icon icon-fw ${opt.iconClass} ${opt.styleClass}"></i> <span class="${opt.styleClass}">${t(opt.label)}</span>`;
  131. return <option key={index++} value={opt.grant} data-content={dataContent}>{t(opt.label)}</option>;
  132. });
  133. const grantGroup = this.state.grantGroup;
  134. if (grantGroup != null) {
  135. selectedValue = SPECIFIED_GROUP_VALUE;
  136. // DIRTY HACK -- 2018.05.25 Yuki Takei
  137. // remove 'Only inside the group' item
  138. // cz: .selectpicker('refresh') doesn't replace data-content
  139. grantElems.splice(3, 1);
  140. }
  141. else {
  142. // DIRTY HACK -- 2018.05.25 Yuki Takei
  143. // remove 'Reselect the group' item
  144. // cz: .selectpicker('refresh') doesn't replace data-content
  145. grantElems.splice(4, 1);
  146. }
  147. /*
  148. * react-bootstrap couldn't be rendered only with React feature.
  149. * see also 'componentDidUpdate'
  150. */
  151. // add specified group option
  152. grantElems.push(
  153. <option ref="specifiedGroupOption" key="specifiedGroupKey" value={SPECIFIED_GROUP_VALUE} style={{ display: grantGroup ? 'inherit' : 'none' }}
  154. data-content={`<i class="icon icon-fw icon-organization text-success"></i> <span class="group-name text-success">${this.getGroupName()}</span>`}>
  155. {this.getGroupName()}
  156. </option>
  157. );
  158. const bsClassName = 'form-control-dummy'; // set form-control* to shrink width
  159. return (
  160. <FormGroup className="grant-selector m-b-0">
  161. <FormControl componentClass="select" placeholder="select" defaultValue={selectedValue} bsClass={bsClassName} className="btn-group-sm selectpicker"
  162. onChange={this.changeGrantHandler}
  163. inputRef={ el => this.grantSelectorInputEl=el }>
  164. {grantElems}
  165. </FormControl>
  166. </FormGroup>
  167. );
  168. }
  169. /**
  170. * Render select grantgroup modal.
  171. *
  172. * @returns
  173. * @memberof GrantSelector
  174. */
  175. renderSelectGroupModal() {
  176. const generateGroupListItems = () => {
  177. return this.state.userRelatedGroups.map((group) => {
  178. return <ListGroupItem key={group._id} header={group.name} onClick={() => { this.groupListItemClickHandler(group) }}>
  179. (TBD) List group members
  180. </ListGroupItem>;
  181. });
  182. };
  183. let content = this.state.userRelatedGroups.length === 0
  184. ? <div>
  185. <h4>There is no group to which you belong.</h4>
  186. { this.props.crowi.isAdmin &&
  187. <p><a href="/admin/user-groups"><i className="icon icon-fw icon-login"></i> Manage Groups</a></p>
  188. }
  189. </div>
  190. : <ListGroup>
  191. {generateGroupListItems()}
  192. </ListGroup>;
  193. return (
  194. <Modal className="select-grant-group"
  195. container={this} show={this.state.isSelectGroupModalShown} onHide={this.hideSelectGroupModal}
  196. >
  197. <Modal.Header closeButton>
  198. <Modal.Title>
  199. Select a Group
  200. </Modal.Title>
  201. </Modal.Header>
  202. <Modal.Body>
  203. {content}
  204. </Modal.Body>
  205. </Modal>
  206. );
  207. }
  208. render() {
  209. return <React.Fragment>
  210. {this.renderGrantSelector()}
  211. {this.renderSelectGroupModal()}
  212. </React.Fragment>;
  213. }
  214. }
  215. GrantSelector.propTypes = {
  216. t: PropTypes.func.isRequired, // i18next
  217. crowi: PropTypes.object.isRequired,
  218. grant: PropTypes.number,
  219. grantGroupId: PropTypes.string,
  220. grantGroupName: PropTypes.string,
  221. };
  222. export default translate()(GrantSelector);