PageDuplicateModal.jsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import React, { useState, useEffect, useCallback } 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 { withUnstatedContainers } from './UnstatedUtils';
  8. import AppContainer from '../services/AppContainer';
  9. import PageContainer from '../services/PageContainer';
  10. import PagePathAutoComplete from './PagePathAutoComplete';
  11. import ApiErrorMessageList from './PageManagement/ApiErrorMessageList';
  12. const PageDuplicateModal = (props) => {
  13. const { t, appContainer, pageContainer } = props;
  14. const config = appContainer.getConfig();
  15. const isReachable = config.isSearchServiceReachable;
  16. const { pageId, path } = pageContainer.state;
  17. const { crowi } = appContainer.config;
  18. const [pageNameInput, setPageNameInput] = useState(path);
  19. const [errs, setErrs] = useState(null);
  20. const [getSubordinatedError, setGetSuborinatedError] = useState(null);
  21. const [isDuplicateRecursively, setIsDuplicateRecursively] = useState(true);
  22. const [isDuplicateRecursivelyWithoutExistPath, setIsDuplicateRecursivelyWithoutExistPath] = useState(true);
  23. const [isDuplicateExist, setIsDuplicateExist] = useState([]);
  24. /**
  25. * change pageNameInput for PagePathAutoComplete
  26. * @param {string} value
  27. */
  28. function ppacInputChangeHandler(value) {
  29. createSubordinatedList(value);
  30. setPageNameInput(value);
  31. }
  32. /**
  33. * change pageNameInput
  34. * @param {string} value
  35. */
  36. function inputChangeHandler(value) {
  37. setPageNameInput(value);
  38. }
  39. function changeIsDuplicateRecursivelyHandler() {
  40. setIsDuplicateRecursively(!isDuplicateRecursively);
  41. }
  42. function changeIsDuplicateRecursivelyWithoutExistPathHandler() {
  43. setIsDuplicateRecursivelyWithoutExistPath(!isDuplicateRecursivelyWithoutExistPath);
  44. }
  45. // function changeIsExistHandler() {
  46. // setIsExist(true);
  47. // }
  48. function createSubordinatedList(value) {
  49. // ToDo: get the duplicated list from sever
  50. // below is psuedo code
  51. // let duplicatedList = get.apiv3......
  52. // duplicatedList = duplicatedList.map((value) =>
  53. // <li className="duplicate-exist" key={value}> {value}: { t('modal_duplicate.label.Same page already exists') } </li>; )
  54. // setIsDuplicateExist(duplicatedList);
  55. // for now we use dummy path
  56. setIsDuplicateExist(['/test146/test147', value]);
  57. }
  58. async function duplicate() {
  59. setErrs(null);
  60. try {
  61. const res = await appContainer.apiv3Post('/pages/duplicate', { pageId, pageNameInput });
  62. const { page } = res.data;
  63. window.location.href = encodeURI(`${page.path}?duplicated=${path}`);
  64. }
  65. catch (err) {
  66. setErrs(err);
  67. }
  68. }
  69. function ppacSubmitHandler() {
  70. duplicate();
  71. }
  72. return (
  73. <Modal size="lg" isOpen={props.isOpen} toggle={props.onClose} className="grw-duplicate-page">
  74. <ModalHeader tag="h4" toggle={props.onClose} className="bg-primary text-light">
  75. { t('modal_duplicate.label.Duplicate page') }
  76. </ModalHeader>
  77. <ModalBody>
  78. <div className="form-group"><label>{t('modal_duplicate.label.Current page name')}</label><br />
  79. <code>{path}</code>
  80. </div>
  81. <div className="form-group">
  82. <label htmlFor="duplicatePageName">{ t('modal_duplicate.label.New page name') }</label><br />
  83. <div className="input-group">
  84. <div className="input-group-prepend">
  85. <span className="input-group-text">{crowi.url}</span>
  86. </div>
  87. <div className="flex-fill">
  88. {isReachable
  89. ? (
  90. <PagePathAutoComplete
  91. initializedPath={path}
  92. onSubmit={ppacSubmitHandler}
  93. onInputChange={ppacInputChangeHandler}
  94. />
  95. )
  96. : (
  97. <input
  98. type="text"
  99. value={pageNameInput}
  100. className="form-control"
  101. onChange={e => inputChangeHandler(e.target.value)}
  102. required
  103. />
  104. )}
  105. </div>
  106. </div>
  107. </div>
  108. <div className="custom-control custom-checkbox custom-checkbox-warning">
  109. <input
  110. className="custom-control-input"
  111. name="recursively"
  112. id="cbDuplicateRecursively"
  113. type="checkbox"
  114. checked={isDuplicateRecursively}
  115. onChange={changeIsDuplicateRecursivelyHandler}
  116. />
  117. <label className="custom-control-label" htmlFor="cbDuplicateRecursively">
  118. { t('modal_duplicate.label.Duplicate with child') }
  119. </label>
  120. </div>
  121. <div
  122. className="custom-control custom-checkbox custom-checkbox-warning"
  123. style={{ display: (isDuplicateExist.length !== 0) && isDuplicateRecursively ? '' : 'none' }}
  124. >
  125. <input
  126. className="custom-control-input"
  127. name="withoutExistRecursively"
  128. id="cbDuplicatewithoutExistRecursively"
  129. type="checkbox"
  130. checked={isDuplicateRecursivelyWithoutExistPath}
  131. onChange={changeIsDuplicateRecursivelyWithoutExistPathHandler}
  132. />
  133. <label className="custom-control-label" htmlFor="cbDuplicatewithoutExistRecursively">
  134. { t('modal_duplicate.label.Duplicate without exist path') }
  135. </label>
  136. </div>
  137. <div>
  138. <ul className="duplicate-name">
  139. {isDuplicateRecursively && isDuplicateExist.length !== 0 && isDuplicateExist}
  140. </ul>
  141. </div>
  142. <div> {getSubordinatedError} </div>
  143. </ModalBody>
  144. <ModalFooter>
  145. <ApiErrorMessageList errs={errs} targetPath={pageNameInput} />
  146. <button type="button" className="btn btn-primary" onClick={duplicate}>Duplicate page</button>
  147. </ModalFooter>
  148. </Modal>
  149. );
  150. };
  151. /**
  152. * Wrapper component for using unstated
  153. */
  154. const PageDuplicateModallWrapper = withUnstatedContainers(PageDuplicateModal, [AppContainer, PageContainer]);
  155. PageDuplicateModal.propTypes = {
  156. t: PropTypes.func.isRequired, // i18next
  157. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  158. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  159. isOpen: PropTypes.bool.isRequired,
  160. onClose: PropTypes.func.isRequired,
  161. };
  162. export default withTranslation()(PageDuplicateModallWrapper);