PageCreateModal.jsx 10 KB

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