PageCreateModal.jsx 11 KB

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