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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import React from 'react';
import PropTypes from 'prop-types';
import ShareDialog from './ShareDialog';
import Storage from 'webodm/classes/Storage';
import ErrorMessage from 'webodm/components/ErrorMessage';
import $ from 'jquery';
 
export default class ShareButton extends React.Component{
    static defaultProps = {
        task: null,
        token: ""
    };
 
    static propTypes = {
        task: PropTypes.object.isRequired,
        token: PropTypes.string.isRequired // OAM Token
    };
 
    constructor(props){
        super(props);
 
        this.state = {
            loading: true,
            taskInfo: {},
            error: ''
        };
    }
 
    componentDidMount(){
        this.updateTaskInfo(false);
    }
 
    updateTaskInfo = (showErrors) => {
        const { task } = this.props;
        return $.ajax({
                type: 'GET',
                url: `/api/plugins/openaerialmap/task/${task.id}/info`,
                contentType: 'application/json'
            }).done(taskInfo => {
                // Allow a user to specify a better name for the sensor
                // and remember it.
                let sensor = Storage.getItem("oam_sensor_pref_" + taskInfo.sensor);
                if (sensor) taskInfo.sensor = sensor;
 
                // Allow a user to change the default provider name
                let provider = Storage.getItem("oam_provider_pref");
                if (provider) taskInfo.provider = provider;
 
                this.setState({taskInfo, loading: false});
                if (taskInfo.error && showErrors) this.setState({error: taskInfo.error});
            }).fail(error => {
                this.setState({error: error.statusText, loading: false});
            });
    }
 
    componentWillUnmount(){
        if (this.monitorTimeout) clearTimeout(this.monitorTimeout);
    }
 
    handleClick = () => {
        const { taskInfo } = this.state;
        if (!taskInfo.shared){
            this.shareDialog.show();
        }else if (taskInfo.oam_upload_id){
            window.open(`https://map.openaerialmap.org/#/upload/status/${encodeURIComponent(taskInfo.oam_upload_id)}`, '_blank');
        }
    }
 
    shareToOAM = (formData) => {
        const { task } = this.props;
 
        const oamParams = {
          token: this.props.token,
          sensor: formData.sensor,
          acquisition_start: formData.startDate,
          acquisition_end: formData.endDate,
          title: formData.title,
          provider: formData.provider,
          tags: formData.tags
        };
 
        return $.ajax({
            url: `/api/plugins/openaerialmap/task/${task.id}/share`,
            contentType: 'application/json',
            data: JSON.stringify({
                oamParams: oamParams
            }),
            dataType: 'json',
            type: 'POST'
          }).done(taskInfo => {
            // Allow a user to associate the sensor name coming from the EXIF tags
            // to one that perhaps is more human readable.
            Storage.setItem("oam_sensor_pref_" + taskInfo.sensor, formData.sensor);
            Storage.setItem("oam_provider_pref", formData.provider);
 
            this.setState({taskInfo});
            this.monitorProgress();
          });
    }
 
    monitorProgress = () => {
        if (this.state.taskInfo.sharing){
            // Monitor progress
            this.monitorTimeout = setTimeout(() => {
                this.updateTaskInfo(true).always(this.monitorProgress);
            }, 5000);
        }
    }
 
    render(){
        const { loading, taskInfo, error } = this.state;
        if (taskInfo.noImages) return (<div/>);
 
        const getButtonIcon = () => {
            if (loading || taskInfo.sharing) return "fa fa-circle-notch fa-spin fa-fw";
            else if (error) return "fa fa-exclamation-triangle";
            else return "fa oam-icon";
        };
 
        const getButtonLabel = () => {
            if (loading) return "";
            else if (taskInfo.sharing) return " Sharing...";
            else if (taskInfo.shared) return " View In OAM";
            else if (error) return " OAM Plugin Error";
            else return " Share To OAM";
        }
 
        const result = [
                <ErrorMessage bind={[this, "error"]} />,
                <button
                onClick={this.handleClick}
                disabled={loading || taskInfo.sharing || error}
                className="btn btn-sm btn-primary">
                    {[<i className={getButtonIcon()}></i>, getButtonLabel()]}
                </button>];
 
        if (taskInfo.sensor !== undefined){
            result.unshift(<ShareDialog
                  ref={(domNode) => { this.shareDialog = domNode; }}
                  task={this.props.task}
                  taskInfo={taskInfo}
                  saveAction={this.shareToOAM}
                />);
        }
 
        return result;
    }
}