SavePageControls.jsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import {
  5. UncontrolledButtonDropdown, Button,
  6. DropdownToggle, DropdownMenu, DropdownItem,
  7. } from 'reactstrap';
  8. import loggerFactory from '~/utils/logger';
  9. import PageContainer from '~/client/services/PageContainer';
  10. import AppContainer from '~/client/services/AppContainer';
  11. import EditorContainer from '~/client/services/EditorContainer';
  12. import { withUnstatedContainers } from './UnstatedUtils';
  13. import GrantSelector from './SavePageControls/GrantSelector';
  14. import { getOptionsToSave } from '~/client/util/editor';
  15. // TODO: remove this when omitting unstated is completed
  16. import { useEditorMode } from '~/stores/ui';
  17. import { useIsEditable, useSlackChannels } from '~/stores/context';
  18. import { useIsSlackEnabled } from '~/stores/editor';
  19. const logger = loggerFactory('growi:SavePageControls');
  20. class SavePageControls extends React.Component {
  21. constructor(props) {
  22. super(props);
  23. const config = this.props.appContainer.getConfig();
  24. this.isAclEnabled = config.isAclEnabled;
  25. this.updateGrantHandler = this.updateGrantHandler.bind(this);
  26. this.save = this.save.bind(this);
  27. this.saveAndOverwriteScopesOfDescendants = this.saveAndOverwriteScopesOfDescendants.bind(this);
  28. }
  29. updateGrantHandler(data) {
  30. this.props.editorContainer.setState(data);
  31. }
  32. async save() {
  33. const {
  34. isSlackEnabled, slackChannels, pageContainer, editorContainer,
  35. } = this.props;
  36. // disable unsaved warning
  37. editorContainer.disableUnsavedWarning();
  38. try {
  39. // save
  40. const optionsToSave = getOptionsToSave(isSlackEnabled, slackChannels, editorContainer);
  41. await pageContainer.saveAndReload(optionsToSave, this.props.editorMode);
  42. }
  43. catch (error) {
  44. logger.error('failed to save', error);
  45. pageContainer.showErrorToastr(error);
  46. }
  47. }
  48. saveAndOverwriteScopesOfDescendants() {
  49. const {
  50. isSlackEnabled, slackChannels, pageContainer, editorContainer,
  51. } = this.props;
  52. // disable unsaved warning
  53. editorContainer.disableUnsavedWarning();
  54. // save
  55. const currentOptionsToSave = getOptionsToSave(isSlackEnabled, slackChannels, editorContainer);
  56. const optionsToSave = Object.assign(currentOptionsToSave, {
  57. overwriteScopesOfDescendants: true,
  58. });
  59. pageContainer.saveAndReload(optionsToSave, this.props.editorMode);
  60. }
  61. render() {
  62. const { t, pageContainer, editorContainer } = this.props;
  63. const isRootPage = pageContainer.state.path === '/';
  64. const labelSubmitButton = pageContainer.state.pageId == null ? t('Create') : t('Update');
  65. const labelOverwriteScopes = t('page_edit.overwrite_scopes', { operation: labelSubmitButton });
  66. return (
  67. <div className="d-flex align-items-center form-inline flex-nowrap">
  68. {this.isAclEnabled
  69. && (
  70. <div className="mr-2">
  71. <GrantSelector
  72. disabled={isRootPage}
  73. grant={editorContainer.state.grant}
  74. grantGroupId={editorContainer.state.grantGroupId}
  75. grantGroupName={editorContainer.state.grantGroupName}
  76. onUpdateGrant={this.updateGrantHandler}
  77. />
  78. </div>
  79. )
  80. }
  81. <UncontrolledButtonDropdown direction="up">
  82. <Button id="caret" color="primary" className="btn-submit" onClick={this.save}>{labelSubmitButton}</Button>
  83. <DropdownToggle caret color="primary" />
  84. <DropdownMenu right>
  85. <DropdownItem onClick={this.saveAndOverwriteScopesOfDescendants}>
  86. {labelOverwriteScopes}
  87. </DropdownItem>
  88. </DropdownMenu>
  89. </UncontrolledButtonDropdown>
  90. </div>
  91. );
  92. }
  93. }
  94. /**
  95. * Wrapper component for using unstated
  96. */
  97. const SavePageControlsHOCWrapper = withUnstatedContainers(SavePageControls, [AppContainer, PageContainer, EditorContainer]);
  98. const SavePageControlsWrapper = (props) => {
  99. const { data: isEditable } = useIsEditable();
  100. const { data: editorMode } = useEditorMode();
  101. const { data: isSlackEnabled } = useIsSlackEnabled();
  102. const { data: slackChannels } = useSlackChannels();
  103. if (isEditable == null || editorMode == null) {
  104. return null;
  105. }
  106. if (!isEditable) {
  107. return null;
  108. }
  109. return (
  110. <SavePageControlsHOCWrapper
  111. {...props}
  112. editorMode={editorMode}
  113. isSlackEnabled={isSlackEnabled}
  114. slackChannels={slackChannels}
  115. />
  116. );
  117. };
  118. SavePageControls.propTypes = {
  119. t: PropTypes.func.isRequired, // i18next
  120. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  121. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  122. editorContainer: PropTypes.instanceOf(EditorContainer).isRequired,
  123. // TODO: remove this when omitting unstated is completed
  124. editorMode: PropTypes.string.isRequired,
  125. isSlackEnabled: PropTypes.bool.isRequired,
  126. slackChannels: PropTypes.string.isRequired,
  127. };
  128. export default withTranslation()(SavePageControlsWrapper);