-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.native.js
213 lines (189 loc) · 4.85 KB
/
index.native.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* External dependencies
*/
import { View } from 'react-native';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { Spinner } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { subscribeMediaUpload } from '@wordpress/react-native-bridge';
/**
* Internal dependencies
*/
import styles from './styles.scss';
import {
MEDIA_UPLOAD_STATE_IDLE,
MEDIA_UPLOAD_STATE_UPLOADING,
MEDIA_UPLOAD_STATE_SUCCEEDED,
MEDIA_UPLOAD_STATE_FAILED,
MEDIA_UPLOAD_STATE_RESET,
MEDIA_UPLOAD_STATE_PAUSED,
} from './constants';
export class MediaUploadProgress extends Component {
constructor( props ) {
super( props );
this.state = {
uploadState: MEDIA_UPLOAD_STATE_IDLE,
progress: 0,
isUploadInProgress: false,
isUploadFailed: false,
};
this.mediaUpload = this.mediaUpload.bind( this );
this.getRetryMessage = this.getRetryMessage.bind( this );
}
componentDidMount() {
this.addMediaUploadListener();
}
componentWillUnmount() {
this.removeMediaUploadListener();
}
mediaUpload( payload ) {
const { mediaId } = this.props;
if (
payload.mediaId !== mediaId ||
( payload.state === this.state.uploadState &&
payload.progress === this.state.progress )
) {
return;
}
switch ( payload.state ) {
case MEDIA_UPLOAD_STATE_UPLOADING:
this.updateMediaProgress( payload );
break;
case MEDIA_UPLOAD_STATE_SUCCEEDED:
this.finishMediaUploadWithSuccess( payload );
break;
case MEDIA_UPLOAD_STATE_PAUSED:
this.finishMediaUploadWithPause( payload );
break;
case MEDIA_UPLOAD_STATE_FAILED:
this.finishMediaUploadWithFailure( payload );
break;
case MEDIA_UPLOAD_STATE_RESET:
this.mediaUploadStateReset( payload );
break;
}
}
updateMediaProgress( payload ) {
this.setState( {
progress: payload.progress,
uploadState: payload.state,
isUploadInProgress: true,
isUploadFailed: false,
} );
if ( this.props.onUpdateMediaProgress ) {
this.props.onUpdateMediaProgress( payload );
}
}
finishMediaUploadWithSuccess( payload ) {
this.setState( {
uploadState: payload.state,
isUploadInProgress: false,
} );
if ( this.props.onFinishMediaUploadWithSuccess ) {
this.props.onFinishMediaUploadWithSuccess( payload );
}
}
finishMediaUploadWithPause( payload ) {
if ( ! this.props.enablePausedUploads ) {
this.finishMediaUploadWithFailure( payload );
return;
}
this.setState( {
uploadState: payload.state,
isUploadInProgress: true,
isUploadFailed: false,
} );
if ( this.props.onFinishMediaUploadWithFailure ) {
this.props.onFinishMediaUploadWithFailure( payload );
}
}
finishMediaUploadWithFailure( payload ) {
this.setState( {
uploadState: payload.state,
isUploadInProgress: false,
isUploadFailed: true,
} );
if ( this.props.onFinishMediaUploadWithFailure ) {
this.props.onFinishMediaUploadWithFailure( payload );
}
}
mediaUploadStateReset( payload ) {
this.setState( {
uploadState: payload.state,
isUploadInProgress: false,
isUploadFailed: false,
} );
if ( this.props.onMediaUploadStateReset ) {
this.props.onMediaUploadStateReset( payload );
}
}
addMediaUploadListener() {
// If we already have a subscription not worth doing it again.
if ( this.subscriptionParentMediaUpload ) {
return;
}
this.subscriptionParentMediaUpload = subscribeMediaUpload(
( payload ) => {
this.mediaUpload( payload );
}
);
}
removeMediaUploadListener() {
if ( this.subscriptionParentMediaUpload ) {
this.subscriptionParentMediaUpload.remove();
}
}
getRetryMessage() {
if (
this.state.uploadState === MEDIA_UPLOAD_STATE_PAUSED &&
this.props.enablePausedUploads
) {
return __( 'Waiting for connection' );
}
// eslint-disable-next-line @wordpress/i18n-no-collapsible-whitespace
return __( 'Failed to insert media.\nTap for more info.' );
}
render() {
const { renderContent = () => null } = this.props;
const { isUploadInProgress, isUploadFailed, uploadState } = this.state;
const showSpinner = this.state.isUploadInProgress;
const progress = this.state.progress * 100;
const retryMessage = this.getRetryMessage();
const progressBarStyle = [
styles.progressBar,
showSpinner || styles.progressBarHidden,
this.props.progressBarStyle,
];
return (
<View
style={ [
styles.mediaUploadProgress,
this.props.containerStyle,
] }
pointerEvents="box-none"
>
<View style={ progressBarStyle }>
{ showSpinner && (
<Spinner
progress={ progress }
style={ this.props.spinnerStyle }
testID="spinner"
/>
) }
</View>
{ renderContent( {
isUploadPaused:
uploadState === MEDIA_UPLOAD_STATE_PAUSED &&
this.props.enablePausedUploads,
isUploadInProgress,
isUploadFailed,
retryMessage,
} ) }
</View>
);
}
}
export default MediaUploadProgress;