PutbackPageModal.jsx 2.9 KB

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