PageCreateModal.jsx 10 KB

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