yapi/client/components/Breadcrumb/Breadcrumb.js

132 lines
4.1 KiB
JavaScript
Raw Normal View History

2017-07-28 17:30:56 +08:00
import './Breadcrumb.scss';
import { withRouter, Link } from 'react-router-dom';
import { Breadcrumb } from 'antd';
2017-07-28 21:16:47 +08:00
import axios from 'axios';
2017-07-30 15:06:03 +08:00
import PropTypes from 'prop-types'
2017-07-28 17:30:56 +08:00
import React, { Component } from 'react';
2017-07-28 21:16:47 +08:00
2017-07-28 17:30:56 +08:00
@withRouter
export default class BreadcrumbNavigation extends Component {
constructor(props) {
super(props);
2017-07-28 21:16:47 +08:00
this.state = {
// breadcrumb: [{name:'首页', path: '/'}],
hash: '',
breadcrumb: []
}
}
2017-07-30 15:06:03 +08:00
static propTypes = {
location: PropTypes.object
}
2017-07-28 21:16:47 +08:00
2017-07-30 15:06:03 +08:00
getBreadcrumb = (pathSnippets) => {
// 重置 state 中的 breadcrumb防止重复渲染
this.setState({
breadcrumb: []
});
2017-07-31 10:56:22 +08:00
if (/project|group|add-interface/.test(pathSnippets[0])) {
let type = pathSnippets[0] === 'add-interface' ? 'interface' : pathSnippets[0],
id = pathSnippets[pathSnippets.length-1];
2017-07-31 11:16:36 +08:00
if (pathSnippets.includes('add-interface') && !pathSnippets.includes('edit')) {
2017-07-31 10:56:22 +08:00
type = 'project';
}
2017-07-30 15:06:03 +08:00
const params = { type, id };
axios.get('/api/user/nav', {params: params}).then( (res) => {
2017-07-30 15:06:03 +08:00
const data = res.data.data;
// 依次填入group/projec/interface
if (data.group_name) {
this.setState({
breadcrumb: this.state.breadcrumb.concat([{
name: data.group_name,
path: '/group/' + data.group_id
}])
});
}
if (data.project_name) {
this.setState({
breadcrumb: this.state.breadcrumb.concat([{
name: data.project_name,
path: '/project/' + data.project_id
}])
});
2017-07-31 11:16:36 +08:00
// '添加接口'页面根据project_id获取面包屑路径并在结尾追加"添加接口"
if (pathSnippets.includes('add-interface') && !pathSnippets.includes('edit')) {
this.setState({
breadcrumb: this.state.breadcrumb.concat([{
name: '添加接口',
path: '/add-interface/' + data.project_id
}])
});
}
2017-07-30 15:06:03 +08:00
}
2017-07-31 11:16:36 +08:00
if (data.interface_name && pathSnippets.includes('edit')) {
2017-07-30 15:06:03 +08:00
this.setState({
breadcrumb: this.state.breadcrumb.concat([{
name: data.interface_name,
2017-07-31 11:16:36 +08:00
path: '/add-interface/edit/' + data.interface_id
2017-07-30 15:06:03 +08:00
}])
});
}
2017-07-28 21:16:47 +08:00
});
2017-07-30 15:06:03 +08:00
} else if (pathSnippets[0] == 'user') {
this.setState({
breadcrumb: [{
name: '个人中心',
path: '/' + pathSnippets.join('/')
}]
});
2017-07-28 21:16:47 +08:00
}
}
componentDidMount() {
2017-07-30 15:06:03 +08:00
const pathSnippets = location.hash.split('#')[1].split('/').filter(i => i);
this.getBreadcrumb(pathSnippets);
2017-07-28 21:16:47 +08:00
this.setState({
2017-07-30 15:06:03 +08:00
hash: location.hash.split('#')[1]
2017-07-28 21:16:47 +08:00
})
}
2017-07-30 15:06:03 +08:00
componentWillReceiveProps(nextProps) {
// this.setState({
// hash: nextProps.location.pathname
// })
const pathSnippets = location.hash.split('#')[1].split('/').filter(i => i);
// console.log(nextProps.location.pathname, this.props.location.pathname);
if (nextProps.location.pathname !== this.props.location.pathname) {
// console.log('in');
this.getBreadcrumb(pathSnippets);
this.setState({
hash: nextProps.location.pathname
})
2017-07-28 21:16:47 +08:00
}
2017-07-28 17:30:56 +08:00
}
render () {
2017-07-30 15:06:03 +08:00
// console.log(this.state.hash);
2017-07-28 17:30:56 +08:00
const pathSnippets = location.hash.split('#')[1].split('/').filter(i => i);
2017-07-30 15:06:03 +08:00
// 获取接口路径并分割
// console.log(this.state);
const extraBreadcrumbItems = this.state.breadcrumb.map((item) => {
2017-07-28 21:16:47 +08:00
// const url = `/${pathSnippets.slice(0, index + 1).join('/')}`;
2017-07-28 17:30:56 +08:00
return (
2017-07-28 21:16:47 +08:00
<Breadcrumb.Item key={item.path}>
<Link to={item.path}>
{item.name}
2017-07-28 17:30:56 +08:00
</Link>
</Breadcrumb.Item>
);
});
const breadcrumbItems = [(
<Breadcrumb.Item key="home">
2017-07-28 21:16:47 +08:00
<Link to="/">首页</Link>
2017-07-28 17:30:56 +08:00
</Breadcrumb.Item>
)].concat(extraBreadcrumbItems);
2017-07-28 17:42:19 +08:00
if (pathSnippets.length) {
return (
<Breadcrumb className="breadcrumb-container">
{breadcrumbItems}
</Breadcrumb>
)
} else {
return <span></span>;
}
2017-07-28 17:30:56 +08:00
}
}