PutbackPageModal.jsx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import React, { useState } from 'react';
  2. import {
  3. Modal, ModalHeader, ModalBody, ModalFooter,
  4. } from 'reactstrap';
  5. import { useTranslation } from 'react-i18next';
  6. import { usePutBackPageModal } from '~/stores/modal';
  7. import { apiPost } from '~/client/util/apiv1-client';
  8. import ApiErrorMessageList from './PageManagement/ApiErrorMessageList';
  9. const PutBackPageModal = () => {
  10. const { t } = useTranslation();
  11. const { data: pageDataToRevert, close: closePutBackPageModal } = usePutBackPageModal();
  12. const { isOpened, page } = pageDataToRevert;
  13. const { pageId, path } = page;
  14. const onPutBacked = pageDataToRevert.opts?.onPutBacked;
  15. const [errs, setErrs] = useState(null);
  16. const [isPutbackRecursively, setIsPutbackRecursively] = useState(true);
  17. function changeIsPutbackRecursivelyHandler() {
  18. setIsPutbackRecursively(!isPutbackRecursively);
  19. }
  20. async function putbackPageButtonHandler() {
  21. setErrs(null);
  22. try {
  23. // control flag
  24. // If is it not true, Request value must be `null`.
  25. const recursively = isPutbackRecursively ? true : null;
  26. const response = await apiPost('/pages.revertRemove', {
  27. page_id: pageId,
  28. recursively,
  29. });
  30. const putbackPagePath = encodeURI(response.page.path);
  31. if (onPutBacked != null) {
  32. onPutBacked(putbackPagePath);
  33. }
  34. closePutBackPageModal();
  35. }
  36. catch (err) {
  37. setErrs(err);
  38. }
  39. }
  40. return (
  41. <Modal isOpen={isOpened} toggle={closePutBackPageModal} className="grw-create-page">
  42. <ModalHeader tag="h4" toggle={closePutBackPageModal} className="bg-info text-light">
  43. <i className="icon-action-undo mr-2" aria-hidden="true"></i> { t('modal_putback.label.Put Back Page') }
  44. </ModalHeader>
  45. <ModalBody>
  46. <div className="form-group">
  47. <label>{t('modal_putback.label.Put Back Page')}:</label><br />
  48. <code>{path}</code>
  49. </div>
  50. <div className="custom-control custom-checkbox custom-checkbox-warning">
  51. <input
  52. className="custom-control-input"
  53. id="cbPutBackRecursively"
  54. type="checkbox"
  55. checked={isPutbackRecursively}
  56. onChange={changeIsPutbackRecursivelyHandler}
  57. />
  58. <label htmlFor="cbPutBackRecursively" className="custom-control-label">
  59. { t('modal_putback.label.recursively') }
  60. </label>
  61. <p className="form-text text-muted mt-0">
  62. <code>{ path }</code>{ t('modal_putback.help.recursively') }
  63. </p>
  64. </div>
  65. </ModalBody>
  66. <ModalFooter>
  67. <ApiErrorMessageList errs={errs} />
  68. <button type="button" className="btn btn-info" onClick={putbackPageButtonHandler}>
  69. <i className="icon-action-undo mr-2" aria-hidden="true"></i> { t('Put Back') }
  70. </button>
  71. </ModalFooter>
  72. </Modal>
  73. );
  74. };
  75. export default PutBackPageModal;