PageCreateModal.jsx 11 KB

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