PageCreateModal.jsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. /**
  70. * access template page
  71. */
  72. function createTemplatePage() {
  73. const pageName = (template === 'children') ? '_template' : '__template';
  74. window.location.href = encodeURI(urljoin(parentPath, pageName, '#edit'));
  75. }
  76. function renderCreateTodayForm() {
  77. return (
  78. <div className="row form-group">
  79. <fieldset className="col-12 mb-4">
  80. <h3 className="grw-modal-head pb-2">{ t("Create today's") }</h3>
  81. <div className="d-flex">
  82. <div className="create-page-input-row d-flex align-items-center">
  83. <span>{userPageRootPath}/</span>
  84. <input
  85. type="text"
  86. className="page-today-input1 form-control text-center"
  87. value={todayInput1}
  88. onChange={e => onChangeTodayInput1Handler(e.target.value)}
  89. />
  90. <span className="page-today-suffix">/{now}/</span>
  91. <input
  92. type="text"
  93. className="page-today-input2 form-control"
  94. id="page-today-input2"
  95. placeholder={t('Input page name (optional)')}
  96. value={todayInput2}
  97. onChange={e => onChangeTodayInput2Handler(e.target.value)}
  98. />
  99. </div>
  100. <div className="create-page-button-container">
  101. <button type="button" className="btn btn-outline-primary rounded-pill" onClick={createTodayPage}>
  102. <i className="icon-fw icon-doc"></i>{ t('Create') }
  103. </button>
  104. </div>
  105. </div>
  106. </fieldset>
  107. </div>
  108. );
  109. }
  110. function renderInputPageForm() {
  111. return (
  112. <div className="row form-group">
  113. <fieldset className="col-12 mb-4">
  114. <h3 className="grw-modal-head pb-2">{ t('Create under') }</h3>
  115. <div className="d-flex create-page-input-container">
  116. <div className="create-page-input-row d-flex align-items-center">
  117. {isReachable
  118. // GW-2355 refactor typeahead
  119. ? <PagePathAutoComplete crowi={appContainer} initializedPath={path} addTrailingSlash />
  120. : (
  121. <input
  122. type="text"
  123. value={pageNameInput}
  124. className="page-name-input form-control"
  125. placeholder={t('Input page name')}
  126. onChange={e => onChangePageNameInputHandler(e.target.value)}
  127. required
  128. />
  129. )}
  130. </div>
  131. <div className="create-page-button-container">
  132. <button type="submit" className="btn btn-outline-primary rounded-pill" onClick={createInputPage}>
  133. <i className="icon-fw icon-doc"></i>{ t('Create') }
  134. </button>
  135. </div>
  136. </div>
  137. </fieldset>
  138. </div>
  139. );
  140. }
  141. function renderTemplatePageForm() {
  142. return (
  143. <div className="row form-group">
  144. <fieldset className="col-12">
  145. <h3 className="grw-modal-head pb-2">{ t('template.modal_label.Create template under')}<br />
  146. <code>{path}</code>
  147. </h3>
  148. <div className="d-flex create-page-input-container">
  149. <div className="create-page-input-row d-flex align-items-center">
  150. <div id="dd-template-type" className="dropdown w-100">
  151. <button id="template-type" type="button" className="btn btn-secondary btn dropdown-toggle" data-toggle="dropdown">
  152. {template == null && t('template.option_label.select') }
  153. {template === 'children' && t('template.children.label')}
  154. {template === 'decendants' && t('template.decendants.label')}
  155. </button>
  156. <div className="dropdown-menu" aria-labelledby="userMenu">
  157. <a className="dropdown-item" type="button" onClick={() => onChangeTemplateHandler('children')}>
  158. { t('template.children.label') } (_template)<br className="d-block d-md-none" />
  159. <small className="text-muted text-wrap">- { t('template.children.desc') }</small>
  160. </a>
  161. <a className="dropdown-item" type="button" onClick={() => onChangeTemplateHandler('decendants')}>
  162. { t('template.decendants.label') } (__template) <br className="d-block d-md-none" />
  163. <small className="text-muted">- { t('template.decendants.desc') }</small>
  164. </a>
  165. </div>
  166. </div>
  167. </div>
  168. <div className="create-page-button-container">
  169. <button type="button" className={`btn btn-outline-primary rounded-pill ${template == null && 'disabled'}`} onClick={createTemplatePage}>
  170. <i className="icon-fw icon-doc"></i>
  171. <span>{ t('Edit') }</span>
  172. </button>
  173. </div>
  174. </div>
  175. </fieldset>
  176. </div>
  177. );
  178. }
  179. return (
  180. <Modal size="lg" isOpen={appContainer.state.isPageCreateModalShown} toggle={appContainer.closePageCreateModal}>
  181. <ModalHeader tag="h4" toggle={appContainer.closePageCreateModal} className="bg-primary text-light">
  182. { t('New Page') }
  183. </ModalHeader>
  184. <ModalBody>
  185. {renderCreateTodayForm}
  186. {renderInputPageForm}
  187. {renderTemplatePageForm}
  188. </ModalBody>
  189. </Modal>
  190. );
  191. };
  192. /**
  193. * Wrapper component for using unstated
  194. */
  195. const ModalControlWrapper = (props) => {
  196. return createSubscribedElement(PageCreateModal, props, [AppContainer, PageContainer]);
  197. };
  198. PageCreateModal.propTypes = {
  199. t: PropTypes.func.isRequired, // i18next
  200. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  201. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  202. };
  203. export default withTranslation()(ModalControlWrapper);