Skip to content

Commit

Permalink
fix(aws-amplify-react): added prop onS3ImageSuccess to the S3Image co…
Browse files Browse the repository at this point in the history
…mponent to have a clear sepration of concerns (aws-amplify#4848)

Co-authored-by: Eric Clemmons <[email protected]>
Co-authored-by: Jordan Ranz <[email protected]>
  • Loading branch information
3 people authored Feb 12, 2020
1 parent 17452eb commit 0c336ef
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4453,6 +4453,7 @@ exports[`signUp with signUpConfig render correctly with authState signUp 1`] = `
>
<Component
data-test="sign-up-create-account-button"
disabled={false}
onClick={[Function]}
theme={
Object {
Expand Down Expand Up @@ -8911,6 +8912,7 @@ exports[`signUp without signUpConfig prop normal case render correctly with auth
>
<Component
data-test="sign-up-create-account-button"
disabled={false}
onClick={[Function]}
theme={
Object {
Expand Down
8 changes: 6 additions & 2 deletions packages/aws-amplify-react/__tests__/Storage/S3Image-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ describe('S3Image', () => {
return;
});

const wrapper = shallow(<S3Image />);
const mockOnUploadSuccess = jest.fn();
const wrapper = shallow(
<S3Image onUploadSuccess={mockOnUploadSuccess} />
);
const s3Image = wrapper.instance();
wrapper.setProps({
imgKey: 'imgKey',
Expand All @@ -189,12 +192,13 @@ describe('S3Image', () => {

await s3Image.handlePick(data);

expect.assertions(2);
expect.assertions(3);
expect(spyon).toBeCalledWith('imgKey', 'file', {
contentType: 'type',
level: 'level',
track: undefined,
});
expect(mockOnUploadSuccess).toBeCalled();
expect(spyon2).toBeCalled();

spyon.mockClear();
Expand Down
15 changes: 12 additions & 3 deletions packages/aws-amplify-react/src/Storage/S3Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface IS3ImageProps {
onClick?: any;
onError?: any;
onLoad?: any;
onUploadSuccess?: any;
path?: any;
picker?: any;
selected?: any;
Expand Down Expand Up @@ -142,9 +143,16 @@ export default class S3Image extends Component<IS3ImageProps, IS3ImageState> {
handlePick(data) {
const that = this;

const path = this.props.path || '';
const { imgKey, level, fileToKey, track, identityId } = this.props;
const { file, name, size, type } = data;
const {
imgKey,
level,
fileToKey,
track,
identityId,
path = '',
onUploadSuccess,
} = this.props;
const { file, type } = data;
const key = imgKey || path + calcKey(data, fileToKey);
if (!Storage || typeof Storage.put !== 'function') {
throw new Error(
Expand All @@ -159,6 +167,7 @@ export default class S3Image extends Component<IS3ImageProps, IS3ImageState> {
.then(data => {
logger.debug('handle pick data', data);
that.getImageSource(key, level, track, identityId);
if (onUploadSuccess) onUploadSuccess();
})
.catch(err => logger.debug('handle pick error', err));
}
Expand Down

0 comments on commit 0c336ef

Please sign in to comment.