File size: 3,707 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
import { withStorageKey } from '@automattic/state-utils';
import {
	EXPORT_ADVANCED_SETTINGS_FETCH_FAIL,
	EXPORT_ADVANCED_SETTINGS_FETCH,
	EXPORT_ADVANCED_SETTINGS_RECEIVE,
	EXPORT_POST_TYPE_SET,
	EXPORT_POST_TYPE_FIELD_SET,
	EXPORT_CLEAR,
	EXPORT_COMPLETE,
	EXPORT_START_REQUEST,
	EXPORT_STARTED,
	EXPORT_FAILURE,
	SET_MEDIA_EXPORT_DATA,
} from 'calypso/state/action-types';
import { combineReducers } from 'calypso/state/utils';
import { States } from './constants';

export function selectedPostType( state = null, action ) {
	switch ( action.type ) {
		case EXPORT_POST_TYPE_SET:
			return action.postType;
	}
	return state;
}

const postTypeField = ( state = '', action ) => {
	switch ( action.type ) {
		case EXPORT_POST_TYPE_FIELD_SET:
			return Object.assign( {}, state, {
				[ action.fieldName ]: action.value,
			} );
	}
	return state;
};

const postTypes = ( state = { post: {}, page: {} }, action ) => {
	switch ( action.type ) {
		case EXPORT_POST_TYPE_FIELD_SET:
			return Object.assign( {}, state, {
				[ action.postType ]: postTypeField( state[ action.postType ], action ),
			} );
	}
	return state;
};

export function selectedAdvancedSettings( state = {}, action ) {
	switch ( action.type ) {
		case EXPORT_POST_TYPE_FIELD_SET:
			return Object.assign( {}, state, {
				[ action.siteId ]: postTypes( state[ action.siteId ], action ),
			} );
	}
	return state;
}

/**
 * Tracks the state of the exporter for each site ID
 */
export function exportingState( state = {}, { type, siteId } ) {
	switch ( type ) {
		case EXPORT_START_REQUEST:
			return Object.assign( {}, state, {
				[ siteId ]: States.STARTING,
			} );
		case EXPORT_STARTED:
			return Object.assign( {}, state, {
				[ siteId ]: States.EXPORTING,
			} );
		case EXPORT_COMPLETE:
			return Object.assign( {}, state, {
				[ siteId ]: States.COMPLETE,
			} );
		case EXPORT_FAILURE:
			return Object.assign( {}, state, {
				[ siteId ]: States.FAILED,
			} );
		case EXPORT_CLEAR:
			return Object.assign( {}, state, {
				[ siteId ]: States.READY,
			} );
	}
	return state;
}

/**
 * Tracks whether the advanced settings for a site are currently being fetched
 * @param  {Object} state  Current global state tree
 * @param  {Object} action Action payload
 * @returns {Object}        Updated state
 */
export function fetchingAdvancedSettings( state = {}, action ) {
	switch ( action.type ) {
		case EXPORT_ADVANCED_SETTINGS_FETCH:
			return Object.assign( {}, state, {
				[ action.siteId ]: true,
			} );
		case EXPORT_ADVANCED_SETTINGS_FETCH_FAIL:
		case EXPORT_ADVANCED_SETTINGS_RECEIVE:
			return Object.assign( {}, state, {
				[ action.siteId ]: false,
			} );
	}
	return state;
}

/**
 * Tracks available advanced settings for sites.
 * @param  {Object} state  Current global state tree
 * @param  {Object} action Action payload
 * @returns {Object}        Updated state
 */
export function advancedSettings( state = {}, action ) {
	switch ( action.type ) {
		case EXPORT_ADVANCED_SETTINGS_RECEIVE:
			return Object.assign( {}, state, {
				[ action.siteId ]: action.advancedSettings,
			} );
	}
	return state;
}

export function downloadURL( state = null, action ) {
	switch ( action.type ) {
		case EXPORT_COMPLETE:
			return action.downloadURL;
		case EXPORT_CLEAR:
			return null;
	}

	return state;
}

export function mediaExportUrl( state = null, action ) {
	if ( action.type === SET_MEDIA_EXPORT_DATA ) {
		return action.mediaExportUrl;
	}

	return state;
}

const combinedReducer = combineReducers( {
	selectedPostType,
	selectedAdvancedSettings,
	exportingState,
	fetchingAdvancedSettings,
	advancedSettings,
	downloadURL,
	mediaExportUrl,
} );

export default withStorageKey( 'exporter', combinedReducer );