PageCreateModal.jsx 8.4 KB

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