File size: 3,421 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
import { get } from 'lodash';
import { States } from './constants.js';

import 'calypso/state/exporter/init';

export const getExportingState = ( state, siteId ) => {
	const exportingState = state.exporter.exportingState;
	if ( ! exportingState[ siteId ] ) {
		return States.READY;
	}
	return exportingState[ siteId ];
};

/**
 * Indicates whether an export activity is in progress.
 * @param  {Object} state    Global state tree
 * @param  {number} siteId   The ID of the site to check
 * @returns {boolean}         true if activity is in progress
 */
export function shouldShowProgress( state, siteId ) {
	const exportingState = getExportingState( state, siteId );

	return exportingState === States.STARTING || exportingState === States.EXPORTING;
}

/**
 * Indicates whether the export is in progress on the server
 * @param  {Object}  state  Global state tree
 * @param  {number}  siteId The site ID for which to check export progress
 * @returns {boolean}        true if an export is in progress
 */
export function isExporting( state, siteId ) {
	const exportingState = getExportingState( state, siteId );
	return exportingState === States.EXPORTING;
}

export function isDateRangeValid( state, siteId, postType ) {
	const site = state.exporter.selectedAdvancedSettings[ siteId ];
	if ( ! site ) {
		return true;
	}
	const values = site[ postType ];
	if ( ! values ) {
		return true;
	}

	const startDate = values.start_date;
	const endDate = values.end_date;
	if ( startDate && endDate && startDate > endDate ) {
		return false;
	}

	return true;
}

export const getAdvancedSettings = ( state, siteId ) => state.exporter.advancedSettings[ siteId ];
export const getSelectedPostType = ( state ) => state.exporter.selectedPostType;
export const getPostTypeFieldOptions = ( state, siteId, postType, fieldName ) => {
	// Choose which set of options to return for the given field name
	const optionSet = get(
		{
			author: 'authors',
			status: 'statuses',
			start_date: 'dates',
			end_date: 'dates',
			category: 'categories',
		},
		fieldName,
		null
	);

	const advancedSettings = getAdvancedSettings( state, siteId );
	if ( ! advancedSettings ) {
		return null;
	}
	const fields = advancedSettings[ postType ];
	if ( ! fields ) {
		return null;
	}
	return fields[ optionSet ] || null;
};

export const getPostTypeFieldValues = ( state, siteId, postType ) => {
	const site = state.exporter.selectedAdvancedSettings[ siteId ];
	if ( ! site ) {
		return null;
	}
	return site[ postType ] || null;
};

export const getPostTypeFieldValue = ( state, siteId, postType, fieldName ) => {
	const fields = getPostTypeFieldValues( state, siteId, postType );
	if ( ! fields ) {
		return null;
	}
	return fields[ fieldName ] || null;
};

/**
 * Prepare currently selected advanced settings for an /exports/start request
 * @param  {Object} state  Global state tree
 * @param  {number} siteId The ID of the site
 * @returns {Object}        The request body
 */
export function prepareExportRequest( state, siteId, { exportAll = true } = {} ) {
	// Request body is empty if we're just exporting everything
	if ( exportAll ) {
		return null;
	}

	const postType = getSelectedPostType( state );
	const selectedFieldValues = getPostTypeFieldValues( state, siteId, postType );
	return Object.assign( { post_type: postType }, selectedFieldValues );
}

export function getDownloadUrl( state ) {
	return state.exporter.downloadURL;
}