PutbackPageModal.jsx 3.6 KB

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