zhongrj
2025-11-24 276323dce9613867abb3f58a4cc2abbfb2fd0dea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import React, { Component, Fragment } from "react";
import PropTypes from 'prop-types';
 
import ResizeModes from 'webodm/classes/ResizeModes';
 
import PlatformSelectButton from "./components/PlatformSelectButton";
import PlatformDialog from "./components/PlatformDialog";
import LibraryDialog from "./components/LibraryDialog";
import ErrorDialog from "./components/ErrorDialog";
import ConfigureNewTaskDialog from "./components/ConfigureNewTaskDialog";
 
export default class TaskView extends Component {
    static propTypes = {
        projectId: PropTypes.number.isRequired,
        apiURL: PropTypes.string.isRequired,
        onNewTaskAdded: PropTypes.func.isRequired,
  }
    
    state = {
        error: "",
        currentPlatform: null,
        selectedFolder: null,
        platforms: [],
    };
    
    componentDidMount() {
        $.getJSON(`${this.props.apiURL}/platforms/`)
                .done(data => {
                    this.setState({platforms: data.platforms});
                })
                .fail(() => {
                    this.onErrorInDialog("Failed to find available platforms")
                })
    }
 
    onSelectPlatform = platform => this.setState({ currentPlatform: platform });
    onSelectFolder = folder => this.setState({ selectedFolder: folder });
    onHideDialog = () => this.setState({ currentPlatform: null, selectedFolder: null, taskId: null });
 
    onSaveTask = taskInfo => {
        // Create task
        const formData = {
                name: taskInfo.name,
                options: taskInfo.options,
                processing_node:  taskInfo.selectedNode.id,
                auto_processing_node: taskInfo.selectedNode.key == "auto",
                partial: true
        };
 
        if (taskInfo.resizeMode === ResizeModes.YES){
                formData.resize_to = taskInfo.resizeSize;
        }
 
        $.ajax({
                url: `/api/projects/${this.props.projectId}/tasks/`,
                contentType: 'application/json',
                data: JSON.stringify(formData),
                dataType: 'json',
                type: 'POST'
            }).done((task) => {
                $.ajax({
                        url: `${this.props.apiURL}/projects/${this.props.projectId}/tasks/${task.id}/import`,
                        contentType: 'application/json',
                        data: JSON.stringify({platform: this.state.currentPlatform.name, selectedFolderUrl: this.state.selectedFolder.url}),
                        dataType: 'json',
                        type: 'POST'
                    }).done(() => {
                        this.onHideDialog();
                        this.props.onNewTaskAdded();
                    }).fail(error => {
                        this.onErrorInDialog("Failed to start importing.");
                    });
            }).fail(() => {
                this.onErrorInDialog("Cannot create new task. Please try again later.");
            });
    }
 
    onErrorInDialog = msg => {
        this.setState({ error: msg });
        this.onHideDialog();
    };
 
    render() {
        const {
            currentPlatform,
            error,
            selectedFolder,
            platforms,
        } = this.state;
        return (
            <Fragment>
            {error ?
                <ErrorDialog errorMessage={error} />
            : ""}
                <PlatformSelectButton
                    platforms={platforms}
                    onSelect={this.onSelectPlatform}
                />
                {selectedFolder === null ?
                    <Fragment>
                        <PlatformDialog
                            show={selectedFolder === null}
                            platform={currentPlatform}
                            apiURL={this.props.apiURL}
                            onHide={this.onHideDialog}
                            onSubmit={this.onSelectFolder}
                        />
                        <LibraryDialog
                          show={selectedFolder === null}
                          platform={currentPlatform}
                            apiURL={this.props.apiURL}
                          onHide={this.onHideDialog}
                          onSubmit={this.onSelectFolder}
                        />
                    </Fragment>
                : 
                    <ConfigureNewTaskDialog
                      show={selectedFolder !== null}
                        folder={selectedFolder}
                      platform={currentPlatform}
                      onHide={this.onHideDialog}
                      onSaveTask={this.onSaveTask}
                    />
                }
            </Fragment>
        );
    }
}