PageCreateModal.jsx 10 KB

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