mock平台

ImportInterface.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import React, { PureComponent as Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { Table, Select, Tooltip, Icon } from 'antd';
  4. import variable from '../../../../constants/variable';
  5. import { connect } from 'react-redux';
  6. const Option = Select.Option;
  7. import { fetchInterfaceListMenu } from '../../../../reducer/modules/interface.js';
  8. @connect(
  9. state => {
  10. return {
  11. projectList: state.project.projectList,
  12. list: state.inter.list
  13. };
  14. },
  15. {
  16. fetchInterfaceListMenu
  17. }
  18. )
  19. export default class ImportInterface extends Component {
  20. constructor(props) {
  21. super(props);
  22. }
  23. state = {
  24. selectedRowKeys: [],
  25. categoryCount: {},
  26. project: this.props.currProjectId
  27. };
  28. static propTypes = {
  29. list: PropTypes.array,
  30. selectInterface: PropTypes.func,
  31. projectList: PropTypes.array,
  32. currProjectId: PropTypes.string,
  33. fetchInterfaceListMenu: PropTypes.func
  34. };
  35. async componentDidMount() {
  36. // console.log(this.props.currProjectId)
  37. await this.props.fetchInterfaceListMenu(this.props.currProjectId);
  38. }
  39. // 切换项目
  40. onChange = async val => {
  41. this.setState({
  42. project: val,
  43. selectedRowKeys: [],
  44. categoryCount: {}
  45. });
  46. await this.props.fetchInterfaceListMenu(val);
  47. };
  48. render() {
  49. const { list, projectList } = this.props;
  50. // const { selectedRowKeys } = this.state;
  51. const data = list.map(item => {
  52. return {
  53. key: 'category_' + item._id,
  54. title: item.name,
  55. isCategory: true,
  56. children: item.list
  57. ? item.list.map(e => {
  58. e.key = e._id;
  59. e.categoryKey = 'category_' + item._id;
  60. e.categoryLength = item.list.length;
  61. return e;
  62. })
  63. : []
  64. };
  65. });
  66. const self = this;
  67. const rowSelection = {
  68. // onChange: (selectedRowKeys) => {
  69. // console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
  70. // if (selectedRows.isCategory) {
  71. // const selectedRowKeys = selectedRows.children.map(item => item._id)
  72. // this.setState({ selectedRowKeys })
  73. // }
  74. // this.props.onChange(selectedRowKeys.filter(id => ('' + id).indexOf('category') === -1));
  75. // },
  76. onSelect: (record, selected) => {
  77. // console.log(record, selected, selectedRows);
  78. const oldSelecteds = self.state.selectedRowKeys;
  79. const categoryCount = self.state.categoryCount;
  80. const categoryKey = record.categoryKey;
  81. const categoryLength = record.categoryLength;
  82. let selectedRowKeys = [];
  83. if (record.isCategory) {
  84. selectedRowKeys = record.children.map(item => item._id).concat(record.key);
  85. if (selected) {
  86. selectedRowKeys = selectedRowKeys
  87. .filter(id => oldSelecteds.indexOf(id) === -1)
  88. .concat(oldSelecteds);
  89. categoryCount[categoryKey] = categoryLength;
  90. } else {
  91. selectedRowKeys = oldSelecteds.filter(id => selectedRowKeys.indexOf(id) === -1);
  92. categoryCount[categoryKey] = 0;
  93. }
  94. } else {
  95. if (selected) {
  96. selectedRowKeys = oldSelecteds.concat(record._id);
  97. if (categoryCount[categoryKey]) {
  98. categoryCount[categoryKey] += 1;
  99. } else {
  100. categoryCount[categoryKey] = 1;
  101. }
  102. if (categoryCount[categoryKey] === record.categoryLength) {
  103. selectedRowKeys.push(categoryKey);
  104. }
  105. } else {
  106. selectedRowKeys = oldSelecteds.filter(id => id !== record._id);
  107. if (categoryCount[categoryKey]) {
  108. categoryCount[categoryKey] -= 1;
  109. }
  110. selectedRowKeys = selectedRowKeys.filter(id => id !== categoryKey);
  111. }
  112. }
  113. self.setState({ selectedRowKeys, categoryCount });
  114. self.props.selectInterface(
  115. selectedRowKeys.filter(id => ('' + id).indexOf('category') === -1),
  116. self.state.project
  117. );
  118. },
  119. onSelectAll: selected => {
  120. // console.log(selected, selectedRows, changeRows);
  121. let selectedRowKeys = [];
  122. let categoryCount = self.state.categoryCount;
  123. if (selected) {
  124. data.forEach(item => {
  125. if (item.children) {
  126. categoryCount['category_' + item._id] = item.children.length;
  127. selectedRowKeys = selectedRowKeys.concat(item.children.map(item => item._id));
  128. }
  129. });
  130. selectedRowKeys = selectedRowKeys.concat(data.map(item => item.key));
  131. } else {
  132. categoryCount = {};
  133. selectedRowKeys = [];
  134. }
  135. self.setState({ selectedRowKeys, categoryCount });
  136. self.props.selectInterface(
  137. selectedRowKeys.filter(id => ('' + id).indexOf('category') === -1),
  138. self.state.project
  139. );
  140. },
  141. selectedRowKeys: self.state.selectedRowKeys
  142. };
  143. const columns = [
  144. {
  145. title: '接口名称',
  146. dataIndex: 'title',
  147. width: '30%'
  148. },
  149. {
  150. title: '接口路径',
  151. dataIndex: 'path',
  152. width: '40%'
  153. },
  154. {
  155. title: '请求方法',
  156. dataIndex: 'method',
  157. render: item => {
  158. let methodColor = variable.METHOD_COLOR[item ? item.toLowerCase() : 'get'];
  159. return (
  160. <span
  161. style={{
  162. color: methodColor.color,
  163. backgroundColor: methodColor.bac,
  164. borderRadius: 4
  165. }}
  166. className="colValue"
  167. >
  168. {item}
  169. </span>
  170. );
  171. }
  172. },
  173. {
  174. title: (
  175. <span>
  176. 状态{' '}
  177. <Tooltip title="筛选满足条件的接口集合">
  178. <Icon type="question-circle-o" />
  179. </Tooltip>
  180. </span>
  181. ),
  182. dataIndex: 'status',
  183. render: text => {
  184. return (
  185. text &&
  186. (text === 'done' ? (
  187. <span className="tag-status done">已完成</span>
  188. ) : (
  189. <span className="tag-status undone">未完成</span>
  190. ))
  191. );
  192. },
  193. filters: [
  194. {
  195. text: '已完成',
  196. value: 'done'
  197. },
  198. {
  199. text: '未完成',
  200. value: 'undone'
  201. }
  202. ],
  203. onFilter: (value, record) => {
  204. let arr = record.children.filter(item => {
  205. return item.status.indexOf(value) === 0;
  206. });
  207. return arr.length > 0;
  208. // record.status.indexOf(value) === 0
  209. }
  210. }
  211. ];
  212. return (
  213. <div>
  214. <div className="select-project">
  215. <span>选择要导入的项目: </span>
  216. <Select value={this.state.project} style={{ width: 200 }} onChange={this.onChange}>
  217. {projectList.map(item => {
  218. return item.projectname ? (
  219. ''
  220. ) : (
  221. <Option value={`${item._id}`} key={item._id}>
  222. {item.name}
  223. </Option>
  224. );
  225. })}
  226. </Select>
  227. </div>
  228. <Table columns={columns} rowSelection={rowSelection} dataSource={data} pagination={false} />
  229. </div>
  230. );
  231. }
  232. }