PutbackPageModal.jsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import React, { useState } 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 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. if (onPutBacked != null) {
  33. onPutBacked(response.page.path);
  34. }
  35. closePutBackPageModal();
  36. }
  37. catch (err) {
  38. setTargetPath(err.data);
  39. setErrs([err]);
  40. }
  41. }
  42. const HeaderContent = () => {
  43. if (!isOpened) {
  44. return <></>;
  45. }
  46. return (
  47. <>
  48. <i className="icon-action-undo mr-2" aria-hidden="true"></i> { t('modal_putback.label.Put Back Page') }
  49. </>
  50. );
  51. };
  52. const BodyContent = () => {
  53. if (!isOpened) {
  54. return <></>;
  55. }
  56. return (
  57. <>
  58. <div className="form-group">
  59. <label>{t('modal_putback.label.Put Back Page')}:</label><br />
  60. <code>{path}</code>
  61. </div>
  62. <div className="custom-control custom-checkbox custom-checkbox-warning">
  63. <input
  64. className="custom-control-input"
  65. id="cbPutBackRecursively"
  66. type="checkbox"
  67. checked={isPutbackRecursively}
  68. onChange={changeIsPutbackRecursivelyHandler}
  69. />
  70. <label htmlFor="cbPutBackRecursively" className="custom-control-label">
  71. { t('modal_putback.label.recursively') }
  72. </label>
  73. <p className="form-text text-muted mt-0">
  74. <code>{ path }</code>{ t('modal_putback.help.recursively') }
  75. </p>
  76. </div>
  77. </>
  78. );
  79. };
  80. const FooterContent = () => {
  81. if (!isOpened) {
  82. return <></>;
  83. }
  84. return (
  85. <>
  86. <ApiErrorMessageList errs={errs} targetPath={targetPath} />
  87. <button type="button" className="btn btn-info" onClick={putbackPageButtonHandler}>
  88. <i className="icon-action-undo mr-2" aria-hidden="true"></i> { t('Put Back') }
  89. </button>
  90. </>
  91. );
  92. };
  93. return (
  94. <Modal isOpen={isOpened} toggle={closePutBackPageModal} className="grw-create-page">
  95. <ModalHeader tag="h4" toggle={closePutBackPageModal} className="bg-info text-light">
  96. <HeaderContent/>
  97. </ModalHeader>
  98. <ModalBody>
  99. <BodyContent/>
  100. </ModalBody>
  101. <ModalFooter>
  102. <FooterContent/>
  103. </ModalFooter>
  104. </Modal>
  105. );
  106. };
  107. export default PutBackPageModal;