SavePageControls.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import React from 'react';
  2. import EventEmitter from 'events';
  3. import { pagePathUtils } from '@growi/core';
  4. import { NullableBoolean } from 'aws-sdk/clients/synthetics';
  5. import { useTranslation } from 'next-i18next';
  6. import {
  7. UncontrolledButtonDropdown, Button,
  8. DropdownToggle, DropdownMenu, DropdownItem,
  9. } from 'reactstrap';
  10. // import PageContainer from '~/client/services/PageContainer';
  11. import { getOptionsToSave } from '~/client/util/editor';
  12. import { CustomWindow } from '~/interfaces/global';
  13. import {
  14. useCurrentPagePath, useIsEditable, useCurrentPageId, useIsAclEnabled,
  15. } from '~/stores/context';
  16. import { usePageTagsForEditors, useIsEnabledUnsavedWarning } from '~/stores/editor';
  17. import { useSWRxCurrentPage } from '~/stores/page';
  18. import { useSelectedGrant } from '~/stores/ui';
  19. import loggerFactory from '~/utils/logger';
  20. import GrantSelector from './SavePageControls/GrantSelector';
  21. // import { withUnstatedContainers } from './UnstatedUtils';
  22. const logger = loggerFactory('growi:SavePageControls');
  23. type Props = {
  24. // pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  25. // TODO: remove this when omitting unstated is completed
  26. // editorMode?: string,
  27. isSlackEnabled: boolean,
  28. slackChannels: string,
  29. // mutateGrant?: () => void,
  30. // mutateGrantGroupId?: () => void,
  31. // mutateGrantGroupName:() => void,
  32. // mutateIsEnabledUnsavedWarning?: () => void,
  33. }
  34. const { isTopPage } = pagePathUtils;
  35. export const SavePageControls = (props: Props): JSX.Element | null => {
  36. const { t } = useTranslation();
  37. const { data: currentPagePath } = useCurrentPagePath();
  38. const { data: isEditable } = useIsEditable();
  39. const { data: isAclEnabled } = useIsAclEnabled();
  40. const { data: grantData, mutate: mutateGrant } = useSelectedGrant();
  41. const { data: pageId } = useCurrentPageId();
  42. const { data: pageTags } = usePageTagsForEditors(pageId);
  43. const { mutate: mutateIsEnabledUnsavedWarning } = useIsEnabledUnsavedWarning();
  44. if (isEditable == null || isAclEnabled == null) {
  45. return null;
  46. }
  47. if (!isEditable) {
  48. return null;
  49. }
  50. const grant = grantData?.grant || 1;
  51. const grantedGroup = grantData?.grantedGroup;
  52. const {
  53. isSlackEnabled, slackChannels,
  54. // pageContainer, mutateGrantGroupId, mutateGrantGroupName,
  55. } = props;
  56. const updateGrantHandler = (data): void => {
  57. console.log({ data });
  58. mutateGrant(data.grant);
  59. // mutateGrantGroupId(data.grantGroupId);
  60. // mutateGrantGroupName(data.grantGroupName);
  61. };
  62. const save = async(): Promise<void> => {
  63. // disable unsaved warning
  64. mutateIsEnabledUnsavedWarning(false);
  65. try {
  66. // save
  67. (window as CustomWindow).globalEmitter.emit('saveAndReload');
  68. }
  69. catch (error) {
  70. logger.error('failed to save', error);
  71. // pageContainer.showErrorToastr(error);
  72. if (error.code === 'conflict') {
  73. // pageContainer.setState({
  74. // remoteRevisionId: error.data.revisionId,
  75. // remoteRevisionBody: error.data.revisionBody,
  76. // remoteRevisionUpdateAt: error.data.createdAt,
  77. // lastUpdateUser: error.data.user,
  78. // });
  79. }
  80. }
  81. };
  82. const saveAndOverwriteScopesOfDescendants = () => {
  83. // disable unsaved warning
  84. mutateIsEnabledUnsavedWarning(false);
  85. // save
  86. // const currentOptionsToSave = getOptionsToSave(isSlackEnabled, slackChannels, grant, grantedGroup?.id, grantedGroup?.name, pageTags || []);
  87. // const optionsToSave = Object.assign(currentOptionsToSave, {
  88. // overwriteScopesOfDescendants: true,
  89. // });
  90. (window as CustomWindow).globalEmitter.emit('saveAndReload', { overwriteScopesOfDescendants: true });
  91. // pageContainer.saveAndReload(optionsToSave, this.props.editorMode);
  92. };
  93. const isRootPage = isTopPage(currentPagePath ?? '');
  94. const labelSubmitButton = pageId == null ? t('Create') : t('Update');
  95. const labelOverwriteScopes = t('page_edit.overwrite_scopes', { operation: labelSubmitButton });
  96. return (
  97. <div className="d-flex align-items-center form-inline flex-nowrap">
  98. {isAclEnabled
  99. && (
  100. <div className="mr-2">
  101. <GrantSelector
  102. grant={grant}
  103. disabled={isRootPage}
  104. grantGroupId={grantedGroup?.id}
  105. grantGroupName={grantedGroup?.name}
  106. onUpdateGrant={updateGrantHandler}
  107. />
  108. </div>
  109. )
  110. }
  111. <UncontrolledButtonDropdown direction="up">
  112. <Button id="caret" color="primary" className="btn-submit" onClick={save}>
  113. {labelSubmitButton}
  114. </Button>
  115. <DropdownToggle caret color="primary" />
  116. <DropdownMenu right>
  117. <DropdownItem onClick={saveAndOverwriteScopesOfDescendants}>
  118. {labelOverwriteScopes}
  119. </DropdownItem>
  120. </DropdownMenu>
  121. </UncontrolledButtonDropdown>
  122. </div>
  123. );
  124. };
  125. export default SavePageControls;