PageCreateModal.jsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 type="button" className="grw-btn-create-page btn btn-outline-primary rounded-pill text-nowrap ml-3" onClick={createInputPage}>
  180. <i className="icon-fw icon-doc"></i>{t('Create')}
  181. </button>
  182. </div>
  183. </div>
  184. </fieldset>
  185. </div>
  186. );
  187. }
  188. function renderTemplatePageForm() {
  189. return (
  190. <div className="row">
  191. <fieldset className="col-12">
  192. <h3 className="grw-modal-head pb-2">
  193. {t('template.modal_label.Create template under')}<br />
  194. <code className="h6">{pathname}</code>
  195. </h3>
  196. <div className="d-sm-flex align-items-center justify-items-between">
  197. <div id="dd-template-type" className="dropdown flex-fill">
  198. <button id="template-type" type="button" className="btn btn-secondary btn dropdown-toggle w-100" data-toggle="dropdown">
  199. {template == null && t('template.option_label.select')}
  200. {template === 'children' && t('template.children.label')}
  201. {template === 'decendants' && t('template.decendants.label')}
  202. </button>
  203. <div className="dropdown-menu" aria-labelledby="userMenu">
  204. <button className="dropdown-item" type="button" onClick={() => onChangeTemplateHandler('children')}>
  205. {t('template.children.label')} (_template)<br className="d-block d-md-none" />
  206. <small className="text-muted text-wrap">- {t('template.children.desc')}</small>
  207. </button>
  208. <button className="dropdown-item" type="button" onClick={() => onChangeTemplateHandler('decendants')}>
  209. {t('template.decendants.label')} (__template) <br className="d-block d-md-none" />
  210. <small className="text-muted">- {t('template.decendants.desc')}</small>
  211. </button>
  212. </div>
  213. </div>
  214. <div className="d-flex justify-content-end mt-1 mt-sm-0">
  215. <button
  216. type="button"
  217. className={`grw-btn-create-page btn btn-outline-primary rounded-pill text-nowrap ml-3 ${template == null && 'disabled'}`}
  218. onClick={createTemplatePage}
  219. >
  220. <i className="icon-fw icon-doc"></i>{t('Edit')}
  221. </button>
  222. </div>
  223. </div>
  224. </fieldset>
  225. </div>
  226. );
  227. }
  228. return (
  229. <Modal
  230. size="lg"
  231. isOpen={isOpened}
  232. toggle={() => closeCreateModal()}
  233. className="grw-create-page"
  234. autoFocus={false}
  235. >
  236. <ModalHeader tag="h4" toggle={() => closeCreateModal()} className="bg-primary text-light">
  237. {t('New Page')}
  238. </ModalHeader>
  239. <ModalBody>
  240. {renderCreateTodayForm()}
  241. {renderInputPageForm()}
  242. {renderTemplatePageForm()}
  243. </ModalBody>
  244. </Modal>
  245. );
  246. };
  247. /**
  248. * Wrapper component for using unstated
  249. */
  250. const ModalControlWrapper = withUnstatedContainers(PageCreateModal, [AppContainer]);
  251. PageCreateModal.propTypes = {
  252. t: PropTypes.func.isRequired, // i18next
  253. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  254. };
  255. export default withTranslation()(ModalControlWrapper);