yapi/client/containers/Project/Project.js

82 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-08-10 16:00:52 +08:00
import React, { Component } from 'react'
2017-08-11 17:24:50 +08:00
import { connect } from 'react-redux';
2017-08-10 16:00:52 +08:00
import PropTypes from 'prop-types'
2017-08-21 10:18:56 +08:00
import { Route, Switch, Redirect, matchPath } from 'react-router-dom';
2017-08-10 20:56:08 +08:00
import { Subnav } from '../../components/index'
2017-08-21 10:18:56 +08:00
import { getProject } from '../../reducer/modules/project';
2017-08-11 17:29:37 +08:00
import Interface from './Interface/Interface.js'
2017-08-14 15:17:05 +08:00
import Activity from './Activity/Activity.js'
2017-08-17 12:09:08 +08:00
import Setting from './Setting/Setting.js'
2017-08-10 16:00:52 +08:00
2017-08-11 17:24:50 +08:00
@connect(
state => {
return {
curProject: state.project.currProject
2017-08-11 17:24:50 +08:00
}
},
{
getProject
2017-08-10 16:00:52 +08:00
}
2017-08-11 17:24:50 +08:00
)
2017-08-21 10:18:56 +08:00
2017-08-11 17:24:50 +08:00
export default class Project extends Component {
2017-08-10 16:00:52 +08:00
2017-08-11 17:24:50 +08:00
static propTypes = {
match: PropTypes.object,
curProject: PropTypes.object,
2017-08-21 10:18:56 +08:00
getProject: PropTypes.func,
location: PropTypes.object
2017-08-10 16:00:52 +08:00
}
constructor(props) {
super(props)
}
componentWillMount() {
2017-08-11 18:57:58 +08:00
this.props.getProject(this.props.match.params.id)
2017-08-10 16:00:52 +08:00
}
2017-08-21 10:18:56 +08:00
render() {
const { match, location } = this.props;
const routers = {
activity: { name: '动态', path: "/project/:id/activity" },
interface: { name: '接口', path: "/project/:id/interface/:action" },
setting: { name: '设置', path: "/project/:id/setting" }
}
let key, defaultName;
for (key in routers) {
if (matchPath(location.pathname, {
path: routers[key].path
}) !== null) {
defaultName = routers[key].name;
break;
}
}
2017-08-10 16:00:52 +08:00
return (
<div>
2017-08-10 20:56:08 +08:00
<Subnav
2017-08-21 10:18:56 +08:00
default={defaultName}
2017-08-10 20:56:08 +08:00
data={[{
2017-08-21 10:18:56 +08:00
name: routers.interface.name,
2017-08-16 18:04:16 +08:00
path: `/project/${match.params.id}/interface/api`
2017-08-10 20:56:08 +08:00
}, {
2017-08-21 10:18:56 +08:00
name: routers.setting.name,
2017-08-11 10:44:33 +08:00
path: `/project/${match.params.id}/setting`
2017-08-10 20:56:08 +08:00
}, {
2017-08-21 10:18:56 +08:00
name: routers.activity.name,
2017-08-11 10:44:33 +08:00
path: `/project/${match.params.id}/activity`
2017-08-21 10:18:56 +08:00
}]} />
2017-08-10 20:08:23 +08:00
<Switch>
2017-08-21 10:18:56 +08:00
<Redirect exact from="/project/:id" to={`/project/${match.params.id}/interface/api`} />
<Route path={routers.activity.path} component={Activity} />
<Route path={routers.interface.path} component={Interface} />
<Route path={routers.setting.path} component={Setting} />
2017-08-10 20:08:23 +08:00
</Switch>
2017-08-10 16:00:52 +08:00
</div>
)
}
}