-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
AvatarWithImagePicker.js
290 lines (259 loc) · 10.3 KB
/
AvatarWithImagePicker.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import _ from 'underscore';
import React from 'react';
import {
Pressable, View, Image,
} from 'react-native';
import PropTypes from 'prop-types';
import lodashGet from 'lodash/get';
import Avatar from './Avatar';
import Icon from './Icon';
import PopoverMenu from './PopoverMenu';
import * as Expensicons from './Icon/Expensicons';
import styles from '../styles/styles';
import themeColors from '../styles/themes/default';
import AttachmentPicker from './AttachmentPicker';
import ConfirmModal from './ConfirmModal';
import AvatarCropModal from './AvatarCropModal/AvatarCropModal';
import withLocalize, {withLocalizePropTypes} from './withLocalize';
import variables from '../styles/variables';
import CONST from '../CONST';
import SpinningIndicatorAnimation from '../styles/animation/SpinningIndicatorAnimation';
import Tooltip from './Tooltip';
import stylePropTypes from '../styles/stylePropTypes';
const propTypes = {
/** Avatar URL to display */
avatarURL: PropTypes.string,
/** Additional style props */
style: stylePropTypes,
/** Executed once an image has been selected */
onImageSelected: PropTypes.func,
/** Execute when the user taps "remove" */
onImageRemoved: PropTypes.func,
/** A default avatar component to display when there is no avatarURL */
DefaultAvatar: PropTypes.func,
/** Whether we are using the default avatar */
isUsingDefaultAvatar: PropTypes.bool,
/** The anchor position of the menu */
anchorPosition: PropTypes.shape({
top: PropTypes.number,
right: PropTypes.number,
bottom: PropTypes.number,
left: PropTypes.number,
}).isRequired,
/** Flag to see if image is being uploaded */
isUploading: PropTypes.bool,
/** Size of Indicator */
size: PropTypes.oneOf([CONST.AVATAR_SIZE.LARGE, CONST.AVATAR_SIZE.DEFAULT]),
/** A fallback avatar icon to display when there is an error on loading avatar from remote URL. */
fallbackIcon: PropTypes.func,
...withLocalizePropTypes,
};
const defaultProps = {
avatarURL: '',
onImageSelected: () => {},
onImageRemoved: () => {},
style: [],
DefaultAvatar: () => {},
isUsingDefaultAvatar: false,
isUploading: false,
size: CONST.AVATAR_SIZE.DEFAULT,
fallbackIcon: Expensicons.FallbackAvatar,
};
class AvatarWithImagePicker extends React.Component {
constructor(props) {
super(props);
this.animation = new SpinningIndicatorAnimation();
this.hideErrorModal = this.hideErrorModal.bind(this);
this.showErrorModal = this.showErrorModal.bind(this);
this.isValidSize = this.isValidSize.bind(this);
this.showAvatarCropModal = this.showAvatarCropModal.bind(this);
this.hideAvatarCropModal = this.hideAvatarCropModal.bind(this);
this.state = {
isMenuVisible: false,
isErrorModalVisible: false,
errorModalPrompt: '',
errorModalTitle: '',
isAvatarCropModalOpen: false,
imageName: '',
imageUri: '',
};
}
componentDidMount() {
if (!this.props.isUploading) {
return;
}
this.animation.start();
}
componentDidUpdate(prevProps) {
if (!prevProps.isUploading && this.props.isUploading) {
this.animation.start();
} else if (prevProps.isUploading && !this.props.isUploading) {
this.animation.stop();
}
}
componentWillUnmount() {
this.animation.stop();
}
/**
* @param {String} title
* @param {String} prompt
*/
showErrorModal(title, prompt) {
this.setState({isErrorModalVisible: true, errorModalTitle: title, errorModalPrompt: prompt});
}
hideErrorModal() {
this.setState({isErrorModalVisible: false});
}
/**
* Check if the attachment size is less than allowed size.
*
* @param {Object} image
* @returns {Boolean}
*/
isValidSize(image) {
return image && lodashGet(image, 'size', 0) < CONST.AVATAR_MAX_ATTACHMENT_SIZE;
}
/**
* Check if the attachment resolution is bigger than required.
*
* @param {String} imageUri
* @returns {Promise}
*/
isValidResolution(imageUri) {
return new Promise((resolve) => {
Image.getSize(imageUri, (width, height) => {
resolve(height >= CONST.AVATAR_MIN_HEIGHT_PX && width >= CONST.AVATAR_MIN_WIDTH_PX);
});
});
}
/**
* Validates if an image has a valid resolution and opens an avatar crop modal
*
* @param {Object} image
*/
showAvatarCropModal(image) {
if (!this.isValidSize(image)) {
this.showErrorModal(
this.props.translate('avatarWithImagePicker.imageUploadFailed'),
this.props.translate('avatarWithImagePicker.sizeExceeded', {maxUploadSizeInMB: CONST.AVATAR_MAX_ATTACHMENT_SIZE / (1024 * 1024)}),
);
return;
}
this.isValidResolution(image.uri)
.then((isValidResolution) => {
if (!isValidResolution) {
this.showErrorModal(
this.props.translate('avatarWithImagePicker.imageUploadFailed'),
this.props.translate('avatarWithImagePicker.tooSmallResolution', {
minHeightInPx: CONST.AVATAR_MIN_HEIGHT_PX,
minWidthInPx: CONST.AVATAR_MIN_WIDTH_PX,
}),
);
return;
}
this.setState({isAvatarCropModalOpen: true, imageUri: image.uri, imageName: image.name});
});
}
hideAvatarCropModal() {
this.setState({isAvatarCropModalOpen: false});
}
/**
* Create menu items list for avatar menu
*
* @param {Function} openPicker
* @returns {Array}
*/
createMenuItems(openPicker) {
const menuItems = [
{
icon: Expensicons.Upload,
text: this.props.translate('avatarWithImagePicker.uploadPhoto'),
onSelected: () => {
openPicker({
onPicked: this.showAvatarCropModal,
});
},
},
];
// If current avatar isn't a default avatar, allow Remove Photo option
if (!this.props.isUsingDefaultAvatar) {
menuItems.push({
icon: Expensicons.Trashcan,
text: this.props.translate('avatarWithImagePicker.removePhoto'),
onSelected: () => {
this.props.onImageRemoved();
},
});
}
return menuItems;
}
render() {
const DefaultAvatar = this.props.DefaultAvatar;
const additionalStyles = _.isArray(this.props.style) ? this.props.style : [this.props.style];
return (
<View style={[styles.alignItemsCenter, ...additionalStyles]}>
<Pressable
onPress={() => this.setState({isMenuVisible: true})}
>
<View style={[styles.pRelative, styles.avatarLarge]}>
{this.props.avatarURL
? (
<Avatar
containerStyles={styles.avatarLarge}
imageStyles={[styles.avatarLarge, styles.alignSelfCenter]}
source={this.props.avatarURL}
fallbackIcon={this.props.fallbackIcon}
size={this.props.size}
/>
)
: (
<DefaultAvatar />
)}
<AttachmentPicker type={CONST.ATTACHMENT_PICKER_TYPE.IMAGE}>
{({openPicker}) => (
<>
<Tooltip absolute text={this.props.translate('avatarWithImagePicker.editImage')}>
<View style={[styles.smallEditIcon, styles.smallAvatarEditIcon]}>
<Icon
src={Expensicons.Camera}
width={variables.iconSizeSmall}
height={variables.iconSizeSmall}
fill={themeColors.iconReversed}
/>
</View>
</Tooltip>
<PopoverMenu
isVisible={this.state.isMenuVisible}
onClose={() => this.setState({isMenuVisible: false})}
onItemSelected={() => this.setState({isMenuVisible: false})}
menuItems={this.createMenuItems(openPicker)}
anchorPosition={this.props.anchorPosition}
/>
</>
)}
</AttachmentPicker>
</View>
</Pressable>
<ConfirmModal
title={this.state.errorModalTitle}
onConfirm={this.hideErrorModal}
onCancel={this.hideErrorModal}
isVisible={this.state.isErrorModalVisible}
prompt={this.state.errorModalPrompt}
confirmText={this.props.translate('common.close')}
shouldShowCancelButton={false}
/>
<AvatarCropModal
onClose={this.hideAvatarCropModal}
isVisible={this.state.isAvatarCropModalOpen}
onSave={this.props.onImageSelected}
imageUri={this.state.imageUri}
imageName={this.state.imageName}
/>
</View>
);
}
}
AvatarWithImagePicker.propTypes = propTypes;
AvatarWithImagePicker.defaultProps = defaultProps;
export default withLocalize(AvatarWithImagePicker);