File size: 1,412 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
import {
	JETPACK_CREDENTIALS_DELETE,
	JETPACK_CREDENTIALS_STORE,
	REWIND_STATE_UPDATE,
} from 'calypso/state/action-types';
import { registerHandlers } from 'calypso/state/data-layer/handler-registry';
import { transformApi } from 'calypso/state/data-layer/wpcom/sites/rewind/api-transformer';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import { dispatchRequest } from 'calypso/state/data-layer/wpcom-http/utils';

const noop = () => {};

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

export const success = ( { siteId }, { rewind_state } ) => {
	const storeAction = {
		type: JETPACK_CREDENTIALS_STORE,
		credentials: {
			main: null,
		},
		siteId,
	};

	// the API transform could fail and the rewind data might
	// be unavailable so if that's the case just let it go
	// for now. we'll improve our rigor as time goes by.
	try {
		return [
			storeAction,
			{
				type: REWIND_STATE_UPDATE,
				siteId,
				data: transformApi( rewind_state ),
			},
		];
	} catch ( e ) {
		return storeAction;
	}
};

registerHandlers( 'state/data-layer/wpcom/sites/rewind/credentials/delete', {
	[ JETPACK_CREDENTIALS_DELETE ]: [
		dispatchRequest( {
			fetch: request,
			onSuccess: success,
			onError: noop,
		} ),
	],
} );