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