Importer.jsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import React, { Fragment } from 'react';
  2. import { withTranslation } from 'react-i18next';
  3. import PropTypes from 'prop-types';
  4. import AppContainer from '../../services/AppContainer';
  5. import { createSubscribedElement } from '../UnstatedUtils';
  6. import { toastSuccess, toastError } from '../../util/apiNotification';
  7. class Importer extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. esaTeamName: '',
  12. esaAccessToken: '',
  13. };
  14. this.esaHandleSubmit = this.esaHandleSubmit.bind(this);
  15. this.esaHandleSubmitTest = this.esaHandleSubmitTest.bind(this);
  16. this.esaHandleSubmitUpdate = this.esaHandleSubmitUpdate.bind(this);
  17. this.handleInputValue = this.handleInputValue.bind(this);
  18. }
  19. handleInputValue(event) {
  20. this.setState({
  21. [event.target.name]: event.target.value,
  22. });
  23. }
  24. esaHandleSubmit() {
  25. try {
  26. const params = {
  27. esaTeamName: this.state.esaTeamName,
  28. esaAccessToken: this.state.esaAccessToken,
  29. };
  30. this.props.appContainer.apiPost('/admin/import/esa', params);
  31. toastSuccess('Import posts from esa success.');
  32. }
  33. catch (error) {
  34. toastError(error, 'Error occurred in importing pages from esa.io');
  35. }
  36. }
  37. esaHandleSubmitTest() {
  38. try {
  39. const params = {
  40. esaTeamName: this.state.esaTeamName,
  41. esaAccessToken: this.state.esaAccessToken,
  42. };
  43. this.props.appContainer.apiPost('/admin/import/testEsaAPI', params);
  44. toastSuccess('Test connection to esa success.');
  45. }
  46. catch (error) {
  47. toastError(error, 'Test connection to esa failed.');
  48. }
  49. }
  50. esaHandleSubmitUpdate() {
  51. try {
  52. const params = {
  53. esaTeamName: this.state.esaTeamName,
  54. esaAccessToken: this.state.esaAccessToken,
  55. };
  56. this.props.appContainer.apiPost('/admin/settings/importerEsa', params);
  57. toastSuccess('Update');
  58. }
  59. catch (error) {
  60. toastError(error);
  61. }
  62. }
  63. render() {
  64. const { esaTeamName, esaAccessToken } = this.state;
  65. const { t } = this.props;
  66. return (
  67. <Fragment>
  68. <form
  69. className="form-horizontal"
  70. id="importerSettingFormEsa"
  71. role="form"
  72. >
  73. <fieldset>
  74. <legend>{ t('importer_management.import_from_esa') }</legend>
  75. <table className="table table-bordered table-mapping">
  76. <thead>
  77. <tr>
  78. <th width="45%">esa.io</th>
  79. <th width="10%"></th>
  80. <th>GROWI</th>
  81. </tr>
  82. </thead>
  83. <tbody>
  84. <tr>
  85. <th>{ t('Article') }</th>
  86. <th><i className="icon-arrow-right-circle text-success"></i></th>
  87. <th>{ t('Page') }</th>
  88. </tr>
  89. <tr>
  90. <th>{ t('Category') }</th>
  91. <th><i className="icon-arrow-right-circle text-success"></i></th>
  92. <th>{ t('Page Path') }</th>
  93. </tr>
  94. <tr>
  95. <th>{ t('User') }</th>
  96. <th></th>
  97. <th>(TBD)</th>
  98. </tr>
  99. </tbody>
  100. </table>
  101. <div className="well well-sm mb-0 small">
  102. <ul>
  103. <li>{ t('importer_management.page_skip') }</li>
  104. </ul>
  105. </div>
  106. <div className="form-group">
  107. <input type="password" name="dummypass" style={{ display: 'none', top: '-100px', left: '-100px' }} />
  108. </div>
  109. <div className="form-group">
  110. <label htmlFor="settingForm[importer:esa:team_name]" className="col-xs-3 control-label">
  111. { t('importer_management.esa_settings.team_name') }
  112. </label>
  113. <div className="col-xs-6">
  114. <input className="form-control" type="text" name="esaTeamName" value={esaTeamName} onChange={this.handleInputValue} />
  115. </div>
  116. </div>
  117. <div className="form-group">
  118. <label htmlFor="settingForm[importer:esa:access_token]" className="col-xs-3 control-label">
  119. { t('importer_management.esa_settings.access_token') }
  120. </label>
  121. <div className="col-xs-6">
  122. <input className="form-control" type="password" name="esaAccessToken" value={esaAccessToken} onChange={this.handleInputValue} />
  123. </div>
  124. </div>
  125. <div className="form-group">
  126. <input type="hidden" name="_csrf" value={this.props.csrf} />
  127. <div className="col-xs-offset-3 col-xs-6">
  128. <input
  129. id="testConnectionToEsa"
  130. type="button"
  131. className="btn btn-primary btn-esa"
  132. name="Esa"
  133. onClick={this.esaHandleSubmit}
  134. value={t('importer_management.import')}
  135. />
  136. <input type="button" className="btn btn-secondary" onClick={this.esaHandleSubmitUpdate} value={t('Update')} />
  137. <span className="col-xs-offset-1">
  138. <input
  139. name="Esa"
  140. type="button"
  141. id="importFromEsa"
  142. className="btn btn-default btn-esa"
  143. onClick={this.esaHandleSubmitTest}
  144. value={t('importer_management.esa_settings.test_connection')}
  145. />
  146. </span>
  147. </div>
  148. </div>
  149. </fieldset>
  150. </form>
  151. </Fragment>
  152. );
  153. }
  154. }
  155. /**
  156. * Wrapper component for using unstated
  157. */
  158. const ImporterWrapper = (props) => {
  159. return createSubscribedElement(Importer, props, [AppContainer]);
  160. };
  161. Importer.propTypes = {
  162. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  163. t: PropTypes.func.isRequired, // i18next
  164. csrf: PropTypes.string,
  165. };
  166. export default withTranslation()(ImporterWrapper);