File size: 2,118 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { translate } from 'i18n-calypso';
import {
	HOSTING_SSH_ACCESS_REQUEST,
	HOSTING_SSH_ACCESS_ENABLE,
	HOSTING_SSH_ACCESS_DISABLE,
} 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 { setAtomicSshAccess } from 'calypso/state/hosting/actions';
import { errorNotice } from 'calypso/state/notices/actions';

const getSshAccessStatus = ( action ) =>
	http(
		{
			method: 'GET',
			path: `/sites/${ action.siteId }/hosting/ssh-access`,
			apiNamespace: 'wpcom/v2',
			body: {},
		},
		action
	);

const enableSshAccess = ( action ) =>
	http(
		{
			method: 'POST',
			path: `/sites/${ action.siteId }/hosting/ssh-access`,
			apiNamespace: 'wpcom/v2',
			body: {
				setting: 'ssh',
			},
		},
		action
	);

const disableSshAccess = ( action ) =>
	http(
		{
			method: 'POST',
			path: `/sites/${ action.siteId }/hosting/ssh-access`,
			apiNamespace: 'wpcom/v2',
			body: {
				setting: 'sftp',
			},
		},
		action
	);

const setSshAccess = ( { siteId }, status ) => {
	return setAtomicSshAccess( siteId, status );
};

const displaySshAccessError = () => [
	errorNotice(
		translate(
			'Sorry, we had a problem retrieving your SSH access details. Please refresh the page and try again.'
		),
		{
			duration: 5000,
		}
	),
];

registerHandlers( 'state/data-layer/wpcom/sites/hosting/ssh-access.js', {
	[ HOSTING_SSH_ACCESS_REQUEST ]: [
		dispatchRequest( {
			fetch: getSshAccessStatus,
			onSuccess: setSshAccess,
			onError: displaySshAccessError,
			fromApi: ( { setting } ) => setting,
		} ),
	],
	[ HOSTING_SSH_ACCESS_ENABLE ]: [
		dispatchRequest( {
			fetch: enableSshAccess,
			onSuccess: setSshAccess,
			onError: displaySshAccessError,
			fromApi: ( { setting } ) => setting,
		} ),
	],
	[ HOSTING_SSH_ACCESS_DISABLE ]: [
		dispatchRequest( {
			fetch: disableSshAccess,
			onSuccess: setSshAccess,
			onError: displaySshAccessError,
			fromApi: ( { setting } ) => setting,
		} ),
	],
} );