PageCreateModal.jsx 9.6 KB

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