File size: 5,178 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { translate } from 'i18n-calypso';
import { isEmpty, get } from 'lodash';
import {
	REWIND_BACKUP_PROGRESS_REQUEST,
	REWIND_BACKUP_DISMISS_PROGRESS,
} from 'calypso/state/action-types';
import {
	updateRewindBackupProgress,
	rewindBackupUpdateError,
} from 'calypso/state/activity-log/actions';
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 } from 'calypso/state/notices/actions';

/** @type {number} how many ms between polls for same data */
const POLL_INTERVAL = 1500;

/** @type {Map<string, number>} stores most-recent polling times */
const recentRequests = new Map();

/**
 * Fetch status updates for backup file downloads
 *
 * Note! Eventually the manual "caching" here should be
 * replaced by the `freshness` system in the data layer
 * when it arrives. For now, it's statefully ugly.
 * @param {Object} action Redux action
 */
const fetchProgress = ( action ) => {
	const { downloadId, siteId } = action;
	const key = `${ siteId }-${ downloadId }`;

	const lastUpdate = recentRequests.get( key ) || -Infinity;
	const now = Date.now();

	if ( now - lastUpdate < POLL_INTERVAL ) {
		return;
	}

	recentRequests.set( key, now );

	return http(
		{
			method: 'GET',
			apiNamespace: 'wpcom/v2',
			path: `/sites/${ action.siteId }/rewind/downloads`,
		},
		action
	);
};

/**
 * Parse and merge response data for backup creation status with defaults.
 * @param   {Object} data The data received from API response.
 * @returns {Object}      Parsed response data.
 */
const fromApi = ( data ) => ( {
	backupPoint: data.backupPoint,
	downloadId: +data.downloadId,
	progress: +data.progress,
	rewindId: data.rewindId,
	startedAt: data.startedAt,
	downloadCount: +data.downloadCount,
	bytesFormatted: data.bytesFormatted,
	validUntil: data.validUntil,
	url: data.url,
	error: data.error,
} );

/**
 * When requesting the status of a backup creation, an array with a single element is returned.
 * This single element contains the information about the latest backup creation progress.
 * If an error property was found, it will display an error card stating it.
 * Otherwise the backup creation progress will be updated.
 * @param {number}   siteId   Id of the site for the one we're creating a backup.
 * @param {Object}   apiData  Data returned by a successful response.
 */
export const updateProgress = ( { siteId }, apiData ) => {
	const [ latestDownloadableBackup ] = apiData;

	if ( isEmpty( latestDownloadableBackup ) ) {
		return updateRewindBackupProgress( siteId );
	}

	const data = fromApi( latestDownloadableBackup );
	return get( data, [ 'error' ], false )
		? rewindBackupUpdateError( siteId, data.downloadId, data )
		: updateRewindBackupProgress( siteId, data.downloadId, data );
};

/**
 * If the backup creation progress request fails, an error notice will be shown.
 * @returns {Function}          The dispatched action.
 */
export const announceError = () =>
	errorNotice(
		translate( "Hmm, we can't update the status of your backup. Please refresh this page." )
	);

/**
 * Mark a specific downloadable backup record as dismissed.
 * This has the effect that subsequent calls to /sites/%site_id%/rewind/downloads won't return the download.
 * @param   {Object}   action   Changeset to update state.
 * @returns {Object}          The dispatched action.
 */
export const dismissBackup = ( action ) =>
	http(
		{
			method: 'POST',
			apiNamespace: 'wpcom/v2',
			path: `/sites/${ action.siteId }/rewind/downloads/${ action.downloadId }`,
			body: {
				dismissed: true,
			},
		},
		action
	);

/**
 * On successful dismiss, the card will be removed and we don't need to do anything further.
 * If request succeeded but backup couldn't be dismissed, a notice will be shown.
 * @param {Object}   action   Changeset to update state.
 * @param {Object}     data     Description of request result.
 */
export const backupSilentlyDismissed = ( action, data ) =>
	! data.dismissed
		? errorNotice( translate( 'Dismissing backup failed. Please reload and try again.' ) )
		: null;

/**
 * If a dismiss request fails, an error notice will be shown.
 * @returns {Function} The dispatched action.
 */
export const backupDismissFailed = () =>
	errorNotice( translate( 'Dismissing backup failed. Please reload and try again.' ) );

/**
 * Parse and merge response data for backup dismiss result with defaults.
 * @param   {Object} data   The data received from API response.
 * @returns {Object} Parsed response data.
 */
const fromBackupDismiss = ( data ) => ( {
	downloadId: +data.download_id,
	dismissed: data.is_dismissed,
} );

registerHandlers( 'state/data-layer/wpcom/sites/rewind/downloads', {
	[ REWIND_BACKUP_PROGRESS_REQUEST ]: [
		dispatchRequest( {
			fetch: fetchProgress,
			onSuccess: updateProgress,
			onError: announceError,
		} ),
	],
	[ REWIND_BACKUP_DISMISS_PROGRESS ]: [
		dispatchRequest( {
			fetch: dismissBackup,
			onSuccess: backupSilentlyDismissed,
			onError: backupDismissFailed,
			fromApi: fromBackupDismiss,
		} ),
	],
} );