PageCreateModal.jsx 10 KB

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