-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.native.js
169 lines (147 loc) · 4.08 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
/**
* External dependencies
*/
import React from 'react';
import { View } from 'react-native';
import {
subscribeMediaUpload,
} from 'react-native-gutenberg-bridge';
/**
* WordPress dependencies
*/
import {
Spinner,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import ImageSize from './image-size';
import styles from './styles.scss';
export const MEDIA_UPLOAD_STATE_UPLOADING = 1;
export const MEDIA_UPLOAD_STATE_SUCCEEDED = 2;
export const MEDIA_UPLOAD_STATE_FAILED = 3;
export const MEDIA_UPLOAD_STATE_RESET = 4;
export class MediaUploadProgress extends React.Component {
constructor( props ) {
super( props );
this.state = {
progress: 0,
isUploadInProgress: false,
isUploadFailed: false,
};
this.mediaUpload = this.mediaUpload.bind( this );
}
componentDidMount() {
this.addMediaUploadListener();
}
componentWillUnmount() {
this.removeMediaUploadListener();
}
mediaUpload( payload ) {
const { mediaId } = this.props;
if ( payload.mediaId !== mediaId ) {
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_FAILED:
this.finishMediaUploadWithFailure( payload );
break;
case MEDIA_UPLOAD_STATE_RESET:
this.mediaUploadStateReset( payload );
break;
}
}
updateMediaProgress( payload ) {
this.setState( { progress: payload.progress, isUploadInProgress: true, isUploadFailed: false } );
if ( this.props.onUpdateMediaProgress ) {
this.props.onUpdateMediaProgress( payload );
}
}
finishMediaUploadWithSuccess( payload ) {
this.setState( { isUploadInProgress: false } );
if ( this.props.onFinishMediaUploadWithSuccess ) {
this.props.onFinishMediaUploadWithSuccess( payload );
}
}
finishMediaUploadWithFailure( payload ) {
this.setState( { isUploadInProgress: false, isUploadFailed: true } );
if ( this.props.onFinishMediaUploadWithFailure ) {
this.props.onFinishMediaUploadWithFailure( payload );
}
}
mediaUploadStateReset( payload ) {
this.setState( { 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();
}
}
render() {
const { coverUrl, width, height } = this.props;
const { isUploadInProgress, isUploadFailed } = this.state;
const showSpinner = this.state.isUploadInProgress;
const progress = this.state.progress * 100;
const retryMessage = __( 'Failed to insert media.\nPlease tap for options.' );
return (
<View style={ styles.mediaUploadProgress }>
{ showSpinner &&
<View style={ styles.progressBar }>
<Spinner progress={ progress } />
</View>
}
{ coverUrl &&
<ImageSize src={ coverUrl } >
{ ( sizes ) => {
const {
imageWidthWithinContainer,
imageHeightWithinContainer,
} = sizes;
let finalHeight = imageHeightWithinContainer;
if ( height > 0 && height < imageHeightWithinContainer ) {
finalHeight = height;
}
let finalWidth = imageWidthWithinContainer;
if ( width > 0 && width < imageWidthWithinContainer ) {
finalWidth = width;
}
return ( this.props.renderContent( {
isUploadInProgress,
isUploadFailed,
finalWidth,
finalHeight,
imageWidthWithinContainer,
retryMessage,
} ) );
} }
</ImageSize>
}
{ ! coverUrl && this.props.renderContent( {
isUploadInProgress,
isUploadFailed,
retryMessage,
} ) }
</View>
);
}
}
export default MediaUploadProgress;