File size: 7,163 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
import wp from 'calypso/lib/wp';
import {
	IMPORTS_AUTHORS_SET_MAPPING,
	IMPORTS_AUTHORS_START_MAPPING,
	IMPORTS_IMPORT_CANCEL,
	IMPORTS_IMPORT_LOCK,
	IMPORTS_IMPORT_RECEIVE,
	IMPORTS_IMPORT_RECEIVED_RESET,
	IMPORTS_IMPORT_RESET,
	IMPORTS_IMPORT_START,
	IMPORTS_IMPORT_UNLOCK,
	IMPORTS_START_IMPORTING,
	IMPORTS_UPLOAD_FAILED,
	IMPORTS_PRE_UPLOAD_FAILED,
	IMPORTS_UPLOAD_COMPLETED,
	IMPORTS_UPLOAD_SET_PROGRESS,
	IMPORTS_UPLOAD_START,
} from 'calypso/state/action-types';
import { fromApi, toApi } from './api';
import { appStates } from './constants';
import { isImporterLocked } from './selectors';

import 'calypso/state/imports/init';

const ID_GENERATOR_PREFIX = 'local-generated-id-';

/*
 * The following `order` functions prepare objects that can be
 * sent to the API to accomplish a specific purpose. Instead of
 * actually calling the API, however, they return the _order_,
 * or request object, so that the calling function can send it
 * to the API.
 */

// Creates a request object to cancel an importer
const createCancelOrder = ( siteId, importerId ) =>
	toApi( { importerId, importerState: appStates.CANCEL_PENDING, site: { ID: siteId } } );

// Creates a request to expire an importer session
const createExpiryOrder = ( siteId, importerId ) =>
	toApi( { importerId, importerState: appStates.EXPIRE_PENDING, site: { ID: siteId } } );

// Creates a request to clear all import sessions
const createClearOrder = ( siteId, importerId ) =>
	toApi( { importerId, importerState: appStates.IMPORT_CLEAR, site: { ID: siteId } } );

// Creates a request object to start performing the actual import
const createImportOrder = ( importerStatus ) =>
	toApi( {
		...importerStatus,
		importerState: appStates.IMPORTING,
	} );

const updateImporter = ( siteId, importerStatus ) =>
	wp.req.post( {
		path: `/sites/${ siteId }/imports/${ importerStatus.importerId }`,
		formData: [ [ 'importStatus', JSON.stringify( importerStatus ) ] ],
	} );

export const uploadExportFile = ( siteId, params ) =>
	new Promise( ( resolve, reject ) => {
		const resolver = ( error, data ) => {
			error ? reject( error ) : resolve( data );
		};

		const formData = [
			[ 'importStatus', JSON.stringify( params.importStatus ) ],
			[ 'import', params.file ],
		];

		if ( params.url ) {
			formData.push( [ 'url', params.url ] );
		}

		const req = wp.req.post(
			{
				path: `/sites/${ siteId }/imports/new`,
				formData,
			},
			resolver
		);

		req.upload.onprogress = params.onprogress;
		req.onabort = params.onabort;
	} );

export const lockImport = ( importerId ) => ( {
	type: IMPORTS_IMPORT_LOCK,
	importerId,
} );

export const unlockImport = ( importerId ) => ( {
	type: IMPORTS_IMPORT_UNLOCK,
	importerId,
} );

export const resetImportReceived = () => ( { type: IMPORTS_IMPORT_RECEIVED_RESET } );

export const receiveImporterStatus = ( importerStatus ) => ( dispatch, getState ) => {
	const isLocked = isImporterLocked( getState(), importerStatus.importId );
	dispatch( { type: IMPORTS_IMPORT_RECEIVE, importerStatus, isLocked } );
};

export const cancelImport = ( siteId, importerId ) => async ( dispatch ) => {
	dispatch( lockImport( importerId ) );

	const cancelImportAction = {
		type: IMPORTS_IMPORT_CANCEL,
		importerId,
		siteId,
	};
	dispatch( cancelImportAction );

	// Bail if this is merely a local importer object because
	// there is nothing on the server-side to cancel
	if ( importerId.startsWith( ID_GENERATOR_PREFIX ) ) {
		return;
	}

	const data = await updateImporter( siteId, createCancelOrder( siteId, importerId ) );
	dispatch( receiveImporterStatus( data ) );
};

export const fetchImporterState = ( siteId ) => async ( dispatch ) => {
	const data = await wp.req.get( `/sites/${ siteId }/imports/` );
	dispatch( receiveImporterStatus( data ) );
};

export const finishUpload = ( importerId, importerStatus ) => ( {
	type: IMPORTS_UPLOAD_COMPLETED,
	importerId,
	importerStatus,
} );

export const mapAuthor = ( importerId, sourceAuthor, targetAuthor ) => ( {
	type: IMPORTS_AUTHORS_SET_MAPPING,
	importerId,
	sourceAuthor,
	targetAuthor,
} );

export const resetImport = ( siteId, importerId ) => async ( dispatch ) => {
	// We are done with this import session, so lock it away
	dispatch( lockImport( importerId ) );

	const resetImportAction = {
		type: IMPORTS_IMPORT_RESET,
		importerId,
		siteId,
	};
	dispatch( resetImportAction );

	const data = await updateImporter( siteId, createExpiryOrder( siteId, importerId ) );
	dispatch( receiveImporterStatus( data ) );
};

export const clearImport = ( siteId, importerId ) => async ( dispatch ) => {
	// We are done with this import session, so lock it away
	dispatch( lockImport( importerId ) );

	const resetImportAction = {
		type: IMPORTS_IMPORT_RESET,
		importerId,
		siteId,
	};
	dispatch( resetImportAction );

	const data = await updateImporter( siteId, createClearOrder( siteId, importerId ) );
	dispatch( receiveImporterStatus( data ) );
};

export const startMappingAuthors = ( importerId ) => ( dispatch ) => {
	dispatch( lockImport( importerId ) );

	const startMappingAuthorsAction = {
		type: IMPORTS_AUTHORS_START_MAPPING,
		importerId,
	};
	dispatch( startMappingAuthorsAction );
};

export const setUploadProgress = ( importerId, data ) => ( {
	type: IMPORTS_UPLOAD_SET_PROGRESS,
	uploadLoaded: data.uploadLoaded,
	uploadTotal: data.uploadTotal,
	importerId,
} );

export const startImport = ( siteId, importerType ) => ( {
	type: IMPORTS_IMPORT_START,
	// Use a fake ID until the server returns the real one
	importerId: `${ ID_GENERATOR_PREFIX }${ Math.round( Math.random() * 10000 ) }`,
	importerType,
	siteId,
} );

export const startImporting = ( importerStatus ) => ( dispatch ) => {
	const {
		importerId,
		site: { ID: siteId },
	} = importerStatus;

	dispatch( unlockImport( importerId ) );

	const startImportingAction = {
		type: IMPORTS_START_IMPORTING,
		importerId,
	};
	dispatch( startImportingAction );

	return updateImporter( siteId, createImportOrder( importerStatus ) );
};

export const setUploadStartState = ( importerId, filenameOrUrl ) => ( {
	type: IMPORTS_UPLOAD_START,
	filename: filenameOrUrl,
	importerId,
} );

export const startUpload =
	( importerStatus, file, url = undefined ) =>
	( dispatch ) => {
		const {
			importerId,
			site: { ID: siteId },
		} = importerStatus;

		dispatch( setUploadStartState( importerId, file.name ) );

		return uploadExportFile( siteId, {
			importStatus: toApi( importerStatus ),
			file,
			url,
			onprogress: ( event ) => {
				dispatch(
					setUploadProgress( importerId, {
						uploadLoaded: event.loaded,
						uploadTotal: event.total,
					} )
				);
			},
			onabort: () => {
				dispatch( cancelImport( siteId, importerId ) );
			},
		} )
			.then( ( data ) => {
				dispatch( finishUpload( importerId, fromApi( data ) ) );
			} )
			.catch( ( error ) => {
				const failUploadAction = {
					type: IMPORTS_UPLOAD_FAILED,
					importerId,
					error: error.message,
				};

				dispatch( failUploadAction );
			} );
	};

export const failPreUpload = ( importerId, message, code, file ) => ( {
	type: IMPORTS_PRE_UPLOAD_FAILED,
	importerId,
	error: message,
	errorCode: code,
	file,
} );