2016-10-11 17:42:17 +00:00
|
|
|
import React from 'react';
|
|
|
|
import $ from 'jquery';
|
2016-11-26 15:27:54 +00:00
|
|
|
import '../css/ProjectList.scss';
|
2016-10-11 17:42:17 +00:00
|
|
|
|
|
|
|
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';
|
2020-12-15 20:56:00 +00:00
|
|
|
import { _, interpolate } from '../classes/gettext';
|
2017-09-06 15:47:04 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2016-10-11 17:42:17 +00:00
|
|
|
|
2016-11-18 20:15:29 +00:00
|
|
|
class ProjectList extends Paginated {
|
2017-09-06 15:47:04 +00:00
|
|
|
static propTypes = {
|
|
|
|
history: PropTypes.object.isRequired,
|
|
|
|
}
|
|
|
|
|
2017-04-12 15:52:40 +00:00
|
|
|
constructor(props){
|
|
|
|
super(props);
|
2016-10-11 17:42:17 +00:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
loading: true,
|
2016-11-26 15:27:54 +00:00
|
|
|
refreshing: false,
|
2016-10-11 17:42:17 +00:00
|
|
|
error: "",
|
2016-11-26 15:27:54 +00:00
|
|
|
projects: []
|
2016-10-11 17:42:17 +00:00
|
|
|
}
|
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);
|
2016-10-11 17:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount(){
|
2016-11-14 21:32:05 +00:00
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
2017-04-12 15:52:40 +00:00
|
|
|
componentDidUpdate(prevProps){
|
|
|
|
if (prevProps.source !== this.props.source){
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-14 21:32:05 +00:00
|
|
|
refresh(){
|
2016-11-26 15:27:54 +00:00
|
|
|
this.setState({refreshing: true});
|
|
|
|
|
2016-10-11 17:42:17 +00:00
|
|
|
// Load projects from API
|
2016-10-24 18:14:35 +00:00
|
|
|
this.serverRequest =
|
2017-04-12 15:52:40 +00:00
|
|
|
$.getJSON(this.props.source, json => {
|
2016-10-11 17:42:17 +00:00
|
|
|
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);
|
2016-10-11 17:42:17 +00:00
|
|
|
}else{
|
|
|
|
this.setState({
|
2020-12-15 20:56:00 +00:00
|
|
|
error: interpolate(_("Invalid JSON response: %(error)s"), {error: JSON.stringify(json)}),
|
2016-10-11 17:42:17 +00:00
|
|
|
loading: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.fail((jqXHR, textStatus, errorThrown) => {
|
|
|
|
this.setState({
|
2020-12-15 20:56:00 +00:00
|
|
|
error: interpolate(_("Could not load projects list: %(error)s"), {error: textStatus}),
|
2016-10-11 17:42:17 +00:00
|
|
|
loading: false
|
|
|
|
});
|
2016-11-26 15:27:54 +00:00
|
|
|
})
|
|
|
|
.always(() => {
|
|
|
|
this.setState({refreshing: false});
|
2016-10-12 22:18:37 +00:00
|
|
|
});
|
2016-11-26 15:27:54 +00:00
|
|
|
}
|
2016-11-14 21:32:05 +00:00
|
|
|
|
2016-11-26 15:27:54 +00:00
|
|
|
onPageChanged(pageNum){
|
|
|
|
this.refresh();
|
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-11-26 15:27:54 +00:00
|
|
|
this.handlePageItemsNumChange(-1, () => {
|
|
|
|
this.refresh();
|
|
|
|
});
|
2016-11-15 16:51:19 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:20:51 +00:00
|
|
|
handleTaskMoved = (task) => {
|
|
|
|
if (this["projectListItem_" + task.project]){
|
|
|
|
this["projectListItem_" + task.project].newTaskAdded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 17:42:17 +00:00
|
|
|
render() {
|
|
|
|
if (this.state.loading){
|
2020-12-14 21:53:25 +00:00
|
|
|
return (<div className="project-list text-center"><i className="fa fa-sync fa-spin fa-2x fa-fw"></i></div>);
|
2016-10-11 17:42:17 +00:00
|
|
|
}else{
|
2016-11-26 15:27:54 +00:00
|
|
|
return (<div className="project-list">
|
2017-04-12 15:52:40 +00:00
|
|
|
<ErrorMessage bind={[this, 'error']} />
|
|
|
|
<Paginator className="text-right" {...this.state.pagination} {...this.props}>
|
2016-11-26 15:27:54 +00:00
|
|
|
<ul className={"list-group project-list " + (this.state.refreshing ? "refreshing" : "")}>
|
|
|
|
{this.state.projects.map(p => (
|
2017-04-13 21:03:42 +00:00
|
|
|
<ProjectListItem
|
2021-08-04 20:20:51 +00:00
|
|
|
ref={(domNode) => { this["projectListItem_" + p.id] = domNode }}
|
2017-04-13 21:03:42 +00:00
|
|
|
key={p.id}
|
|
|
|
data={p}
|
2021-08-04 20:20:51 +00:00
|
|
|
onDelete={this.handleDelete}
|
|
|
|
onTaskMoved={this.handleTaskMoved}
|
2017-04-13 21:03:42 +00:00
|
|
|
history={this.props.history} />
|
2016-11-26 15:27:54 +00:00
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</Paginator>
|
|
|
|
</div>);
|
2016-10-11 17:42:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ProjectList;
|