GrantSelector.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 ControlLabel from 'react-bootstrap/es/ControlLabel';
  7. // import Button from 'react-bootstrap/es/Button';
  8. // import Modal from 'react-bootstrap/es/Modal';
  9. /**
  10. * Page grant select component
  11. *
  12. * @export
  13. * @class GrantSelector
  14. * @extends {React.Component}
  15. */
  16. class GrantSelector extends React.Component {
  17. constructor(props) {
  18. super(props);
  19. this.state = {
  20. pageGrant: this.props.pageGrant,
  21. isGroupModalShown: false,
  22. };
  23. this.availableGrants = [1, 2, /*3, */4, 5];
  24. this.availableGrantLabels = {
  25. 1: 'Public',
  26. 2: 'Anyone with the link',
  27. // 3:'Specified users only',
  28. 4: 'Just me',
  29. 5: 'Only inside the group',
  30. };
  31. this.onChangeGrant = this.onChangeGrant.bind(this);
  32. }
  33. // Init component when the component did mount.
  34. componentDidMount() {
  35. this.init();
  36. }
  37. // Initialize the component.
  38. init() {
  39. this.grantSelectorInputEl.value = this.state.pageGrant.grant;
  40. }
  41. /**
  42. * On change event handler for pagegrant.
  43. * @param {any} grant page grant
  44. * @memberof GrantSelector
  45. */
  46. onChangeGrant(grant) {
  47. const newValue = this.grantSelectorInputEl.value;
  48. const newGrant = Object.assign(this.state.pageGrant, {grant: newValue});
  49. this.setState({ pageGrant: newGrant });
  50. // dispatch event
  51. this.dispatchOnChange();
  52. }
  53. // (TBD)
  54. // /**
  55. // * On click event handler for grant usergroup.
  56. // *
  57. // * @memberof GrantSelector
  58. // */
  59. // onClickGrantGroup() {
  60. // const newValue = this.groupSelectorInputEl.value;
  61. // const newGrant = Object.assign(this.state.pageGrant, { grantGroup: newValue });
  62. // this.setState({ pageGrant: newGrant });
  63. // // dispatch event
  64. // this.dispatchOnChange();
  65. // // close group select modal
  66. // if (this.state.isModalShown) {
  67. // this.setState({ isGroupModalShown: false });
  68. // }
  69. // }
  70. /**
  71. * dispatch onChange event
  72. * @memberof GrantSelector
  73. */
  74. dispatchOnChange() {
  75. if (this.props.onChange != null) {
  76. this.props.onChange(this.state.pageGrant);
  77. }
  78. }
  79. /**
  80. * Render grant selector DOM.
  81. * @returns
  82. * @memberof GrantSelector
  83. */
  84. renderGrantSelector() {
  85. const { t } = this.props;
  86. const grantElems = this.availableGrants.map((grant) => {
  87. return <option key={grant} value={grant}>{t(this.availableGrantLabels[grant])}</option>;
  88. });
  89. const bsClassName = 'form-control-dummy'; // set form-control* to shrink width
  90. return (
  91. <FormGroup controlId="formControlsSelect" className="m-b-0">
  92. <FormControl componentClass="select" placeholder="select" defaultValue={this.state.pageGrant.grant} bsClass={bsClassName} className="btn-group-sm selectpicker"
  93. onChange={this.onChangeGrant}
  94. inputRef={ el => this.grantSelectorInputEl=el }>
  95. {grantElems}
  96. </FormControl>
  97. </FormGroup>
  98. );
  99. }
  100. // (TBD)
  101. // /**
  102. // * Render select grantgroup modal.
  103. // *
  104. // * @returns
  105. // * @memberof GrantSelector
  106. // */
  107. // renderSelectGroupModal() {
  108. // // const userRelatedGroups = this.props.userRelatedGroups;
  109. // const groupList = this.userRelatedGroups.map((group) => {
  110. // return <li>
  111. // <Button onClick={this.onClickGrantGroup(group)} bsClass="btn btn-sm btn-primary">{group.name}</Button>
  112. // </li>;
  113. // });
  114. // return (
  115. // <Modal show={this.props.isGroupModalShown} className="select-grant-group">
  116. // <Modal.Header closeButton>
  117. // <Modal.Title>
  118. // Select a Group
  119. // </Modal.Title>
  120. // </Modal.Header>
  121. // <Modal.Body>
  122. // <ul className="list-inline">
  123. // {groupList}
  124. // </ul>
  125. // </Modal.Body>
  126. // </Modal>
  127. // );
  128. // }
  129. render() {
  130. return <div className="m-r-5">{this.renderGrantSelector()}</div>;
  131. }
  132. }
  133. export class PageGrant {
  134. constructor(props) {
  135. this.grant = '';
  136. this.grantGroup = null;
  137. Object.assign(this, props);
  138. }
  139. }
  140. export class UserGroup {
  141. constructor(props) {
  142. this.userGroupId = '';
  143. this.userGroup;
  144. Object.assign(this, props);
  145. }
  146. }
  147. GrantSelector.propTypes = {
  148. t: PropTypes.func.isRequired, // i18next
  149. crowi: PropTypes.object.isRequired,
  150. isGroupModalShown: PropTypes.bool,
  151. userRelatedGroups: PropTypes.object,
  152. pageGrant: PropTypes.instanceOf(PageGrant),
  153. onChange: PropTypes.func,
  154. };
  155. export default translate()(GrantSelector);