PageCreateModal.jsx 8.3 KB

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