mock平台

Project.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import React, { PureComponent as Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import { Route, Switch, Redirect, matchPath } from 'react-router-dom';
  5. import { Subnav } from '../../components/index';
  6. import { fetchGroupMsg } from '../../reducer/modules/group';
  7. import { setBreadcrumb } from '../../reducer/modules/user';
  8. import { getProject } from '../../reducer/modules/project';
  9. import Interface from './Interface/Interface.js';
  10. import Activity from './Activity/Activity.js';
  11. import Setting from './Setting/Setting.js';
  12. import Loading from '../../components/Loading/Loading';
  13. import ProjectMember from './Setting/ProjectMember/ProjectMember.js';
  14. import ProjectData from './Setting/ProjectData/ProjectData.js';
  15. const plugin = require('client/plugin.js');
  16. @connect(
  17. state => {
  18. return {
  19. curProject: state.project.currProject,
  20. currGroup: state.group.currGroup
  21. };
  22. },
  23. {
  24. getProject,
  25. fetchGroupMsg,
  26. setBreadcrumb
  27. }
  28. )
  29. export default class Project extends Component {
  30. static propTypes = {
  31. match: PropTypes.object,
  32. curProject: PropTypes.object,
  33. getProject: PropTypes.func,
  34. location: PropTypes.object,
  35. fetchGroupMsg: PropTypes.func,
  36. setBreadcrumb: PropTypes.func,
  37. currGroup: PropTypes.object
  38. };
  39. constructor(props) {
  40. super(props);
  41. }
  42. async componentWillMount() {
  43. await this.props.getProject(this.props.match.params.id);
  44. await this.props.fetchGroupMsg(this.props.curProject.group_id);
  45. this.props.setBreadcrumb([
  46. {
  47. name: this.props.currGroup.group_name,
  48. href: '/group/' + this.props.currGroup._id
  49. },
  50. {
  51. name: this.props.curProject.name
  52. }
  53. ]);
  54. }
  55. async componentWillReceiveProps(nextProps) {
  56. const currProjectId = this.props.match.params.id;
  57. const nextProjectId = nextProps.match.params.id;
  58. if (currProjectId !== nextProjectId) {
  59. await this.props.getProject(nextProjectId);
  60. await this.props.fetchGroupMsg(this.props.curProject.group_id);
  61. this.props.setBreadcrumb([
  62. {
  63. name: this.props.currGroup.group_name,
  64. href: '/group/' + this.props.currGroup._id
  65. },
  66. {
  67. name: this.props.curProject.name
  68. }
  69. ]);
  70. }
  71. }
  72. render() {
  73. const { match, location } = this.props;
  74. let routers = {
  75. interface: { name: '接口', path: '/project/:id/interface/:action', component: Interface },
  76. activity: { name: '动态', path: '/project/:id/activity', component: Activity },
  77. data: { name: '数据管理', path: '/project/:id/data', component: ProjectData },
  78. members: { name: '成员管理', path: '/project/:id/members', component: ProjectMember },
  79. setting: { name: '设置', path: '/project/:id/setting', component: Setting }
  80. };
  81. plugin.emitHook('sub_nav', routers);
  82. let key, defaultName;
  83. for (key in routers) {
  84. if (
  85. matchPath(location.pathname, {
  86. path: routers[key].path
  87. }) !== null
  88. ) {
  89. defaultName = routers[key].name;
  90. break;
  91. }
  92. }
  93. // let subnavData = [{
  94. // name: routers.interface.name,
  95. // path: `/project/${match.params.id}/interface/api`
  96. // }, {
  97. // name: routers.activity.name,
  98. // path: `/project/${match.params.id}/activity`
  99. // }, {
  100. // name: routers.data.name,
  101. // path: `/project/${match.params.id}/data`
  102. // }, {
  103. // name: routers.members.name,
  104. // path: `/project/${match.params.id}/members`
  105. // }, {
  106. // name: routers.setting.name,
  107. // path: `/project/${match.params.id}/setting`
  108. // }];
  109. let subnavData = [];
  110. Object.keys(routers).forEach(key => {
  111. let item = routers[key];
  112. let value = {};
  113. if (key === 'interface') {
  114. value = {
  115. name: item.name,
  116. path: `/project/${match.params.id}/interface/api`
  117. };
  118. } else {
  119. value = {
  120. name: item.name,
  121. path: item.path.replace(/\:id/gi, match.params.id)
  122. };
  123. }
  124. subnavData.push(value);
  125. });
  126. if (this.props.currGroup.type === 'private') {
  127. subnavData = subnavData.filter(item => {
  128. return item.name != '成员管理';
  129. });
  130. }
  131. if (Object.keys(this.props.curProject).length === 0) {
  132. return <Loading visible />;
  133. }
  134. return (
  135. <div>
  136. <Subnav default={defaultName} data={subnavData} />
  137. <Switch>
  138. <Redirect exact from="/project/:id" to={`/project/${match.params.id}/interface/api`} />
  139. {/* <Route path={routers.activity.path} component={Activity} />
  140. <Route path={routers.setting.path} component={Setting} />
  141. {this.props.currGroup.type !== 'private' ?
  142. <Route path={routers.members.path} component={routers.members.component}/>
  143. : null
  144. }
  145. <Route path={routers.data.path} component={ProjectData} /> */}
  146. {Object.keys(routers).map(key => {
  147. let item = routers[key];
  148. return key === 'members' ? (
  149. this.props.currGroup.type !== 'private' ? (
  150. <Route path={item.path} component={item.component} key={key} />
  151. ) : null
  152. ) : (
  153. <Route path={item.path} component={item.component} key={key} />
  154. );
  155. })}
  156. </Switch>
  157. </div>
  158. );
  159. }
  160. }