OpenDroneMap-WebODM/app/static/app/js/components/ProjectList.jsx

95 wiersze
2.8 KiB
React
Czysty Zwykły widok Historia

import React from 'react';
import $ from 'jquery';
2016-11-26 15:27:54 +00:00
import '../css/ProjectList.scss';
import ProjectListItem from './ProjectListItem';
2016-11-18 20:15:29 +00:00
import Paginated from './Paginated';
import Paginator from './Paginator';
2016-11-26 15:27:54 +00:00
import ErrorMessage from './ErrorMessage';
2016-11-18 20:15:29 +00:00
class ProjectList extends Paginated {
constructor(){
super();
this.state = {
loading: true,
2016-11-26 15:27:54 +00:00
refreshing: false,
error: "",
2016-11-26 15:27:54 +00:00
projects: []
}
2016-11-15 16:51:19 +00:00
2016-11-26 15:27:54 +00:00
this.PROJECTS_PER_PAGE = 10;
2016-11-15 16:51:19 +00:00
this.handleDelete = this.handleDelete.bind(this);
}
componentDidMount(){
this.refresh();
}
refresh(){
2016-11-26 15:27:54 +00:00
this.setState({refreshing: true});
// Load projects from API
this.serverRequest =
2016-11-26 15:27:54 +00:00
$.getJSON(this.getPaginatedUrl(this.props.source), json => {
if (json.results){
this.setState({
projects: json.results,
loading: false
});
2016-11-26 15:27:54 +00:00
this.updatePagination(this.PROJECTS_PER_PAGE, json.count);
}else{
this.setState({
error: `Invalid JSON response: ${JSON.stringify(json)}`,
loading: false
});
}
})
.fail((jqXHR, textStatus, errorThrown) => {
this.setState({
error: `Could not load projects list: ${textStatus}`,
loading: false
});
2016-11-26 15:27:54 +00:00
})
.always(() => {
this.setState({refreshing: false});
});
2016-11-26 15:27:54 +00:00
}
2016-11-26 15:27:54 +00:00
onPageChanged(pageNum){
this.refresh();
}
componentWillUnmount(){
this.serverRequest.abort();
}
2016-11-15 16:51:19 +00:00
handleDelete(projectId){
let projects = this.state.projects.filter(p => p.id !== projectId);
this.setState({projects: projects});
2016-11-26 15:27:54 +00:00
this.handlePageItemsNumChange(-1, () => {
this.refresh();
});
2016-11-15 16:51:19 +00:00
}
render() {
if (this.state.loading){
2016-11-26 15:27:54 +00:00
return (<div className="project-list">Loading projects... <i className="fa fa-refresh fa-spin fa-fw"></i></div>);
}else{
2016-11-26 15:27:54 +00:00
return (<div className="project-list">
<ErrorMessage bind={[this, 'error']} />
<Paginator className="text-right" {...this.state.pagination} handlePageChange={this.handlePageChange.bind(this)}>
<ul className={"list-group project-list " + (this.state.refreshing ? "refreshing" : "")}>
{this.state.projects.map(p => (
<ProjectListItem key={p.id} data={p} onDelete={this.handleDelete} />
))}
</ul>
</Paginator>
</div>);
}
}
}
export default ProjectList;