PageCreateModal.jsx 9.6 KB

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