File size: 8,230 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import page from '@automattic/calypso-router';
import { addQueryArgs } from 'calypso/lib/url';
import {
	ACTIVITY_LOG_FILTER_SET,
	ACTIVITY_LOG_FILTER_UPDATE,
	REWIND_ACTIVITY_SHARE_REQUEST,
	REWIND_CLONE,
	REWIND_RESTORE,
	REWIND_RESTORE_DISMISS,
	REWIND_RESTORE_DISMISS_PROGRESS,
	REWIND_GRANULAR_RESTORE,
	REWIND_GRANULAR_BACKUP_REQUEST,
	REWIND_RESTORE_PROGRESS_REQUEST,
	REWIND_RESTORE_REQUEST,
	REWIND_RESTORE_UPDATE_PROGRESS,
	REWIND_STAGING_CLONE,
	REWIND_BACKUP,
	REWIND_BACKUP_REQUEST,
	REWIND_BACKUP_DISMISS,
	REWIND_BACKUP_PROGRESS_REQUEST,
	REWIND_BACKUP_SET_DOWNLOAD_ID,
	REWIND_BACKUP_SITE,
	REWIND_BACKUP_UPDATE_ERROR,
	REWIND_BACKUP_UPDATE_PROGRESS,
	REWIND_BACKUP_DISMISS_PROGRESS,
} from 'calypso/state/action-types';
import getActivityLogFilter from 'calypso/state/selectors/get-activity-log-filter';
import { filterStateToQuery } from './utils';

import 'calypso/state/data-layer/wpcom/activity-log/rewind/backup';
import 'calypso/state/data-layer/wpcom/activity-log/rewind/downloads';
import 'calypso/state/data-layer/wpcom/activity-log/rewind/restore-status';
import 'calypso/state/data-layer/wpcom/activity-log/rewind/to';
import 'calypso/state/data-layer/wpcom/activity-log/share';
import 'calypso/state/data-layer/wpcom/sites/rewind/downloads';
import 'calypso/state/data-layer/wpcom/sites/rewind/restores';
import 'calypso/state/activity-log/init';

/**
 * Share a rewind/activity-log event via email.
 * @param {string|number} siteId Site ID
 * @param {string|number} rewindId Activity ID
 * @param {string} email Email address to send to
 * @returns {Object} action object
 */
export function rewindShareRequest( siteId, rewindId, email ) {
	return {
		type: REWIND_ACTIVITY_SHARE_REQUEST,
		siteId,
		rewindId,
		email,
	};
}

/**
 * Request a restore to a specific Activity.
 * @param  {string|number} siteId Site ID
 * @param  {number}        activityId Activity ID
 * @returns {Object}        action object
 */
export function rewindRequestRestore( siteId, activityId ) {
	return {
		type: REWIND_RESTORE_REQUEST,
		siteId,
		activityId,
	};
}

/**
 * Dismiss a restore request.
 * @param  {string|number} siteId Site ID
 * @returns {Object}        action object
 */
export function rewindRequestDismiss( siteId ) {
	return {
		type: REWIND_RESTORE_DISMISS,
		siteId,
	};
}

/**
 * Restore a site to the given timestamp.
 * @param {string|number} siteId the site ID
 * @param {string|number} timestamp Unix timestamp to restore site to
 * @param {Object} args Additional request params, such as `types`
 * @returns {Object} action object
 */
export function rewindRestore( siteId, timestamp, args ) {
	return {
		type: REWIND_RESTORE,
		siteId,
		timestamp,
		args,
	};
}

/**
 * Restore specific files and/or database tables to the given timestamp.
 * @param {string|number} siteId the site ID
 * @param {string|number} timestamp Unix timestamp to restore site to
 * @param {string} includePaths List of included files, separated by comma
 * @param {string} excludePaths List of excluded files, separated by comma
 * @returns {Object} action object
 */
export function rewindGranularRestore( siteId, timestamp, includePaths, excludePaths = '' ) {
	return {
		type: REWIND_GRANULAR_RESTORE,
		siteId,
		timestamp,
		includePaths,
		excludePaths,
	};
}

export function rewindClone( siteId, timestamp, payload ) {
	return {
		type: REWIND_CLONE,
		siteId,
		timestamp,
		payload,
	};
}

export function rewindStagingClone( sourceBlogId, timestamp, payload, stagingBlogId ) {
	return {
		type: REWIND_STAGING_CLONE,
		sourceBlogId,
		timestamp,
		payload,
		stagingBlogId,
	};
}

export function dismissRewindRestoreProgress( siteId, restoreId ) {
	return {
		type: REWIND_RESTORE_DISMISS_PROGRESS,
		siteId,
		restoreId,
	};
}

export function getRewindRestoreProgress( siteId, restoreId ) {
	return {
		type: REWIND_RESTORE_PROGRESS_REQUEST,
		siteId,
		restoreId,
	};
}

export function updateRewindRestoreProgress( siteId, timestamp, restoreId, progress ) {
	return {
		type: REWIND_RESTORE_UPDATE_PROGRESS,
		...progress,
		restoreId,
		siteId,
		timestamp,
	};
}

export function setRewindBackupDownloadId( siteId, downloadId ) {
	return {
		type: REWIND_BACKUP_SET_DOWNLOAD_ID,
		siteId,
		downloadId,
	};
}

/**
 * Request a backup up to a specific Activity.
 * @param  {string|number} siteId Site ID
 * @param  {string|number} rewindId Rewind ID
 * @returns {Object}        action object
 */
export function rewindRequestBackup( siteId, rewindId ) {
	return {
		type: REWIND_BACKUP_REQUEST,
		siteId,
		rewindId,
	};
}

export function rewindRequestGranularBackup( siteId, rewindId, includePaths, excludePaths = '' ) {
	return {
		type: REWIND_GRANULAR_BACKUP_REQUEST,
		siteId,
		rewindId,
		includePaths,
		excludePaths,
	};
}

/**
 * Dismiss a backup request.
 * @param  {string|number} siteId Site ID
 * @returns {Object}        action object
 */
export function rewindBackupDismiss( siteId ) {
	return {
		type: REWIND_BACKUP_DISMISS,
		siteId,
	};
}

/**
 * Create a backup of the site up the given rewind id.
 * @param  {string|number} siteId   The site ID
 * @param  {string|number} rewindId Id of activity up to the one the backup will be created.
 * @param  {Object}        args     Additional request params, such as `types`
 * @returns {Object}                 Action object
 */
export function rewindBackup( siteId, rewindId, args ) {
	return {
		type: REWIND_BACKUP,
		siteId,
		rewindId,
		args,
	};
}

/**
 * Check progress of backup creation for the a given download id.
 * @param  {string|number} siteId The site ID
 * @returns {Object}               Action object
 */
export function getRewindBackupProgress( siteId ) {
	return {
		type: REWIND_BACKUP_PROGRESS_REQUEST,
		siteId,
		meta: {
			dataLayer: {
				trackRequest: true,
			},
		},
	};
}

/**
 * Update the status of the backup creation with its progress.
 * @param  {string|number} siteId     The site ID
 * @param  {?number}        downloadId Id of the backup being created.
 * @param  {?number}        progress   Number from 0 to 100 that indicates the progress of the backup creation.
 * @returns {Object}                   Action object
 */
export function updateRewindBackupProgress( siteId, downloadId, progress ) {
	return {
		type: REWIND_BACKUP_UPDATE_PROGRESS,
		...progress,
		downloadId,
		siteId,
	};
}

/**
 * Update the status of the backup creation when it errors.
 * @param  {string|number} siteId     The site ID
 * @param  {number}        downloadId Id of the backup being created.
 * @param  {Object}        error      Info about downloadable backup and error.
 * @returns {Object}                   Action object
 */
export function rewindBackupUpdateError( siteId, downloadId, error ) {
	return {
		type: REWIND_BACKUP_UPDATE_ERROR,
		siteId,
		downloadId,
		...error,
	};
}

/**
 * Remove success banner.
 * @param  {string|number} siteId     The site ID
 * @param  {number}        downloadId Id of the backup being dismissed.
 * @returns {Object}                   Action object
 */
export function dismissRewindBackupProgress( siteId, downloadId ) {
	return {
		type: REWIND_BACKUP_DISMISS_PROGRESS,
		siteId,
		downloadId,
	};
}

/**
 * Enqueue a new backup of a site
 * @param  {string|number} siteId   The site ID
 * @returns {Object}                 Action object
 */
export function rewindBackupSite( siteId ) {
	return {
		type: REWIND_BACKUP_SITE,
		siteId,
	};
}

function navigateToFilter( filter ) {
	const { pathname, hash } = window.location;

	if ( ! pathname.startsWith( '/activity-log/' ) && ! pathname.startsWith( '/backup/activity/' ) ) {
		return;
	}

	page( addQueryArgs( filterStateToQuery( filter ), pathname + hash ) );
}

export const setFilter =
	( siteId, filter, skipUrlUpdate = false ) =>
	( dispatch, getState ) => {
		dispatch( { type: ACTIVITY_LOG_FILTER_SET, siteId, filter } );
		if ( ! skipUrlUpdate ) {
			navigateToFilter( getActivityLogFilter( getState(), siteId ) );
		}
	};

export const updateFilter =
	( siteId, filter, skipUrlUpdate = false ) =>
	( dispatch, getState ) => {
		dispatch( { type: ACTIVITY_LOG_FILTER_UPDATE, siteId, filter } );
		if ( ! skipUrlUpdate ) {
			navigateToFilter( getActivityLogFilter( getState(), siteId ) );
		}
	};