File size: 1,211 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
import { JETPACK_CREDENTIALS_TEST } 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 {
	markCredentialsAsInvalid,
	markCredentialsAsValid,
} from 'calypso/state/jetpack/credentials/actions';

export const testCredentials = ( action ) =>
	http(
		{
			apiNamespace: 'wpcom/v2',
			method: 'POST',
			path: `/sites/${ action.siteId }/rewind/credentials/test`,
			query: {
				role: action.role,
			},
			body: {},
		},
		action
	);

export const testCredentialsSucceeded = ( { siteId, role }, { ok: testResult } ) => {
	if ( ! testResult ) {
		return markCredentialsAsInvalid( siteId, role );
	}

	return markCredentialsAsValid( siteId, role );
};

export const testCredentialsFailed = ( { siteId, role } ) =>
	markCredentialsAsInvalid( siteId, role );

registerHandlers( 'state/data-layer/wpcom/sites/rewind/credentials', {
	[ JETPACK_CREDENTIALS_TEST ]: [
		dispatchRequest( {
			fetch: testCredentials,
			onSuccess: testCredentialsSucceeded,
			onError: testCredentialsFailed,
		} ),
	],
} );