2016-10-11 17:42:17 +00:00
|
|
|
import React from 'react';
|
|
|
|
import $ from 'jquery';
|
|
|
|
|
|
|
|
import ProjectListItem from './ProjectListItem';
|
|
|
|
|
|
|
|
class ProjectList extends React.Component {
|
|
|
|
constructor(){
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
loading: true,
|
|
|
|
error: "",
|
|
|
|
projects: null
|
|
|
|
}
|
2016-11-15 16:51:19 +00:00
|
|
|
|
|
|
|
this.handleDelete = this.handleDelete.bind(this);
|
2016-10-11 17:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount(){
|
2016-11-14 21:32:05 +00:00
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh(){
|
2016-10-11 17:42:17 +00:00
|
|
|
// Load projects from API
|
2016-10-24 18:14:35 +00:00
|
|
|
this.serverRequest =
|
|
|
|
$.getJSON(this.props.source, json => {
|
2016-10-11 17:42:17 +00:00
|
|
|
if (json.results){
|
|
|
|
this.setState({
|
|
|
|
projects: json.results,
|
|
|
|
loading: false
|
|
|
|
});
|
|
|
|
}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-10-12 22:18:37 +00:00
|
|
|
});
|
2016-11-14 21:32:05 +00:00
|
|
|
|
2016-10-11 17:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-10-11 17:42:17 +00:00
|
|
|
render() {
|
|
|
|
if (this.state.loading){
|
2016-10-19 18:00:03 +00:00
|
|
|
return (<div>Loading projects... <i className="fa fa-refresh fa-spin fa-fw"></i></div>);
|
2016-10-11 17:42:17 +00:00
|
|
|
}
|
|
|
|
else if (this.state.projects){
|
2016-10-11 20:37:00 +00:00
|
|
|
return (<ul className="list-group">
|
2016-10-11 17:42:17 +00:00
|
|
|
{this.state.projects.map(p => (
|
2016-11-15 16:51:19 +00:00
|
|
|
<ProjectListItem key={p.id} data={p} onDelete={this.handleDelete} />
|
2016-10-11 17:42:17 +00:00
|
|
|
))}
|
2016-10-11 20:37:00 +00:00
|
|
|
</ul>);
|
2016-10-11 17:42:17 +00:00
|
|
|
}else if (this.state.error){
|
|
|
|
return (<div>An error occurred: {this.state.error}</div>);
|
|
|
|
}else{
|
|
|
|
return (<div></div>); // should never happen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ProjectList;
|