Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple avatar image types #12549

Merged
merged 7 commits into from
Dec 1, 2022
Merged
12 changes: 10 additions & 2 deletions src/components/AvatarCropModal/AvatarCropModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const propTypes = {
/** Name of the image */
imageName: PropTypes.string,

/** Type of the image file */
imageType: PropTypes.string,

/** Callback to be called when user closes the modal */
onClose: PropTypes.func,

Expand All @@ -54,6 +57,7 @@ const propTypes = {
const defaultProps = {
imageUri: '',
imageName: '',
imageType: '',
onClose: () => {},
onSave: () => {},
};
Expand Down Expand Up @@ -266,12 +270,16 @@ const AvatarCropModal = (props) => {
height: size, width: size, originX, originY,
};

cropOrRotateImage(props.imageUri, [{rotate: rotation.value % 360}, {crop}], {compress: 1, name: props.imageName})
cropOrRotateImage(
props.imageUri,
[{rotate: rotation.value % 360}, {crop}],
{compress: 1, name: props.imageName, type: props.imageType},
)
.then((newImage) => {
props.onClose();
props.onSave(newImage);
});
}, [props.imageUri, props.imageName, imageContainerSize]);
}, [props.imageUri, props.imageName, props.imageType, imageContainerSize]);

/**
* @param {Event} event
Expand Down
9 changes: 8 additions & 1 deletion src/components/AvatarWithImagePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class AvatarWithImagePicker extends React.Component {
isAvatarCropModalOpen: false,
imageName: '',
imageUri: '',
imageType: '',
};
}

Expand Down Expand Up @@ -194,7 +195,12 @@ class AvatarWithImagePicker extends React.Component {
return;
}

this.setState({isAvatarCropModalOpen: true, imageUri: image.uri, imageName: image.name});
this.setState({
isAvatarCropModalOpen: true,
imageUri: image.uri,
imageName: image.name,
imageType: image.type,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this (image type) is the main "new thing" that we're passing down to cropOrRotateImage

});
});
}

Expand Down Expand Up @@ -297,6 +303,7 @@ class AvatarWithImagePicker extends React.Component {
onSave={this.props.onImageSelected}
imageUri={this.state.imageUri}
imageName={this.state.imageName}
imageType={this.state.imageType}
/>
</View>
);
Expand Down
2 changes: 1 addition & 1 deletion src/libs/cropOrRotateImage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function cropCanvas(canvas, options) {
function convertCanvasToFile(canvas, options = {}) {
return new Promise((resolve) => {
canvas.toBlob((blob) => {
const file = new File([blob], `${options.name || 'fileName'}.jpg`, {type: 'image/jpeg'});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idk why we were appending .jpg to every file name before 😅

const file = new File([blob], options.name || 'fileName.jpeg', {type: options.type || 'image/jpeg'});
file.uri = URL.createObjectURL(file);
resolve(file);
});
Expand Down
2 changes: 1 addition & 1 deletion src/libs/cropOrRotateImage/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function cropOrRotateImage(uri, actions, options = {}) {
RNImageManipulator.manipulate(uri, actions, options).then((result) => {
RNFetchBlob.fs.stat(result.uri.replace('file://', '')).then(({size}) => {
resolve({
...result, size, type: 'image/png', name: `${options.name || 'fileName'}.jpg`,
...result, size, type: options.type || 'image/jpeg', name: options.name || 'fileName.jpg',
});
});
});
Expand Down