PageCreateModal.jsx 11 KB

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