PutbackPageModal.jsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import React, { useState, useCallback } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import {
  4. Modal, ModalHeader, ModalBody, ModalFooter,
  5. } from 'reactstrap';
  6. import { apiPost } from '~/client/util/apiv1-client';
  7. import { PathAlreadyExistsError } from '~/server/models/errors';
  8. import { usePutBackPageModal } from '~/stores/modal';
  9. import { usePageInfoTermManager } from '~/stores/page';
  10. import ApiErrorMessageList from './PageManagement/ApiErrorMessageList';
  11. const PutBackPageModal = () => {
  12. const { t } = useTranslation();
  13. const { data: pageDataToRevert, close: closePutBackPageModal } = usePutBackPageModal();
  14. const { advance: advancePi } = usePageInfoTermManager();
  15. const { isOpened, page } = pageDataToRevert;
  16. const { pageId, path } = page;
  17. const onPutBacked = pageDataToRevert.opts?.onPutBacked;
  18. const [errs, setErrs] = useState(null);
  19. const [targetPath, setTargetPath] = useState(null);
  20. const [isPutbackRecursively, setIsPutbackRecursively] = useState(true);
  21. function changeIsPutbackRecursivelyHandler() {
  22. setIsPutbackRecursively(!isPutbackRecursively);
  23. }
  24. async function putbackPageButtonHandler() {
  25. setErrs(null);
  26. try {
  27. // control flag
  28. // If is it not true, Request value must be `null`.
  29. const recursively = isPutbackRecursively ? true : null;
  30. const response = await apiPost('/pages.revertRemove', {
  31. page_id: pageId,
  32. recursively,
  33. });
  34. advancePi();
  35. if (onPutBacked != null) {
  36. onPutBacked(response.page.path);
  37. }
  38. closePutBackPageModal();
  39. }
  40. catch (err) {
  41. setTargetPath(err.data);
  42. setErrs([err]);
  43. }
  44. }
  45. const HeaderContent = () => {
  46. if (!isOpened) {
  47. return <></>;
  48. }
  49. return (
  50. <>
  51. <i className="icon-action-undo mr-2" aria-hidden="true"></i> { t('modal_putback.label.Put Back Page') }
  52. </>
  53. );
  54. };
  55. const BodyContent = () => {
  56. if (!isOpened) {
  57. return <></>;
  58. }
  59. return (
  60. <>
  61. <div className="form-group">
  62. <label>{t('modal_putback.label.Put Back Page')}:</label><br />
  63. <code>{path}</code>
  64. </div>
  65. <div className="custom-control custom-checkbox custom-checkbox-warning">
  66. <input
  67. className="custom-control-input"
  68. id="cbPutBackRecursively"
  69. type="checkbox"
  70. checked={isPutbackRecursively}
  71. onChange={changeIsPutbackRecursivelyHandler}
  72. />
  73. <label htmlFor="cbPutBackRecursively" className="custom-control-label">
  74. { t('modal_putback.label.recursively') }
  75. </label>
  76. <p className="form-text text-muted mt-0">
  77. <code>{ path }</code>{ t('modal_putback.help.recursively') }
  78. </p>
  79. </div>
  80. </>
  81. );
  82. };
  83. const FooterContent = () => {
  84. if (!isOpened) {
  85. return <></>;
  86. }
  87. return (
  88. <>
  89. <ApiErrorMessageList errs={errs} targetPath={targetPath} />
  90. <button type="button" className="btn btn-info" onClick={putbackPageButtonHandler} data-testid="put-back-execution-button">
  91. <i className="icon-action-undo mr-2" aria-hidden="true"></i> { t('Put Back') }
  92. </button>
  93. </>
  94. );
  95. };
  96. const closeModalHandler = useCallback(() => {
  97. closePutBackPageModal();
  98. setErrs(null);
  99. }, [closePutBackPageModal]);
  100. return (
  101. <Modal isOpen={isOpened} toggle={closeModalHandler} data-testid="put-back-page-modal">
  102. <ModalHeader tag="h4" toggle={closeModalHandler} className="bg-info text-light">
  103. <HeaderContent/>
  104. </ModalHeader>
  105. <ModalBody>
  106. <BodyContent/>
  107. </ModalBody>
  108. <ModalFooter>
  109. <FooterContent/>
  110. </ModalFooter>
  111. </Modal>
  112. );
  113. };
  114. export default PutBackPageModal;