yapi/client/components/Breadcrumb/Breadcrumb.js

123 lines
3.5 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: []
});
if (/project|group|add-interface]/.test(pathSnippets[0])) {
const type = pathSnippets[0] === 'add-interface' ? 'interface' : pathSnippets[0],
2017-07-28 21:16:47 +08:00
id = pathSnippets[1];
2017-07-30 15:06:03 +08:00
const params = { type, id };
axios.get('/user/nav', {params: params}).then( (res) => {
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
}])
});
}
if (data.interface_name) {
this.setState({
breadcrumb: this.state.breadcrumb.concat([{
name: data.interface_name,
path: '/project/' + data.interface_id
}])
});
}
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('/')
}]
});
} else {
console.log(2);
2017-07-28 21:16:47 +08:00
}
}
componentDidMount() {
2017-07-30 15:06:03 +08:00
console.log(location.hash);
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
}
}