File size: 1,288 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
import { translate } from 'i18n-calypso';
import {
	JETPACK_CREDENTIALS_GET,
	JETPACK_CREDENTIALS_GET_FAILURE,
	JETPACK_CREDENTIALS_GET_SUCCESS,
} from 'calypso/state/action-types';
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 } from 'calypso/state/notices/actions';

export const getCredentials = ( action ) =>
	http(
		{
			method: 'GET',
			path: `/activity-log/${ action.siteId }/get-credentials`,
			apiVersion: '1',
		},
		action
	);

export const getCredentialsSucceeded = ( { siteId }, { credentials } ) => ( {
	type: JETPACK_CREDENTIALS_GET_SUCCESS,
	credentials,
	siteId,
} );

export const getCredentialsFailed = ( { siteId }, error ) => [
	errorNotice(
		translate( 'There was a problem fetching your site credentials. Please refresh the page.' )
	),
	{
		type: JETPACK_CREDENTIALS_GET_FAILURE,
		siteId,
		wpcomError: error,
	},
];

registerHandlers( 'state/data-layer/wpcom/activity-log/get-credentials', {
	[ JETPACK_CREDENTIALS_GET ]: [
		dispatchRequest( {
			fetch: getCredentials,
			onSuccess: getCredentialsSucceeded,
			onError: getCredentialsFailed,
		} ),
	],
} );