mock平台

ProjectCard.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import './ProjectCard.scss';
  2. import React, { PureComponent as Component } from 'react';
  3. import { Card, Icon, Tooltip, Modal, Alert, Input, message } from 'antd';
  4. import { connect } from 'react-redux';
  5. import { delFollow, addFollow } from '../../reducer/modules/follow';
  6. import PropTypes from 'prop-types';
  7. import { withRouter } from 'react-router';
  8. import { debounce } from '../../common';
  9. import constants from '../../constants/variable.js';
  10. import produce from 'immer';
  11. import { getProject, checkProjectName, copyProjectMsg } from '../../reducer/modules/project';
  12. import { trim } from '../../common.js';
  13. const confirm = Modal.confirm;
  14. @connect(
  15. state => {
  16. return {
  17. uid: state.user.uid,
  18. currPage: state.project.currPage
  19. };
  20. },
  21. {
  22. delFollow,
  23. addFollow,
  24. getProject,
  25. checkProjectName,
  26. copyProjectMsg
  27. }
  28. )
  29. @withRouter
  30. class ProjectCard extends Component {
  31. constructor(props) {
  32. super(props);
  33. this.add = debounce(this.add, 400);
  34. this.del = debounce(this.del, 400);
  35. }
  36. static propTypes = {
  37. projectData: PropTypes.object,
  38. uid: PropTypes.number,
  39. inFollowPage: PropTypes.bool,
  40. callbackResult: PropTypes.func,
  41. history: PropTypes.object,
  42. delFollow: PropTypes.func,
  43. addFollow: PropTypes.func,
  44. isShow: PropTypes.bool,
  45. getProject: PropTypes.func,
  46. checkProjectName: PropTypes.func,
  47. copyProjectMsg: PropTypes.func,
  48. currPage: PropTypes.number
  49. };
  50. copy = async projectName => {
  51. const id = this.props.projectData._id;
  52. let projectData = await this.props.getProject(id);
  53. let data = projectData.payload.data.data;
  54. let newData = produce(data, draftData => {
  55. draftData.preName = draftData.name;
  56. draftData.name = projectName;
  57. });
  58. await this.props.copyProjectMsg(newData);
  59. message.success('项目复制成功');
  60. this.props.callbackResult();
  61. };
  62. // 复制项目的二次确认
  63. showConfirm = () => {
  64. const that = this;
  65. confirm({
  66. title: '确认复制 ' + that.props.projectData.name + ' 项目吗?',
  67. okText: '确认',
  68. cancelText: '取消',
  69. content: (
  70. <div style={{ marginTop: '10px', fontSize: '13px', lineHeight: '25px' }}>
  71. <Alert
  72. message={`该操作将会复制 ${
  73. that.props.projectData.name
  74. } 下的所有接口集合,但不包括测试集合中的接口`}
  75. type="info"
  76. />
  77. <div style={{ marginTop: '16px' }}>
  78. <p>
  79. <b>项目名称:</b>
  80. </p>
  81. <Input id="project_name" placeholder="项目名称" />
  82. </div>
  83. </div>
  84. ),
  85. async onOk() {
  86. const projectName = trim(document.getElementById('project_name').value);
  87. // 查询项目名称是否重复
  88. const group_id = that.props.projectData.group_id;
  89. await that.props.checkProjectName(projectName, group_id);
  90. that.copy(projectName);
  91. },
  92. iconType: 'copy',
  93. onCancel() {}
  94. });
  95. };
  96. del = () => {
  97. const id = this.props.projectData.projectid || this.props.projectData._id;
  98. this.props.delFollow(id).then(res => {
  99. if (res.payload.data.errcode === 0) {
  100. this.props.callbackResult();
  101. // message.success('已取消关注!'); // 星号已做出反馈 无需重复提醒用户
  102. }
  103. });
  104. };
  105. add = () => {
  106. const { uid, projectData } = this.props;
  107. const param = {
  108. uid,
  109. projectid: projectData._id,
  110. projectname: projectData.name,
  111. icon: projectData.icon || constants.PROJECT_ICON[0],
  112. color: projectData.color || constants.PROJECT_COLOR.blue
  113. };
  114. this.props.addFollow(param).then(res => {
  115. if (res.payload.data.errcode === 0) {
  116. this.props.callbackResult();
  117. // message.success('已添加关注!'); // 星号已做出反馈 无需重复提醒用户
  118. }
  119. });
  120. };
  121. render() {
  122. const { projectData, inFollowPage, isShow } = this.props;
  123. return (
  124. <div className="card-container">
  125. <Card
  126. bordered={false}
  127. className="m-card"
  128. onClick={() =>
  129. this.props.history.push('/project/' + (projectData.projectid || projectData._id))
  130. }
  131. >
  132. <Icon
  133. type={projectData.icon || 'star-o'}
  134. className="ui-logo"
  135. style={{
  136. backgroundColor:
  137. constants.PROJECT_COLOR[projectData.color] || constants.PROJECT_COLOR.blue
  138. }}
  139. />
  140. <h4 className="ui-title">{projectData.name || projectData.projectname}</h4>
  141. </Card>
  142. <div
  143. className="card-btns"
  144. onClick={projectData.follow || inFollowPage ? this.del : this.add}
  145. >
  146. <Tooltip
  147. placement="rightTop"
  148. title={projectData.follow || inFollowPage ? '取消关注' : '添加关注'}
  149. >
  150. <Icon
  151. type={projectData.follow || inFollowPage ? 'star' : 'star-o'}
  152. className={'icon ' + (projectData.follow || inFollowPage ? 'active' : '')}
  153. />
  154. </Tooltip>
  155. </div>
  156. {isShow && (
  157. <div className="copy-btns" onClick={this.showConfirm}>
  158. <Tooltip placement="rightTop" title="复制项目">
  159. <Icon type="copy" className="icon" />
  160. </Tooltip>
  161. </div>
  162. )}
  163. </div>
  164. );
  165. }
  166. }
  167. export default ProjectCard;