forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiProgressBar.js
More file actions
107 lines (94 loc) · 2.47 KB
/
Copy pathApiProgressBar.js
File metadata and controls
107 lines (94 loc) · 2.47 KB
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
import React from 'react'
import _ from 'underscore'
import ProgressStore from 'jsx/shared/stores/ProgressStore'
import ProgressBar from 'jsx/shared/ProgressBar'
var ApiProgressBar = React.createClass({
displayName: 'ProgressBar',
propTypes: {
progress_id: React.PropTypes.string,
onComplete: React.PropTypes.func,
delay: React.PropTypes.number
},
intervalID: null,
//
// Preparation
//
getDefaultProps(){
return {
delay: 1000
}
},
getInitialState () {
return {
completion: 0,
workflow_state: null
}
},
//
// Lifecycle
//
componentDidMount () {
ProgressStore.addChangeListener(this.handleStoreChange);
this.intervalID = setInterval(this.poll, this.props.delay);
},
componentWillUnmount () {
ProgressStore.removeChangeListener(this.handleStoreChange);
if (!_.isNull(this.intervalID)) {
clearInterval(this.intervalID);
this.intervalID = null;
};
},
shouldComponentUpdate (nextProps, nextState) {
return this.state.workflow_state != nextState.workflow_state ||
this.state.completion != nextState.completion ||
this.props.progress_id != nextProps.progress_id;
},
componentDidUpdate () {
if (this.isComplete()) {
if (!_.isNull(this.intervalID)) {
clearInterval(this.intervalID);
this.intervalID = null;
};
if (!_.isUndefined(this.props.onComplete)) {
this.props.onComplete();
};
};
},
//
// Custom Helpers
//
handleStoreChange () {
var progress = ProgressStore.getState()[this.props.progress_id];
if (_.isObject(progress)) {
this.setState({
completion: progress.completion,
workflow_state: progress.workflow_state
});
};
},
isComplete () {
return _.contains(['completed', 'failed'], this.state.workflow_state);
},
isInProgress () {
return _.contains(['queued', 'running'], this.state.workflow_state);
},
poll () {
if (!_.isUndefined(this.props.progress_id)) {
ProgressStore.get(this.props.progress_id);
}
},
//
// Render
//
render() {
if (!this.isInProgress()) {
return null;
};
return (
<div style={{width: '300px'}}>
<ProgressBar progress={this.state.completion} />
</div>
);
}
});
export default ApiProgressBar