forgeplus-react/src/forge/Main/Index.js

256 lines
7.6 KiB
JavaScript
Raw Normal View History

2020-05-07 10:10:13 +08:00
import React, { Component } from 'react';
2020-03-16 14:12:11 +08:00
import { Link } from 'react-router-dom';
2020-05-11 14:18:32 +08:00
import { Menu, Input , Spin, Pagination , Popover } from 'antd';
2020-03-16 14:12:11 +08:00
import '../css/index.css'
import './list.css';
import ListItem from './IndexItem'
import axios from 'axios';
import img_new from '../Images/new.png';
import img_array from '../Images/array.png';
const Search = Input.Search;
2020-05-07 10:10:13 +08:00
class Index extends Component {
constructor(props) {
2020-03-16 14:12:11 +08:00
super(props);
2020-05-07 10:10:13 +08:00
this.state = {
projectsList: undefined,
page: 1,
limit: 15,
search: undefined,
sort: undefined,
total: 0,
isSpin: true,
project_type: undefined,
category_id: undefined,
2020-03-16 14:12:11 +08:00
2020-05-07 10:10:13 +08:00
typeList: undefined,
categoryList: undefined
2020-03-16 14:12:11 +08:00
}
}
2020-05-07 10:10:13 +08:00
componentDidMount = () => {
const { page, limit, search, sort, project_type, category_id } = this.state;
this.getListData(page, limit, search, sort, project_type, category_id);
2020-03-16 14:12:11 +08:00
this.getType();
this.getCategory();
}
// 获取列表
2020-05-07 10:10:13 +08:00
getListData = (page, limit, search, sort, project_type, category_id) => {
2020-03-16 14:12:11 +08:00
const { current_user } = this.props;
const url = `/projects.json`;
2020-05-07 10:10:13 +08:00
axios.get(url, {
params: {
user_id: current_user && current_user.user_id,
page,
limit,
search,
sort_by: sort,
project_type,
category_id
}
}).then((result) => {
if (result) {
2020-03-16 14:12:11 +08:00
this.setState({
2020-05-07 10:10:13 +08:00
projectsList: result.data.projects,
total: result.data.total_count,
isSpin: false
2020-03-16 14:12:11 +08:00
})
}
2020-05-07 10:10:13 +08:00
}).catch((error) => { })
2020-03-16 14:12:11 +08:00
}
// 获取类型
2020-05-07 10:10:13 +08:00
getType = () => {
2020-03-16 14:12:11 +08:00
const url = `/projects/group_type_list.json`;
2020-05-07 10:10:13 +08:00
axios.get(url).then((result) => {
if (result && result.data) {
2020-03-16 14:12:11 +08:00
this.setTypeList(result.data, undefined)
}
2020-05-07 10:10:13 +08:00
}).catch((error) => { })
2020-03-16 14:12:11 +08:00
}
2020-05-07 10:10:13 +08:00
setTypeList = (list, active_type) => {
2020-03-16 14:12:11 +08:00
this.setState({
2020-05-07 10:10:13 +08:00
typeList: list.map((item, key) => {
return (
<li key={key} className={active_type && active_type === item.project_type ? 'active' : ''} onClick={() => this.changeType(`${item.project_type}`, list)}>
2020-03-16 14:12:11 +08:00
<p>
<span className="font-16">{item.name}</span>
<span className="color-blue">{item.projects_count}</span>
</p>
</li>
)
})
})
}
// 切换类型
2020-05-07 10:10:13 +08:00
changeType = (type, list) => {
2020-03-16 14:12:11 +08:00
this.setState({
2020-05-07 10:10:13 +08:00
isSpin: true,
project_type: type,
search: undefined
2020-03-16 14:12:11 +08:00
})
2020-05-07 10:10:13 +08:00
this.setTypeList(list, type)
const { page, limit, sort, category_id } = this.state;
this.getListData(page, limit, undefined, sort, type, category_id);
2020-03-16 14:12:11 +08:00
}
// 获取类型
2020-05-07 10:10:13 +08:00
getCategory = () => {
2020-03-16 14:12:11 +08:00
const url = `/project_categories/group_list.json`;
2020-05-07 10:10:13 +08:00
axios.get(url).then((result) => {
if (result && result.data) {
2020-03-16 14:12:11 +08:00
this.setCategoryList(result.data, undefined);
}
2020-05-07 10:10:13 +08:00
}).catch((error) => { })
2020-03-16 14:12:11 +08:00
}
2020-05-07 10:10:13 +08:00
setCategoryList = (list, active_id) => {
2020-03-16 14:12:11 +08:00
this.setState({
2020-05-07 10:10:13 +08:00
categoryList: list.map((item, key) => {
return (
<li key={key} className={active_id && parseInt(active_id) === item.id ? 'active' : ''} onClick={() => this.changeCategory(`${item.id}`, list)}>
2020-03-16 14:12:11 +08:00
<p>
<span className="font-16">{item.name}</span>
<span className="color-blue">{item.projects_count}</span>
</p>
</li>
)
})
})
}
2020-05-07 10:10:13 +08:00
changeCategory = (id, list) => {
2020-03-16 14:12:11 +08:00
this.setState({
2020-05-07 10:10:13 +08:00
category_id: id,
page: 1
2020-03-16 14:12:11 +08:00
});
2020-05-07 10:10:13 +08:00
this.setCategoryList(list, id)
const { limit, sort, project_type } = this.state;
this.getListData(1, limit, undefined, sort, project_type, id);
2020-03-16 14:12:11 +08:00
}
// 排序
2020-05-07 10:10:13 +08:00
ChangeSoryBy = (e) => {
2020-03-16 14:12:11 +08:00
this.setState({
2020-05-07 10:10:13 +08:00
sort_by: e.key,
page: 1,
search: undefined,
isSpin: true
2020-03-16 14:12:11 +08:00
})
2020-05-07 10:10:13 +08:00
const { limit, project_type, category_id } = this.state;
this.getListData(1, limit, undefined, e.key, project_type, category_id);
2020-03-16 14:12:11 +08:00
}
// 搜索
2020-05-07 10:10:13 +08:00
searchFun = (value) => {
2020-03-16 14:12:11 +08:00
this.setState({
2020-05-07 10:10:13 +08:00
page: 1,
search: value,
isSpin: true,
project_type: undefined,
2020-04-10 19:30:00 +08:00
sort: "updated_on"
2020-03-16 14:12:11 +08:00
})
2020-05-07 10:10:13 +08:00
const { limit, sort, category_id } = this.state;
this.getListData(1, limit, value, sort, undefined, category_id);
2020-03-16 14:12:11 +08:00
}
2020-05-07 10:10:13 +08:00
changeSearchValue = (e) => {
2020-03-16 14:12:11 +08:00
this.setState({
2020-05-07 10:10:13 +08:00
search: e.target.value
2020-03-16 14:12:11 +08:00
})
}
// 翻页
2020-05-07 10:10:13 +08:00
ChangePage = (page) => {
2020-03-16 14:12:11 +08:00
this.setState({
page
})
2020-05-07 10:10:13 +08:00
const { limit, search, sort, project_type, category_id } = this.state;
this.getListData(page, limit, search, sort, project_type, category_id);
2020-03-16 14:12:11 +08:00
}
2020-05-07 10:10:13 +08:00
render() {
2020-03-24 15:40:22 +08:00
const { current_user } = this.props;
2020-05-07 10:10:13 +08:00
const menu = (
2020-03-16 14:12:11 +08:00
<Menu onClick={this.ChangeSoryBy}>
<Menu.Item key="updated_on">更新时间排序</Menu.Item>
<Menu.Item key="created_on">创建时间排序</Menu.Item>
<Menu.Item key="forked_count">fork数据排序</Menu.Item>
<Menu.Item key="praises_count">点赞数量排序</Menu.Item>
</Menu>
)
2020-05-07 10:10:13 +08:00
const newItem = (
2020-03-16 14:12:11 +08:00
<Menu>
<Menu.Item key="created_mirror"><Link to={`/projects/mirror/new`}>新建镜像项目</Link></Menu.Item>
<Menu.Item key="created_deposit"><Link to={`/projects/deposit/new`}>新建托管项目</Link></Menu.Item>
</Menu>
)
2020-05-07 10:10:13 +08:00
const { projectsList, isSpin, total, search, limit, page, typeList, categoryList } = this.state;
2020-03-16 14:12:11 +08:00
const pagination = (
total && total > limit ?
2020-05-07 10:10:13 +08:00
<div className="edu-txt-center pt30 mb30">
<Pagination simple defaultCurrent={page} total={total} pageSize={limit} onChange={this.ChangePage}></Pagination>
</div> : ""
2020-03-16 14:12:11 +08:00
)
2020-05-07 10:10:13 +08:00
return (
2020-05-20 18:11:32 +08:00
<div>
2020-03-16 14:12:11 +08:00
<p className="t_project_banner"></p>
2020-05-20 18:11:32 +08:00
<div className="ProjectListIndex">
2020-03-16 14:12:11 +08:00
<div className="list-left">
<ul className="list-l-Menu">
2020-05-11 14:18:32 +08:00
<li className="MenuTitle"><i className="iconfont icon-xiangmuleixing color-grey-9 font-15 mr5"></i></li>
2020-05-07 10:10:13 +08:00
{typeList}
2020-03-16 14:12:11 +08:00
</ul>
<ul className="list-l-Menu">
2020-05-11 14:18:32 +08:00
<li className="MenuTitle"><i className="iconfont icon-xiangmuleibie color-grey-9 font-15 mr5"></i></li>
2020-05-07 10:10:13 +08:00
{categoryList}
2020-03-16 14:12:11 +08:00
</ul>
</div>
2020-05-11 14:18:32 +08:00
<div className="list-right boxShandow radius-2" style={{padding:0}}>
2020-03-16 14:12:11 +08:00
<Spin spinning={isSpin}>
<div className="list-r-operation">
2020-05-11 14:18:32 +08:00
<Search
placeholder="输入项目名称关键字进行搜索"
enterButton="搜索"
size="large"
onSearch={this.searchFun}
className="list-r-Search"
value={search}
onChange={this.changeSearchValue}
/>
2020-03-16 14:12:11 +08:00
<div>
2020-03-24 15:40:22 +08:00
{
current_user && current_user.login &&
2020-05-11 14:18:32 +08:00
<Popover content={newItem} trigger={["click"]} placement='bottom' className="mr50">
2020-03-24 15:40:22 +08:00
<a className="ant-dropdown-link">
2020-05-07 10:10:13 +08:00
<span className="color-blue font-16"><img src={img_new} alt="" width="13px" /> 新建</span>
2020-03-24 15:40:22 +08:00
</a>
2020-05-11 14:18:32 +08:00
</Popover>
2020-03-24 15:40:22 +08:00
}
2020-05-07 10:10:13 +08:00
2020-05-11 14:18:32 +08:00
<Popover content={menu} trigger={['click']} placement='bottom'>
2020-03-16 14:12:11 +08:00
<a className="ant-dropdown-link">
2020-05-07 10:10:13 +08:00
<span className="color-blue font-16">排序 <img src={img_array} alt="" width="10px" /></span>
2020-03-16 14:12:11 +08:00
</a>
2020-05-11 14:18:32 +08:00
</Popover>
2020-03-16 14:12:11 +08:00
</div>
</div>
<ListItem {...this.props} {...this.state} projects={projectsList}></ListItem>
2020-05-07 10:10:13 +08:00
{pagination}
2020-03-16 14:12:11 +08:00
</Spin>
</div>
</div>
</div>
)
}
}
export default Index;