File size: 2,006 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { translate } from 'i18n-calypso';
import { CONNECTED_APPLICATION_DELETE } from 'calypso/state/action-types';
import { deleteConnectedApplicationSuccess } from 'calypso/state/connected-applications/actions';
import { registerHandlers } from 'calypso/state/data-layer/handler-registry';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import { dispatchRequest } from 'calypso/state/data-layer/wpcom-http/utils';
import { errorNotice, successNotice } from 'calypso/state/notices/actions';

/**
 * Dispatches a request to delete a connected application for the current user
 * @param   {Object} action Redux action
 * @returns {Object} Dispatched http action
 */
export const removeConnectedApplication = ( action ) =>
	http(
		{
			apiVersion: '1.1',
			method: 'POST',
			path: '/me/connected-applications/' + action.appId + '/delete',
		},
		action
	);

/**
 * Dispatches a user connected application removal success action and notice when the request succeeded.
 * @param   {Object} action Redux action
 * @param   {string} action.appId
 * @returns {Object} Dispatched user connected applications add action
 */
export const handleRemoveSuccess = ( { appId } ) => [
	deleteConnectedApplicationSuccess( appId ),
	successNotice(
		translate( 'This application no longer has access to your WordPress.com account.' ),
		{
			duration: 8000,
			id: `connected-app-notice-success-${ appId }`,
		}
	),
];

/**
 * Dispatches an error notice when the request failed.
 * @returns {Object} Dispatched error notice action
 */
export const handleRemoveError = () =>
	errorNotice( translate( 'The connected application was not disconnected. Please try again.' ), {
		duration: 8000,
		id: 'connected-app-notice-error',
	} );

registerHandlers( 'state/data-layer/wpcom/me/connected-applications/delete/index.js', {
	[ CONNECTED_APPLICATION_DELETE ]: [
		dispatchRequest( {
			fetch: removeConnectedApplication,
			onSuccess: handleRemoveSuccess,
			onError: handleRemoveError,
		} ),
	],
} );