PageCreateModal.jsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import React, {
  2. useEffect, useState, useMemo, useCallback,
  3. } from 'react';
  4. import { pagePathUtils, pathUtils } from '@growi/core';
  5. import { format } from 'date-fns';
  6. import PropTypes from 'prop-types';
  7. import { withTranslation } from 'react-i18next';
  8. import { Modal, ModalHeader, ModalBody } from 'reactstrap';
  9. import { debounce } from 'throttle-debounce';
  10. import AppContainer from '~/client/services/AppContainer';
  11. import { toastError } from '~/client/util/apiNotification';
  12. import { usePageCreateModal } from '~/stores/modal';
  13. import PagePathAutoComplete from './PagePathAutoComplete';
  14. import { withUnstatedContainers } from './UnstatedUtils';
  15. const {
  16. userPageRoot, isCreatablePage, generateEditorPath, isUsersHomePage,
  17. } = pagePathUtils;
  18. const PageCreateModal = (props) => {
  19. const { t, appContainer } = props;
  20. const { data: pageCreateModalData, close: closeCreateModal } = usePageCreateModal();
  21. const { isOpened, path } = pageCreateModalData;
  22. const config = appContainer.getConfig();
  23. const isReachable = config.isSearchServiceReachable;
  24. const pathname = path || '';
  25. const userPageRootPath = userPageRoot(appContainer.currentUser);
  26. const isCreatable = isCreatablePage(pathname) || isUsersHomePage(pathname);
  27. const pageNameInputInitialValue = isCreatable ? pathUtils.addTrailingSlash(pathname) : '/';
  28. const now = format(new Date(), 'yyyy/MM/dd');
  29. const [todayInput1, setTodayInput1] = useState(t('Memo'));
  30. const [todayInput2, setTodayInput2] = useState('');
  31. const [pageNameInput, setPageNameInput] = useState(pageNameInputInitialValue);
  32. const [template, setTemplate] = useState(null);
  33. const [isMatchedWithUserHomePagePath, setIsMatchedWithUserHomePagePath] = useState(false);
  34. // ensure pageNameInput is synced with selectedPagePath || currentPagePath
  35. useEffect(() => {
  36. setPageNameInput(isCreatable ? pathUtils.addTrailingSlash(pathname) : '/');
  37. }, [pathname, isCreatable]);
  38. const checkIsUsersHomePageDebounce = useMemo(() => {
  39. const checkIsUsersHomePage = () => {
  40. setIsMatchedWithUserHomePagePath(isUsersHomePage(pageNameInput));
  41. };
  42. return debounce(1000, checkIsUsersHomePage);
  43. }, [pageNameInput]);
  44. useEffect(() => {
  45. checkIsUsersHomePageDebounce(pageNameInput);
  46. }, [checkIsUsersHomePageDebounce, pageNameInput]);
  47. function transitBySubmitEvent(e, transitHandler) {
  48. // prevent page transition by submit
  49. e.preventDefault();
  50. transitHandler();
  51. }
  52. /**
  53. * change todayInput1
  54. * @param {string} value
  55. */
  56. function onChangeTodayInput1Handler(value) {
  57. setTodayInput1(value);
  58. }
  59. /**
  60. * change todayInput2
  61. * @param {string} value
  62. */
  63. function onChangeTodayInput2Handler(value) {
  64. setTodayInput2(value);
  65. }
  66. /**
  67. * change template
  68. * @param {string} value
  69. */
  70. function onChangeTemplateHandler(value) {
  71. setTemplate(value);
  72. }
  73. /**
  74. * join path, check if creatable, then redirect
  75. * @param {string} paths
  76. */
  77. async function redirectToEditor(...paths) {
  78. try {
  79. const editorPath = await generateEditorPath(...paths);
  80. window.location.href = editorPath;
  81. }
  82. catch (err) {
  83. toastError(err);
  84. }
  85. }
  86. /**
  87. * access today page
  88. */
  89. function createTodayPage() {
  90. let tmpTodayInput1 = todayInput1;
  91. if (tmpTodayInput1 === '') {
  92. tmpTodayInput1 = t('Memo');
  93. }
  94. redirectToEditor(userPageRootPath, tmpTodayInput1, now, todayInput2);
  95. }
  96. /**
  97. * access input page
  98. */
  99. function createInputPage() {
  100. redirectToEditor(pageNameInput);
  101. }
  102. function ppacSubmitHandler(input) {
  103. redirectToEditor(input);
  104. }
  105. /**
  106. * access template page
  107. */
  108. function createTemplatePage(e) {
  109. const pageName = (template === 'children') ? '_template' : '__template';
  110. redirectToEditor(pathname, pageName);
  111. }
  112. function renderCreateTodayForm() {
  113. return (
  114. <div className="row">
  115. <fieldset className="col-12 mb-4">
  116. <h3 className="grw-modal-head pb-2">{t("Create today's")}</h3>
  117. <div className="d-sm-flex align-items-center justify-items-between">
  118. <div className="d-flex align-items-center flex-fill flex-wrap flex-lg-nowrap">
  119. <div className="d-flex align-items-center">
  120. <span>{userPageRootPath}/</span>
  121. <form onSubmit={e => transitBySubmitEvent(e, createTodayPage)}>
  122. <input
  123. type="text"
  124. className="page-today-input1 form-control text-center mx-2"
  125. value={todayInput1}
  126. onChange={e => onChangeTodayInput1Handler(e.target.value)}
  127. />
  128. </form>
  129. <span className="page-today-suffix">/{now}/</span>
  130. </div>
  131. <form className="mt-1 mt-lg-0 ml-lg-2 w-100" onSubmit={e => transitBySubmitEvent(e, createTodayPage)}>
  132. <input
  133. type="text"
  134. className="page-today-input2 form-control w-100"
  135. id="page-today-input2"
  136. placeholder={t('Input page name (optional)')}
  137. value={todayInput2}
  138. onChange={e => onChangeTodayInput2Handler(e.target.value)}
  139. />
  140. </form>
  141. </div>
  142. <div className="d-flex justify-content-end mt-1 mt-sm-0">
  143. <button
  144. type="button"
  145. data-testid="btn-create-memo"
  146. className="grw-btn-create-page btn btn-outline-primary rounded-pill text-nowrap ml-3"
  147. onClick={createTodayPage}
  148. >
  149. <i className="icon-fw icon-doc"></i>{t('Create')}
  150. </button>
  151. </div>
  152. </div>
  153. </fieldset>
  154. </div>
  155. );
  156. }
  157. function renderInputPageForm() {
  158. return (
  159. <div className="row" data-testid="row-create-page-under-below">
  160. <fieldset className="col-12 mb-4">
  161. <h3 className="grw-modal-head pb-2">{t('Create under')}</h3>
  162. <div className="d-sm-flex align-items-center justify-items-between">
  163. <div className="flex-fill">
  164. {isReachable
  165. ? (
  166. <PagePathAutoComplete
  167. initializedPath={pageNameInput}
  168. addTrailingSlash
  169. onSubmit={ppacSubmitHandler}
  170. onInputChange={value => setPageNameInput(value)}
  171. autoFocus
  172. />
  173. )
  174. : (
  175. <form onSubmit={e => transitBySubmitEvent(e, createInputPage)}>
  176. <input
  177. type="text"
  178. value={pageNameInput}
  179. className="form-control flex-fill"
  180. placeholder={t('Input page name')}
  181. onChange={e => setPageNameInput(e.target.value)}
  182. required
  183. />
  184. </form>
  185. )}
  186. </div>
  187. <div className="d-flex justify-content-end mt-1 mt-sm-0">
  188. <button
  189. type="button"
  190. data-testid="btn-create-page-under-below"
  191. className="grw-btn-create-page btn btn-outline-primary rounded-pill text-nowrap ml-3"
  192. onClick={createInputPage}
  193. disabled={isMatchedWithUserHomePagePath}
  194. >
  195. <i className="icon-fw icon-doc"></i>{t('Create')}
  196. </button>
  197. </div>
  198. </div>
  199. { isMatchedWithUserHomePagePath && (
  200. <p className="text-danger mt-2">Error: Cannot create page under /user page directory.</p>
  201. ) }
  202. </fieldset>
  203. </div>
  204. );
  205. }
  206. function renderTemplatePageForm() {
  207. return (
  208. <div className="row">
  209. <fieldset className="col-12">
  210. <h3 className="grw-modal-head pb-2">
  211. {t('template.modal_label.Create template under')}<br />
  212. <code className="h6">{pathname}</code>
  213. </h3>
  214. <div className="d-sm-flex align-items-center justify-items-between">
  215. <div id="dd-template-type" className="dropdown flex-fill">
  216. <button id="template-type" type="button" className="btn btn-secondary btn dropdown-toggle w-100" data-toggle="dropdown">
  217. {template == null && t('template.option_label.select')}
  218. {template === 'children' && t('template.children.label')}
  219. {template === 'decendants' && t('template.decendants.label')}
  220. </button>
  221. <div className="dropdown-menu" aria-labelledby="userMenu">
  222. <button className="dropdown-item" type="button" onClick={() => onChangeTemplateHandler('children')}>
  223. {t('template.children.label')} (_template)<br className="d-block d-md-none" />
  224. <small className="text-muted text-wrap">- {t('template.children.desc')}</small>
  225. </button>
  226. <button className="dropdown-item" type="button" onClick={() => onChangeTemplateHandler('decendants')}>
  227. {t('template.decendants.label')} (__template) <br className="d-block d-md-none" />
  228. <small className="text-muted">- {t('template.decendants.desc')}</small>
  229. </button>
  230. </div>
  231. </div>
  232. <div className="d-flex justify-content-end mt-1 mt-sm-0">
  233. <button
  234. type="button"
  235. className={`grw-btn-create-page btn btn-outline-primary rounded-pill text-nowrap ml-3 ${template == null && 'disabled'}`}
  236. onClick={createTemplatePage}
  237. >
  238. <i className="icon-fw icon-doc"></i>{t('Edit')}
  239. </button>
  240. </div>
  241. </div>
  242. </fieldset>
  243. </div>
  244. );
  245. }
  246. return (
  247. <Modal
  248. size="lg"
  249. isOpen={isOpened}
  250. toggle={() => closeCreateModal()}
  251. data-testid="page-create-modal"
  252. className="grw-create-page"
  253. autoFocus={false}
  254. >
  255. <ModalHeader tag="h4" toggle={() => closeCreateModal()} className="bg-primary text-light">
  256. {t('New Page')}
  257. </ModalHeader>
  258. <ModalBody>
  259. {renderCreateTodayForm()}
  260. {renderInputPageForm()}
  261. {renderTemplatePageForm()}
  262. </ModalBody>
  263. </Modal>
  264. );
  265. };
  266. /**
  267. * Wrapper component for using unstated
  268. */
  269. const ModalControlWrapper = withUnstatedContainers(PageCreateModal, [AppContainer]);
  270. PageCreateModal.propTypes = {
  271. t: PropTypes.func.isRequired, // i18next
  272. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  273. };
  274. export default withTranslation()(ModalControlWrapper);