2016-10-17 17:19:32 +00:00
|
|
|
import '../css/ProjectListItem.scss';
|
2016-10-11 17:42:17 +00:00
|
|
|
import React from 'react';
|
2016-11-28 20:18:53 +00:00
|
|
|
import update from 'immutability-helper';
|
2016-10-25 14:47:49 +00:00
|
|
|
import TaskList from './TaskList';
|
2016-10-18 15:25:14 +00:00
|
|
|
import EditTaskPanel from './EditTaskPanel';
|
2016-10-17 17:19:32 +00:00
|
|
|
import UploadProgressBar from './UploadProgressBar';
|
2016-11-14 15:58:00 +00:00
|
|
|
import ErrorMessage from './ErrorMessage';
|
2016-11-14 21:32:05 +00:00
|
|
|
import EditProjectDialog from './EditProjectDialog';
|
2016-10-12 22:18:37 +00:00
|
|
|
import Dropzone from '../vendor/dropzone';
|
2016-10-13 20:28:32 +00:00
|
|
|
import csrf from '../django/csrf';
|
2017-04-13 21:03:42 +00:00
|
|
|
import HistoryNav from '../classes/HistoryNav';
|
2016-10-12 22:18:37 +00:00
|
|
|
import $ from 'jquery';
|
2016-10-11 17:42:17 +00:00
|
|
|
|
|
|
|
class ProjectListItem extends React.Component {
|
2016-10-11 20:37:00 +00:00
|
|
|
constructor(props){
|
|
|
|
super(props);
|
|
|
|
|
2017-04-13 21:03:42 +00:00
|
|
|
this.historyNav = new HistoryNav(props.history);
|
|
|
|
|
2016-10-11 17:42:17 +00:00
|
|
|
this.state = {
|
2017-04-13 21:03:42 +00:00
|
|
|
showTaskList: this.historyNav.isValueInQSList("project_task_open", props.data.id),
|
2016-10-22 15:23:37 +00:00
|
|
|
updatingTask: false,
|
2016-11-14 15:58:00 +00:00
|
|
|
upload: this.getDefaultUploadState(),
|
2016-11-14 21:32:05 +00:00
|
|
|
error: "",
|
2016-11-15 16:51:19 +00:00
|
|
|
data: props.data,
|
|
|
|
refreshing: false
|
2016-10-11 17:42:17 +00:00
|
|
|
};
|
|
|
|
|
2016-10-25 14:47:49 +00:00
|
|
|
this.toggleTaskList = this.toggleTaskList.bind(this);
|
2016-10-17 17:19:32 +00:00
|
|
|
this.handleUpload = this.handleUpload.bind(this);
|
2016-10-18 15:25:14 +00:00
|
|
|
this.closeUploadError = this.closeUploadError.bind(this);
|
|
|
|
this.cancelUpload = this.cancelUpload.bind(this);
|
2016-10-21 14:42:46 +00:00
|
|
|
this.handleTaskSaved = this.handleTaskSaved.bind(this);
|
2016-11-09 21:13:43 +00:00
|
|
|
this.viewMap = this.viewMap.bind(this);
|
2016-11-14 15:58:00 +00:00
|
|
|
this.handleDelete = this.handleDelete.bind(this);
|
2016-11-14 21:32:05 +00:00
|
|
|
this.handleEditProject = this.handleEditProject.bind(this);
|
|
|
|
this.updateProject = this.updateProject.bind(this);
|
|
|
|
this.taskDeleted = this.taskDeleted.bind(this);
|
2016-10-11 17:42:17 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 16:51:19 +00:00
|
|
|
refresh(){
|
|
|
|
// Update project information based on server
|
|
|
|
this.setState({refreshing: true});
|
|
|
|
|
|
|
|
this.refreshRequest =
|
|
|
|
$.getJSON(`/api/projects/${this.state.data.id}/`)
|
|
|
|
.done((json) => {
|
|
|
|
this.setState({data: json});
|
|
|
|
})
|
|
|
|
.fail((_, __, e) => {
|
|
|
|
this.setState({error: e.message});
|
|
|
|
})
|
|
|
|
.always(() => {
|
|
|
|
this.setState({refreshing: false});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-22 15:23:37 +00:00
|
|
|
componentWillUnmount(){
|
|
|
|
if (this.updateTaskRequest) this.updateTaskRequest.abort();
|
2016-11-14 15:58:00 +00:00
|
|
|
if (this.deleteProjectRequest) this.deleteProjectRequest.abort();
|
2016-11-15 16:51:19 +00:00
|
|
|
if (this.refreshRequest) this.refreshRequest.abort();
|
2016-10-22 15:23:37 +00:00
|
|
|
}
|
|
|
|
|
2016-10-17 17:19:32 +00:00
|
|
|
getDefaultUploadState(){
|
|
|
|
return {
|
|
|
|
uploading: false,
|
2016-10-18 15:25:14 +00:00
|
|
|
showEditTask: false,
|
|
|
|
error: "",
|
2016-10-17 17:19:32 +00:00
|
|
|
progress: 0,
|
|
|
|
totalCount: 0,
|
|
|
|
totalBytes: 0,
|
2016-10-21 14:42:46 +00:00
|
|
|
totalBytesSent: 0,
|
2016-10-22 15:23:37 +00:00
|
|
|
savedTaskInfo: false,
|
2016-10-21 14:42:46 +00:00
|
|
|
taskId: null
|
2016-10-17 17:19:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resetUploadState(){
|
|
|
|
this.setUploadState(this.getDefaultUploadState());
|
|
|
|
}
|
|
|
|
|
|
|
|
setUploadState(props){
|
|
|
|
this.setState(update(this.state, {
|
|
|
|
upload: {
|
|
|
|
$merge: props
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount(){
|
|
|
|
Dropzone.autoDiscover = false;
|
2016-10-12 22:18:37 +00:00
|
|
|
|
2016-10-18 15:25:14 +00:00
|
|
|
this.dz = new Dropzone(this.dropzone, {
|
2016-10-17 17:19:32 +00:00
|
|
|
paramName: "images",
|
2016-11-15 16:51:19 +00:00
|
|
|
url : `/api/projects/${this.state.data.id}/tasks/`,
|
2016-10-17 17:19:32 +00:00
|
|
|
parallelUploads: 9999999,
|
|
|
|
uploadMultiple: true,
|
2017-05-15 19:54:47 +00:00
|
|
|
acceptedFiles: "image/*, .txt",
|
2016-10-17 17:19:32 +00:00
|
|
|
autoProcessQueue: true,
|
|
|
|
createImageThumbnails: false,
|
|
|
|
clickable: this.uploadButton,
|
|
|
|
|
|
|
|
headers: {
|
|
|
|
[csrf.header]: csrf.token
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-10-18 15:25:14 +00:00
|
|
|
this.dz.on("totaluploadprogress", (progress, totalBytes, totalBytesSent) => {
|
2016-11-04 18:19:18 +00:00
|
|
|
this.setUploadState({
|
|
|
|
progress, totalBytes, totalBytesSent
|
|
|
|
});
|
2016-10-17 17:19:32 +00:00
|
|
|
})
|
|
|
|
.on("addedfile", () => {
|
|
|
|
this.setUploadState({
|
|
|
|
totalCount: this.state.upload.totalCount + 1
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.on("processingmultiple", () => {
|
|
|
|
this.setUploadState({
|
2016-10-18 15:25:14 +00:00
|
|
|
uploading: true,
|
|
|
|
showEditTask: true
|
2016-10-17 17:19:32 +00:00
|
|
|
})
|
|
|
|
})
|
2016-10-18 15:25:14 +00:00
|
|
|
.on("completemultiple", (files) => {
|
|
|
|
// Check
|
2016-10-21 14:42:46 +00:00
|
|
|
let success = files.length > 0 && files.filter(file => file.status !== "success").length === 0;
|
2016-10-18 15:25:14 +00:00
|
|
|
|
2016-10-21 14:42:46 +00:00
|
|
|
// All files have uploaded!
|
2016-10-18 15:25:14 +00:00
|
|
|
if (success){
|
|
|
|
this.setUploadState({uploading: false});
|
2016-10-21 14:42:46 +00:00
|
|
|
|
2017-02-10 17:40:09 +00:00
|
|
|
try{
|
2016-10-21 14:42:46 +00:00
|
|
|
let response = JSON.parse(files[0].xhr.response);
|
|
|
|
if (!response.id) throw new Error(`Expected id field, but none given (${response})`);
|
|
|
|
|
|
|
|
let taskId = response.id;
|
|
|
|
this.setUploadState({taskId});
|
|
|
|
|
|
|
|
// Update task information (if the user has completed this step)
|
2016-10-22 15:23:37 +00:00
|
|
|
if (this.state.upload.savedTaskInfo){
|
|
|
|
this.updateTaskInfo(taskId, this.editTaskPanel.getTaskInfo());
|
2016-10-21 14:42:46 +00:00
|
|
|
}else{
|
|
|
|
// Need to wait for user to confirm task options
|
|
|
|
}
|
2017-02-10 17:40:09 +00:00
|
|
|
}catch(e){
|
|
|
|
this.setUploadState({error: `Invalid response from server: ${e.message}`})
|
|
|
|
}
|
2016-10-21 14:42:46 +00:00
|
|
|
|
2016-10-18 15:25:14 +00:00
|
|
|
}else{
|
|
|
|
this.setUploadState({
|
|
|
|
uploading: false,
|
2016-10-21 14:42:46 +00:00
|
|
|
error: "Could not upload all files. An error occured. Please try again."
|
2016-10-18 15:25:14 +00:00
|
|
|
});
|
|
|
|
}
|
2016-10-17 17:19:32 +00:00
|
|
|
})
|
|
|
|
.on("reset", () => {
|
|
|
|
this.resetUploadState();
|
2016-11-11 18:30:11 +00:00
|
|
|
})
|
|
|
|
.on("dragenter", () => {
|
|
|
|
this.resetUploadState();
|
2017-02-15 19:20:41 +00:00
|
|
|
})
|
|
|
|
.on("sending", (file, xhr, formData) => {
|
|
|
|
formData.append('auto_processing_node', "false");
|
2016-10-12 22:18:37 +00:00
|
|
|
});
|
2016-10-17 17:19:32 +00:00
|
|
|
}
|
2016-10-12 22:18:37 +00:00
|
|
|
|
2016-10-22 15:23:37 +00:00
|
|
|
updateTaskInfo(taskId, taskInfo){
|
|
|
|
if (!taskId) throw new Error("taskId is not set");
|
|
|
|
if (!taskInfo) throw new Error("taskId is not set");
|
|
|
|
|
|
|
|
this.setUploadState({showEditTask: false});
|
|
|
|
this.setState({updatingTask: true});
|
|
|
|
|
2016-10-24 18:14:35 +00:00
|
|
|
this.updateTaskRequest =
|
|
|
|
$.ajax({
|
2016-11-15 16:51:19 +00:00
|
|
|
url: `/api/projects/${this.state.data.id}/tasks/${this.state.upload.taskId}/`,
|
2016-10-22 15:23:37 +00:00
|
|
|
contentType: 'application/json',
|
|
|
|
data: JSON.stringify({
|
|
|
|
name: taskInfo.name,
|
|
|
|
options: taskInfo.options,
|
2017-02-15 19:20:41 +00:00
|
|
|
processing_node: taskInfo.selectedNode.id,
|
|
|
|
auto_processing_node: taskInfo.selectedNode.key == "auto"
|
2016-10-22 15:23:37 +00:00
|
|
|
}),
|
|
|
|
dataType: 'json',
|
|
|
|
type: 'PATCH'
|
2016-11-15 16:51:19 +00:00
|
|
|
}).done((json) => {
|
2016-10-25 14:47:49 +00:00
|
|
|
if (this.state.showTaskList){
|
|
|
|
this.taskList.refresh();
|
2016-10-22 15:23:37 +00:00
|
|
|
}else{
|
2016-10-25 14:47:49 +00:00
|
|
|
this.setState({showTaskList: true});
|
2016-10-22 15:23:37 +00:00
|
|
|
}
|
2016-11-15 16:51:19 +00:00
|
|
|
this.refresh();
|
2016-10-22 15:23:37 +00:00
|
|
|
}).fail(() => {
|
|
|
|
this.setUploadState({error: "Could not update task information. Plese try again."});
|
|
|
|
}).always(() => {
|
|
|
|
this.setState({updatingTask: false});
|
|
|
|
});
|
2016-10-21 14:42:46 +00:00
|
|
|
}
|
|
|
|
|
2016-10-17 17:19:32 +00:00
|
|
|
setRef(prop){
|
|
|
|
return (domNode) => {
|
|
|
|
if (domNode != null) this[prop] = domNode;
|
2016-10-12 22:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 14:47:49 +00:00
|
|
|
toggleTaskList(){
|
2017-04-13 21:03:42 +00:00
|
|
|
const showTaskList = !this.state.showTaskList;
|
|
|
|
|
|
|
|
this.historyNav.toggleQSListItem("project_task_open", this.state.data.id, showTaskList);
|
|
|
|
|
2016-10-11 20:37:00 +00:00
|
|
|
this.setState({
|
2017-04-13 21:03:42 +00:00
|
|
|
showTaskList: showTaskList
|
2016-10-11 20:37:00 +00:00
|
|
|
});
|
2016-10-11 17:42:17 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 15:25:14 +00:00
|
|
|
closeUploadError(){
|
|
|
|
this.setUploadState({error: ""});
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelUpload(e){
|
|
|
|
this.dz.removeAllFiles(true);
|
|
|
|
}
|
|
|
|
|
2016-10-17 17:19:32 +00:00
|
|
|
handleUpload(){
|
|
|
|
this.resetUploadState();
|
|
|
|
}
|
|
|
|
|
2016-11-14 21:32:05 +00:00
|
|
|
taskDeleted(){
|
2016-11-15 16:51:19 +00:00
|
|
|
this.refresh();
|
2016-11-14 21:32:05 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 15:58:00 +00:00
|
|
|
handleDelete(){
|
2016-11-15 16:51:19 +00:00
|
|
|
return $.ajax({
|
|
|
|
url: `/api/projects/${this.state.data.id}/`,
|
|
|
|
type: 'DELETE'
|
|
|
|
}).done(() => {
|
|
|
|
if (this.props.onDelete) this.props.onDelete(this.state.data.id);
|
|
|
|
});
|
2016-11-14 15:58:00 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 14:42:46 +00:00
|
|
|
handleTaskSaved(taskInfo){
|
2016-10-22 15:23:37 +00:00
|
|
|
this.setUploadState({savedTaskInfo: true});
|
2016-10-18 15:25:14 +00:00
|
|
|
|
2016-10-21 14:42:46 +00:00
|
|
|
// Has the upload finished?
|
|
|
|
if (!this.state.upload.uploading && this.state.upload.taskId !== null){
|
2016-10-22 15:23:37 +00:00
|
|
|
this.updateTaskInfo(this.state.upload.taskId, taskInfo);
|
2016-10-21 14:42:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-14 21:32:05 +00:00
|
|
|
handleEditProject(){
|
|
|
|
this.editProjectDialog.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateProject(project){
|
2016-11-15 16:51:19 +00:00
|
|
|
return $.ajax({
|
|
|
|
url: `/api/projects/${this.state.data.id}/`,
|
|
|
|
contentType: 'application/json',
|
|
|
|
data: JSON.stringify({
|
|
|
|
name: project.name,
|
|
|
|
description: project.descr,
|
|
|
|
}),
|
|
|
|
dataType: 'json',
|
|
|
|
type: 'PATCH'
|
|
|
|
}).done(() => {
|
|
|
|
this.refresh();
|
|
|
|
});
|
2016-11-14 21:32:05 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 21:13:43 +00:00
|
|
|
viewMap(){
|
2016-11-15 16:51:19 +00:00
|
|
|
location.href = `/map/project/${this.state.data.id}/`;
|
2016-11-09 21:13:43 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 14:42:46 +00:00
|
|
|
render() {
|
2016-11-15 16:51:19 +00:00
|
|
|
const { refreshing, data } = this.state;
|
|
|
|
const numTasks = data.tasks.length;
|
|
|
|
|
2016-10-11 17:42:17 +00:00
|
|
|
return (
|
2016-11-15 16:51:19 +00:00
|
|
|
<li className={"project-list-item list-group-item " + (refreshing ? "refreshing" : "")}
|
2016-11-11 18:22:29 +00:00
|
|
|
href="javascript:void(0);"
|
2017-02-08 22:12:05 +00:00
|
|
|
ref={this.setRef("dropzone")}
|
|
|
|
>
|
2016-11-14 21:32:05 +00:00
|
|
|
|
|
|
|
<EditProjectDialog
|
|
|
|
ref={(domNode) => { this.editProjectDialog = domNode; }}
|
|
|
|
title="Edit Project"
|
|
|
|
saveLabel="Save Changes"
|
|
|
|
savingLabel="Saving changes..."
|
|
|
|
saveIcon="fa fa-edit"
|
2016-11-15 16:51:19 +00:00
|
|
|
projectName={data.name}
|
|
|
|
projectDescr={data.description}
|
2016-11-14 21:32:05 +00:00
|
|
|
saveAction={this.updateProject}
|
|
|
|
deleteAction={this.handleDelete}
|
|
|
|
/>
|
|
|
|
|
2016-10-18 15:25:14 +00:00
|
|
|
<div className="row no-margin">
|
2016-11-14 15:58:00 +00:00
|
|
|
<ErrorMessage bind={[this, 'error']} />
|
2016-10-18 15:25:14 +00:00
|
|
|
<div className="btn-group pull-right">
|
|
|
|
<button type="button"
|
|
|
|
className={"btn btn-primary btn-sm " + (this.state.upload.uploading ? "hide" : "")}
|
|
|
|
onClick={this.handleUpload}
|
|
|
|
ref={this.setRef("uploadButton")}>
|
|
|
|
<i className="glyphicon glyphicon-upload"></i>
|
2017-05-15 21:19:07 +00:00
|
|
|
Upload Images and GCP
|
2016-10-18 15:25:14 +00:00
|
|
|
</button>
|
|
|
|
|
|
|
|
<button disabled={this.state.upload.error !== ""}
|
|
|
|
type="button"
|
|
|
|
className={"btn btn-primary btn-sm " + (!this.state.upload.uploading ? "hide" : "")}
|
|
|
|
onClick={this.cancelUpload}>
|
|
|
|
<i className="glyphicon glyphicon-remove-circle"></i>
|
|
|
|
Cancel Upload
|
|
|
|
</button>
|
|
|
|
|
2016-11-09 21:13:43 +00:00
|
|
|
<button type="button" className="btn btn-default btn-sm" onClick={this.viewMap}>
|
|
|
|
<i className="fa fa-globe"></i> View Map
|
2016-10-18 15:25:14 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
2016-10-11 20:37:00 +00:00
|
|
|
|
2016-10-25 14:47:49 +00:00
|
|
|
<span className="project-name">
|
2016-11-15 16:51:19 +00:00
|
|
|
{data.name}
|
2016-10-25 14:47:49 +00:00
|
|
|
</span>
|
|
|
|
<div className="project-description">
|
2016-11-15 16:51:19 +00:00
|
|
|
{data.description}
|
2016-10-25 14:47:49 +00:00
|
|
|
</div>
|
|
|
|
<div className="row project-links">
|
2016-11-15 16:51:19 +00:00
|
|
|
{numTasks > 0 ?
|
2016-11-14 21:32:05 +00:00
|
|
|
<span>
|
|
|
|
<i className='fa fa-tasks'>
|
|
|
|
</i> <a href="javascript:void(0);" onClick={this.toggleTaskList}>
|
2016-11-15 16:51:19 +00:00
|
|
|
{numTasks} Tasks <i className={'fa fa-caret-' + (this.state.showTaskList ? 'down' : 'right')}></i>
|
2016-11-14 21:32:05 +00:00
|
|
|
</a>
|
|
|
|
</span>
|
|
|
|
: ""}
|
|
|
|
|
|
|
|
<i className='fa fa-edit'>
|
|
|
|
</i> <a href="javascript:void(0);" onClick={this.handleEditProject}> Edit
|
2016-10-25 14:47:49 +00:00
|
|
|
</a>
|
|
|
|
</div>
|
2016-10-18 15:25:14 +00:00
|
|
|
</div>
|
2016-11-11 18:22:29 +00:00
|
|
|
<i className="drag-drop-icon fa fa-inbox"></i>
|
2016-10-18 15:25:14 +00:00
|
|
|
<div className="row">
|
2016-10-21 14:42:46 +00:00
|
|
|
{this.state.upload.showEditTask ? <UploadProgressBar {...this.state.upload}/> : ""}
|
2016-10-18 15:25:14 +00:00
|
|
|
|
|
|
|
{this.state.upload.error !== "" ?
|
|
|
|
<div className="alert alert-warning alert-dismissible">
|
|
|
|
<button type="button" className="close" aria-label="Close" onClick={this.closeUploadError}><span aria-hidden="true">×</span></button>
|
|
|
|
{this.state.upload.error}
|
2016-10-12 22:18:37 +00:00
|
|
|
</div>
|
2016-10-18 15:25:14 +00:00
|
|
|
: ""}
|
2016-10-12 22:18:37 +00:00
|
|
|
|
2016-10-22 15:23:37 +00:00
|
|
|
{this.state.upload.showEditTask ?
|
|
|
|
<EditTaskPanel
|
|
|
|
uploading={this.state.upload.uploading}
|
|
|
|
onSave={this.handleTaskSaved}
|
|
|
|
ref={this.setRef("editTaskPanel")}
|
2016-10-21 14:42:46 +00:00
|
|
|
/>
|
2016-10-22 15:23:37 +00:00
|
|
|
: ""}
|
|
|
|
|
|
|
|
{this.state.updatingTask ?
|
|
|
|
<span>Updating task information... <i className="fa fa-refresh fa-spin fa-fw"></i></span>
|
|
|
|
: ""}
|
2016-10-17 17:19:32 +00:00
|
|
|
|
2016-11-14 21:32:05 +00:00
|
|
|
{this.state.showTaskList ?
|
|
|
|
<TaskList
|
|
|
|
ref={this.setRef("taskList")}
|
2016-11-15 16:51:19 +00:00
|
|
|
source={`/api/projects/${data.id}/tasks/?ordering=-created_at`}
|
2016-11-14 21:32:05 +00:00
|
|
|
onDelete={this.taskDeleted}
|
2017-04-13 21:03:42 +00:00
|
|
|
history={this.props.history}
|
2016-11-14 21:32:05 +00:00
|
|
|
/> : ""}
|
2016-11-02 22:32:24 +00:00
|
|
|
|
2016-10-18 15:25:14 +00:00
|
|
|
</div>
|
2016-10-11 20:37:00 +00:00
|
|
|
</li>
|
2016-10-11 17:42:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ProjectListItem;
|