File size: 1,915 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
import { translate } from 'i18n-calypso';
import {
	JETPACK_BACKUP_RETENTION_UPDATE,
	JETPACK_BACKUP_RETENTION_UPDATE_ERROR,
	JETPACK_BACKUP_RETENTION_UPDATE_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, successNotice } from 'calypso/state/notices/actions';
import { setBackupRetention } from 'calypso/state/rewind/retention/actions';
import type { UpdateRequestActionType } from 'calypso/state/rewind/retention/types';

export const updateRetention = ( action: UpdateRequestActionType ) =>
	http(
		{
			apiNamespace: 'wpcom/v2',
			method: 'POST',
			path: `/sites/${ action.siteId }/backup/retention/update`,
			body: { retention_days: action.retentionDays },
		},
		action
	);

const onSuccess = ( { siteId, retentionDays }: UpdateRequestActionType ) => [
	{
		type: JETPACK_BACKUP_RETENTION_UPDATE_SUCCESS,
		siteId,
	},
	// This will update the backup retention period in the state,
	// so we don't need to fetch again the updated value from API
	setBackupRetention( siteId, retentionDays ),
	successNotice(
		translate( 'You have successfully changed the time period of saved backups to %(days)d days', {
			args: {
				days: retentionDays,
			},
		} ),
		{
			duration: 5000,
			isPersistent: true,
		}
	),
];

const onError = ( { siteId }: UpdateRequestActionType, error: string ) => [
	{
		type: JETPACK_BACKUP_RETENTION_UPDATE_ERROR,
		siteId,
		error,
	},
	errorNotice( translate( 'Update settings failed. Please, try again.' ), {
		duration: 5000,
	} ),
];

registerHandlers( 'state/data-layer/wpcom/sites/rewind/retention', {
	[ JETPACK_BACKUP_RETENTION_UPDATE ]: [
		dispatchRequest( {
			fetch: updateRetention,
			onSuccess,
			onError,
		} ),
	],
} );