index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import React, {Component} from 'react';
  2. import {Row, Col, Input, Button} from 'antd';
  3. import './index.css';
  4. import Diff from './diff';
  5. const Search = Input.Search;
  6. class Config extends Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. currentConfig: '',
  11. switch: true,
  12. configs: [
  13. {
  14. name: 'Tenc and Mongo',
  15. cloudServer: 'tencent',
  16. database: 'mongodb',
  17. configLocal: {
  18. SCF_secretID: 'scfscfscfididididiid',
  19. SCF_secretKey: 'i_am_a_key',
  20. API_secretID: 'apiapiapiididididiid',
  21. API_secretKey: 'apiapipaikeykeykey'
  22. }
  23. },
  24. {
  25. name: 'Ali but nedb',
  26. cloudServer: 'Aliyun',
  27. database: 'nedb',
  28. configLocal: {
  29. SCF_secretID: 'sididid34idiid',
  30. SCF_secretKey: 'scfkeyk243weeykkeykey',
  31. API_secretID: 'iidididdapi3eapiapidiid',
  32. API_secretKey: 'apiapipav_ikeykeykey'
  33. }
  34. }
  35. ]
  36. };
  37. if(this.state.configs.length !== 0) {
  38. this.state.currentConfig = this.state.configs[0].name
  39. }
  40. }
  41. switchConfig = (name) => {
  42. return () => {
  43. this.setState({
  44. currentConfig: name
  45. })
  46. }
  47. };
  48. addConfig = (content, name) => {
  49. if (content === 'new') {
  50. this.setState({
  51. configs: [...this.state.configs, {
  52. name,
  53. schemas: [],
  54. cloudServer: '',
  55. database: '',
  56. configLocal: {}
  57. }]
  58. })
  59. } else {
  60. let configs = this.state.configs;
  61. configs.splice(this.state.configs.findIndex(obj => obj.name === name), 1, content);
  62. this.setState({
  63. configs
  64. })
  65. }
  66. };
  67. deleteConfig = (name) => {
  68. let configs = this.state.configs;
  69. configs.splice(this.state.configs.findIndex(obj => obj.name === name), 1);
  70. this.setState({
  71. configs
  72. });
  73. if (this.state.configs.length !== 0) {
  74. this.setState({
  75. currentConfig: this.state.configs[0].name
  76. });
  77. } else {
  78. this.setState({
  79. currentConfig: ''
  80. });
  81. }
  82. };
  83. render() {
  84. return (
  85. <div>
  86. <Row>
  87. <Col span={6}>
  88. <div className='warpper'>
  89. <div className='current'>{this.state.currentConfig}</div>
  90. {
  91. this.state.switch?
  92. <Button className='add' type='dashed' icon="plus" onClick={()=>{this.setState({switch: false})}} /> :
  93. <Search
  94. className='add-input'
  95. placeholder="input config_name"
  96. enterButton="Confirm"
  97. onSearch={value => {
  98. this.setState({
  99. switch: true,
  100. });
  101. this.addConfig('new', value);
  102. }}
  103. disabled={this.state.switch}
  104. />
  105. }
  106. </div>
  107. {
  108. this.state.configs.map((config) => {
  109. return <div key={config.name} onClick={this.switchConfig(config.name)} className='title'>
  110. <Row>
  111. <Col span={22}>Config: ({config.name})</Col>
  112. </Row>
  113. </div>
  114. })
  115. }
  116. </Col>
  117. <Col span={18}>
  118. <Diff addConfig={this.addConfig} deleteConfig={this.deleteConfig} currentConfig={this.state.currentConfig} configs={this.state.configs} />
  119. </Col>
  120. </Row>
  121. </div>
  122. )
  123. }
  124. }
  125. export default Config;