File size: 2,621 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
import i18n from 'i18n-calypso';
import { JETPACK_SCAN_THREATS_GET_FIX_STATUS } 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 { requestScanStatus } from 'calypso/state/jetpack-scan/actions';
import { requestJetpackScanHistory } from 'calypso/state/jetpack-scan/history/actions';
import { getFixThreatsStatus } from 'calypso/state/jetpack-scan/threats/actions';
import { errorNotice, successNotice } from 'calypso/state/notices/actions';

const POLL_EVERY_MILLISECONDS = 1000;

export const request = ( action ) => {
	return [
		http(
			{
				apiNamespace: 'wpcom/v2',
				method: 'GET',
				path: `/sites/${ action.siteId }/alerts/fix/`,
				query: { threat_ids: action.threatIds },
			},
			{ ...action }
		),
	];
};

export const success = ( action, fixer_state ) => {
	const threatArray = Object.values( fixer_state.threats );

	const inProgressThreats = threatArray.filter( ( threat ) => threat.status === 'in_progress' );

	if ( inProgressThreats.length > 0 ) {
		return [
			( dispatch ) =>
				setTimeout(
					() => dispatch( getFixThreatsStatus( action.siteId, action.threatIds ) ),
					POLL_EVERY_MILLISECONDS
				),
		];
	}

	const fixedThreats = threatArray.filter( ( threat ) => threat.status === 'fixed' );

	if ( fixedThreats.length === action.threatIds.length ) {
		return [
			successNotice(
				i18n.translate(
					'The threat was successfully fixed.',
					'All threats were successfully fixed.',
					{ count: fixedThreats.length }
				),
				{
					duration: 4000,
				}
			),
			requestScanStatus( action.siteId ),
			// Since we can fix threats from the History section, we need to update that
			// information as well.
			requestJetpackScanHistory( action.siteId ),
		];
	}

	return [
		errorNotice( i18n.translate( 'Not all threats could be fixed. Please contact our support.' ), {
			duration: 4000,
		} ),
		requestScanStatus( action.siteId ),
		// Since we can fix threats from the History section, we need to update that
		// information as well.
		requestJetpackScanHistory( action.siteId ),
	];
};

export const failure = ( action ) =>
	errorNotice( i18n.translate( 'Error getting fixer status.' ), {
		duration: 10000,
		id: action.noticeId,
	} );

registerHandlers( 'state/data-layer/wpcom/sites/alerts/fix-status.js', {
	[ JETPACK_SCAN_THREATS_GET_FIX_STATUS ]: [
		dispatchRequest( {
			fetch: request,
			onSuccess: success,
			onError: failure,
		} ),
	],
} );