-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* EditGravatar: remove bearer token and use new endpoint * Config: enable me/edit-gravatar in development * Data-layer: add gravatarUpload * EditGravatar: Fix calypso_edit_gravatar_file_receive_failure event typo
- Loading branch information
1 parent
b1f47d8
commit f839992
Showing
7 changed files
with
177 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
GRAVATAR_UPLOAD_RECEIVE, | ||
GRAVATAR_UPLOAD_REQUEST, | ||
GRAVATAR_UPLOAD_REQUEST_SUCCESS, | ||
GRAVATAR_UPLOAD_REQUEST_FAILURE, | ||
} from 'state/action-types'; | ||
import { http } from 'state/data-layer/wpcom-http/actions'; | ||
import { dispatchRequest } from 'state/data-layer/wpcom-http/utils'; | ||
import { | ||
bumpStat, | ||
composeAnalytics, | ||
recordTracksEvent, | ||
withAnalytics, | ||
} from 'state/analytics/actions'; | ||
|
||
export function uploadGravatar( { dispatch }, action ) { | ||
const { email, file } = action; | ||
dispatch( http( { | ||
method: 'POST', | ||
path: '/gravatar-upload', | ||
body: {}, | ||
apiNamespace: 'wpcom/v2', | ||
formData: [ | ||
[ 'account', email ], | ||
[ 'filedata', file ], | ||
], | ||
}, action ) ); | ||
} | ||
|
||
export function announceSuccess( { dispatch }, { file } ) { | ||
const fileReader = new FileReader(); | ||
fileReader.addEventListener( 'load', () => { | ||
dispatch( { | ||
type: GRAVATAR_UPLOAD_RECEIVE, | ||
src: fileReader.result, | ||
} ); | ||
dispatch( withAnalytics( | ||
recordTracksEvent( 'calypso_edit_gravatar_upload_success' ), | ||
{ type: GRAVATAR_UPLOAD_REQUEST_SUCCESS } | ||
) ); | ||
} ); | ||
fileReader.readAsDataURL( file ); | ||
} | ||
|
||
export function announceFailure( { dispatch } ) { | ||
dispatch( withAnalytics( | ||
composeAnalytics( | ||
recordTracksEvent( 'calypso_edit_gravatar_upload_failure' ), | ||
bumpStat( 'calypso_gravatar_update_error', 'unsuccessful_http_response' ) | ||
), | ||
{ type: GRAVATAR_UPLOAD_REQUEST_FAILURE } | ||
) ); | ||
} | ||
|
||
export default { | ||
[ GRAVATAR_UPLOAD_REQUEST ]: [ dispatchRequest( uploadGravatar, announceSuccess, announceFailure ) ], | ||
}; |
101 changes: 101 additions & 0 deletions
101
client/state/data-layer/wpcom/gravatar-upload/test/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
import sinon, { spy } from 'sinon'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useSandbox } from 'test/helpers/use-sinon'; | ||
import { http } from 'state/data-layer/wpcom-http/actions'; | ||
import { | ||
GRAVATAR_UPLOAD_RECEIVE, | ||
GRAVATAR_UPLOAD_REQUEST_SUCCESS, | ||
GRAVATAR_UPLOAD_REQUEST_FAILURE, | ||
} from 'state/action-types'; | ||
import { | ||
uploadGravatar, | ||
announceSuccess, | ||
announceFailure, | ||
} from '../'; | ||
|
||
describe( '#uploadGravatar()', () => { | ||
it( 'dispatches an HTTP request to the gravatar upload endpoint', () => { | ||
const action = { | ||
type: 'DUMMY_ACTION', | ||
file: 'file', | ||
email: 'email', | ||
}; | ||
const dispatch = spy(); | ||
|
||
uploadGravatar( { dispatch }, action ); | ||
|
||
expect( dispatch ).to.have.been.calledOnce; | ||
expect( dispatch ).to.have.been.calledWith( http( { | ||
apiNamespace: 'wpcom/v2', | ||
method: 'POST', | ||
body: {}, | ||
path: '/gravatar-upload', | ||
formData: [ | ||
[ 'account', 'email' ], | ||
[ 'filedata', 'file' ], | ||
], | ||
}, action ) ); | ||
} ); | ||
} ); | ||
|
||
describe( '#announceSuccess()', () => { | ||
let sandbox; | ||
const noop = () => {}; | ||
|
||
const tempImageSrc = 'tempImageSrc'; | ||
useSandbox( newSandbox => { | ||
sandbox = newSandbox; | ||
global.FormData = sandbox.stub().returns( { | ||
append: noop | ||
} ); | ||
global.FileReader = sandbox.stub().returns( { | ||
readAsDataURL: noop, | ||
addEventListener: function( event, callback ) { | ||
this.result = tempImageSrc; | ||
callback(); | ||
} | ||
} ); | ||
} ); | ||
|
||
it( 'dispatches a success action when the file is read', () => { | ||
const action = { | ||
type: 'DUMMY_ACTION', | ||
file: 'file', | ||
email: 'email', | ||
}; | ||
const dispatch = spy(); | ||
|
||
announceSuccess( { dispatch }, action, noop, { success: true } ); | ||
expect( dispatch ).to.have.been.calledWith( sinon.match( { type: GRAVATAR_UPLOAD_REQUEST_SUCCESS } ) ); | ||
} ); | ||
|
||
it( 'dispatches a upload received action with the image data when the file is read', () => { | ||
const action = { | ||
type: 'DUMMY_ACTION', | ||
file: 'file', | ||
email: 'email', | ||
}; | ||
const dispatch = spy(); | ||
|
||
announceSuccess( { dispatch }, action, noop, { success: true } ); | ||
expect( dispatch ).to.have.been.calledWith( sinon.match( { type: GRAVATAR_UPLOAD_RECEIVE, src: 'tempImageSrc' } ) ); | ||
} ); | ||
} ); | ||
|
||
describe( '#announceFailure()', () => { | ||
it( 'should dispatch an error notice', () => { | ||
const dispatch = spy(); | ||
|
||
announceFailure( { dispatch } ); | ||
|
||
expect( dispatch ).to.have.been.calledOnce; | ||
expect( dispatch ).to.have.been.calledWith( sinon.match( { type: GRAVATAR_UPLOAD_REQUEST_FAILURE } ) ); | ||
} ); | ||
} ); |
Oops, something went wrong.