PageCreateModal.jsx 9.1 KB

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