File size: 4,130 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
import wpcom from 'calypso/lib/wp';
import {
	EXPORT_ADVANCED_SETTINGS_FETCH,
	EXPORT_ADVANCED_SETTINGS_FETCH_FAIL,
	EXPORT_ADVANCED_SETTINGS_RECEIVE,
	EXPORT_CLEAR,
	EXPORT_COMPLETE,
	EXPORT_FAILURE,
	EXPORT_START_REQUEST,
	EXPORT_STARTED,
	EXPORT_STATUS_FETCH,
	EXPORT_POST_TYPE_SET,
	EXPORT_POST_TYPE_FIELD_SET,
	EXPORT_MEDIA_REQUEST,
	SET_MEDIA_EXPORT_DATA,
} from 'calypso/state/action-types';
import { prepareExportRequest } from './selectors';

import 'calypso/state/data-layer/wpcom/sites/exports/media';
import 'calypso/state/exporter/init';

/**
 * Sets the post type to export.
 * @param  {Object} postType   The name of the post type to use - 'posts', 'pages', 'feedback', or null for all
 * @returns {Object}            Action object
 */
export function setPostType( postType ) {
	return {
		type: EXPORT_POST_TYPE_SET,
		postType,
	};
}

export function setPostTypeFieldValue( siteId, postType, fieldName, value ) {
	return {
		type: EXPORT_POST_TYPE_FIELD_SET,
		siteId,
		postType,
		fieldName,
		value,
	};
}

/**
 * Fetches the available advanced settings for customizing export content
 * @param {number} siteId The ID of the site to fetch
 * @returns {Function}        An action thunk for fetching the advanced settings
 */
export function advancedSettingsFetch( siteId ) {
	return ( dispatch, getState ) => {
		if ( siteId === null || typeof siteId === 'undefined' ) {
			return;
		}

		if ( getState().exporter.fetchingAdvancedSettings[ siteId ] === true ) {
			return;
		}

		dispatch( {
			type: EXPORT_ADVANCED_SETTINGS_FETCH,
			siteId,
		} );

		const updateExportSettings = ( settings ) =>
			dispatch( advancedSettingsReceive( siteId, settings ) );

		const fetchFail = ( error ) => dispatch( advancedSettingsFail( siteId, error ) );

		return wpcom.req
			.get( `/sites/${ siteId }/exports/settings` )
			.then( updateExportSettings )
			.catch( fetchFail );
	};
}

export function advancedSettingsReceive( siteId, advancedSettings ) {
	return {
		type: EXPORT_ADVANCED_SETTINGS_RECEIVE,
		siteId,
		advancedSettings,
	};
}

export function advancedSettingsFail( siteId, error ) {
	return {
		type: EXPORT_ADVANCED_SETTINGS_FETCH_FAIL,
		siteId,
		error,
	};
}

/**
 * Sends a request to the server to start an export.
 * @param  {number}   siteId  The ID of the site to export
 * @returns {Function}         Action thunk
 */
export function startExport( siteId, { exportAll = true } = {} ) {
	return ( dispatch, getState ) => {
		if ( ! siteId ) {
			return;
		}

		dispatch( {
			type: EXPORT_START_REQUEST,
			siteId,
			exportAll,
		} );

		const advancedSettings = prepareExportRequest( getState(), siteId, { exportAll } );

		const success = () => dispatch( exportStarted( siteId ) );

		const failure = ( error ) => dispatch( exportFailed( siteId, error ) );

		return wpcom.req
			.post( `/sites/${ siteId }/exports/start`, advancedSettings )
			.then( success )
			.catch( failure );
	};
}

export function exportStarted( siteId ) {
	return {
		type: EXPORT_STARTED,
		siteId,
	};
}

export function exportStatusFetch( siteId ) {
	return ( dispatch ) => {
		dispatch( {
			type: EXPORT_STATUS_FETCH,
			siteId,
		} );

		const failure = ( error ) => {
			dispatch( exportFailed( siteId, error ) );
		};

		const success = ( response = {} ) => {
			switch ( response.status ) {
				case 'finished':
					return dispatch( exportComplete( siteId, response.attachment_url ) );
				case 'running':
					return;
			}

			return failure( response );
		};

		return wpcom.req.get( `/sites/${ siteId }/exports/0` ).then( success ).catch( failure );
	};
}

export function exportFailed( siteId, error ) {
	return {
		type: EXPORT_FAILURE,
		siteId,
		error,
	};
}

export function exportComplete( siteId, downloadURL ) {
	return {
		type: EXPORT_COMPLETE,
		siteId,
		downloadURL,
	};
}

export function clearExport( siteId ) {
	return {
		type: EXPORT_CLEAR,
		siteId,
	};
}

export function requestMediaExport( siteId ) {
	return {
		type: EXPORT_MEDIA_REQUEST,
		siteId,
	};
}

export function setMediaExportData( mediaExportUrl ) {
	return {
		type: SET_MEDIA_EXPORT_DATA,
		mediaExportUrl,
	};
}