PageCreateModal.jsx 9.4 KB

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