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

342 lines
10 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-10-05 18:01:56 +08:00
import { Menu, Input , Spin, Pagination , Popover , Select } from 'antd';
import { getUrl } from 'educoder';
2020-06-22 14:07:06 +08:00
import '../css/index.scss'
2020-03-16 14:12:11 +08:00
import './list.css';
2020-10-05 18:01:56 +08:00
import './Index.scss';
2020-03-16 14:12:11 +08:00
import ListItem from './IndexItem'
import axios from 'axios';
import img_new from '../Images/new.png';
import img_array from '../Images/array.png';
import banner from '../Images/banner_list.png';
2020-03-16 14:12:11 +08:00
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,
2020-09-11 18:31:31 +08:00
categoryList: undefined,
2020-10-05 18:01:56 +08:00
recommendList:undefined,
languageList:undefined,
languageId:undefined
2020-03-16 14:12:11 +08:00
}
}
2020-05-07 10:10:13 +08:00
componentDidMount = () => {
2020-10-05 18:01:56 +08:00
const { page, limit, search, sort, project_type, category_id , languageId } = this.state;
this.getListData(page, limit, search, sort, project_type, category_id , languageId);
2020-03-16 14:12:11 +08:00
this.getType();
this.getCategory();
2020-10-05 18:01:56 +08:00
this.getRecommand();
this.getLanguage();
}
// 获取语言列表
getLanguage=()=>{
const url = '/project_languages.json';
axios.get(url).then(result=>{
if(result){
this.setState({
languageList:result.data.project_languages
})
}
}).catch(error=>{})
}
// 获取推荐列表
getRecommand=()=>{
const url = `/projects/recommend.json`;
axios.get(url).then(result=>{
if(result){
this.setState({
recommendList:result.data
})
}
}).catch(error=>{})
2020-03-16 14:12:11 +08:00
}
// 获取列表
2020-10-05 18:01:56 +08:00
getListData = (page, limit, search, sort, project_type, category_id,languageId) => {
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,
2020-10-05 18:01:56 +08:00
category_id,
language_id:languageId
2020-05-07 10:10:13 +08:00
}
}).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)
2020-10-05 18:01:56 +08:00
const { page, limit, sort, category_id , languageId } = this.state;
this.getListData(page, limit, undefined, sort, type, category_id , languageId);
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)
2020-10-05 18:01:56 +08:00
const { limit, sort, project_type , languageId } = this.state;
this.getListData(1, limit, undefined, sort, project_type, id , languageId);
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-10-05 18:01:56 +08:00
const { limit, project_type, category_id , languageId } = this.state;
this.getListData(1, limit, undefined, e.key, project_type, category_id , languageId);
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-10-09 16:04:45 +08:00
const { limit, sort, category_id , languageId } = this.state;
this.getListData(1, limit, value, sort, undefined, category_id , languageId);
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-10-05 18:01:56 +08:00
const { limit, search, sort, project_type, category_id , languageId } = this.state;
this.getListData(page, limit, search, sort, project_type, category_id , languageId);
}
2020-03-16 14:12:11 +08:00
2020-10-05 18:01:56 +08:00
getoDetail=(login,identifier)=>{
2020-10-10 10:11:44 +08:00
this.props.history.push(`/projects/${login}/${identifier}`);
2020-10-05 18:01:56 +08:00
}
// 选择语言类别
changeLanguage=(e)=>{
this.setState({
isSpin:true,
languageId:e === 0 ?undefined:e
})
const { page, limit, sort , project_type , category_id } = this.state;
this.getListData(page, limit, undefined, sort, project_type, category_id ,e === 0 ?undefined:e);
2020-03-16 14:12:11 +08:00
}
2020-10-10 15:55:42 +08:00
menu =()=> {
return(
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-10-10 15:55:42 +08:00
}
newItem = ()=>{
return(
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-10-10 15:55:42 +08:00
}
2020-03-16 14:12:11 +08:00
2020-10-10 15:55:42 +08:00
pagination=(total,limit,page)=>{
return(
2020-12-23 17:39:50 +08:00
total && total > limit ?
2020-10-10 15:55:42 +08:00
<div className="edu-txt-center pt30 mb30 border-top-grey">
<Pagination simple defaultCurrent={page} total={total} pageSize={limit} onChange={this.ChangePage}></Pagination>
2020-12-23 17:39:50 +08:00
</div>:""
2020-10-10 15:55:42 +08:00
)
}
render() {
const { current_user } = this.props;
2020-03-16 14:12:11 +08:00
2020-10-05 18:01:56 +08:00
const { projectsList , recommendList , languageList , languageId ,
isSpin, total, search, limit, page, typeList, categoryList } = this.state;
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-09-16 18:27:51 +08:00
<p className="t_project_banner">
<img src={banner} width="100%" alt=""/>
</p>
{/* {
2020-10-05 18:01:56 +08:00
recommendList && recommendList.length>0 &&
<div className="recommandProjects">
{
recommendList.map((item,key)=>{
return(
<div onClick={()=>this.getoDetail(item.author && item.author.login,item.identifier)}>
<div className="mainInfo">
<img src={getUrl(`/images/${item.author && item.author.image_url}`)} alt=""/>
<p className="school">{item.name}</p>
2020-10-10 10:11:44 +08:00
<p className="name">{item.author && item.author.name}</p>
2020-10-05 18:01:56 +08:00
</div>
<div className="baseInfo">
<span className="look"><i className="iconfont icon-dianjiliang font-12"></i>{item.visits}</span>
2020-12-01 16:49:24 +08:00
<span className="type">{item.category && item.category.name}</span>
2020-10-05 18:01:56 +08:00
</div>
</div>
)
})
}
</div>
}
*/}
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">
<li className="MenuTitle"><i className="iconfont icon-bianchengyuyan color-grey-9 font-15 mr5"></i>
语言</li>
<div className="list-affix">{typeList}</div>
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>
<div className="list-affix">{categoryList}</div>
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">
<div>
2020-10-05 18:01:56 +08:00
<Search
placeholder="输入项目名称关键字进行搜索"
enterButton="搜索"
size="large"
onSearch={this.searchFun}
className="list-r-Search"
value={search}
onChange={this.changeSearchValue}
/>
</div>
2020-03-16 14:12:11 +08:00
<div>
2020-03-24 15:40:22 +08:00
{
current_user && current_user.login &&
2020-10-10 15:55:42 +08:00
<Popover content={this.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-10-10 15:55:42 +08:00
<Popover content={this.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-10-10 15:55:42 +08:00
{this.pagination(total,limit,page)}
2020-03-16 14:12:11 +08:00
</Spin>
</div>
</div>
</div>
)
}
}
export default Index;