File size: 3,080 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
import { get } from 'lodash';
import {
	SITE_IMPORTER_IMPORT_FAILURE,
	SITE_IMPORTER_IMPORT_RESET,
	SITE_IMPORTER_IMPORT_START,
	SITE_IMPORTER_IMPORT_SUCCESS,
	SITE_IMPORTER_IS_SITE_IMPORTABLE_FAILURE,
	SITE_IMPORTER_IS_SITE_IMPORTABLE_START,
	SITE_IMPORTER_IS_SITE_IMPORTABLE_SUCCESS,
	SITE_IMPORTER_VALIDATION_ERROR_SET,
} from 'calypso/state/action-types';
import { combineReducers } from 'calypso/state/utils';

const DEFAULT_ERROR_STATE = {
	error: false,
	errorMessage: '',
	errorType: null,
};
const DEFAULT_IMPORT_STAGE = 'idle';
const DEFAULT_IMPORT_DATA = {};

const isLoading = ( state = false, action ) => {
	switch ( action.type ) {
		case SITE_IMPORTER_IMPORT_FAILURE:
			return false;
		case SITE_IMPORTER_IMPORT_START:
			return true;
		case SITE_IMPORTER_IMPORT_SUCCESS:
			return false;
		case SITE_IMPORTER_IMPORT_RESET:
			return false;
		case SITE_IMPORTER_IS_SITE_IMPORTABLE_FAILURE:
			return false;
		case SITE_IMPORTER_IS_SITE_IMPORTABLE_START:
			return true;
		case SITE_IMPORTER_IS_SITE_IMPORTABLE_SUCCESS:
			return false;
	}

	return state;
};

const error = ( state = DEFAULT_ERROR_STATE, action ) => {
	switch ( action.type ) {
		case SITE_IMPORTER_IMPORT_SUCCESS:
			return DEFAULT_ERROR_STATE;
		case SITE_IMPORTER_IMPORT_FAILURE: {
			const { message } = action;

			return {
				error: true,
				errorType: 'importError',
				errorMessage: message,
			};
		}
		case SITE_IMPORTER_IMPORT_START:
			return DEFAULT_ERROR_STATE;
		case SITE_IMPORTER_IS_SITE_IMPORTABLE_SUCCESS:
			return DEFAULT_ERROR_STATE;
		case SITE_IMPORTER_IS_SITE_IMPORTABLE_FAILURE: {
			const { message } = action;

			return {
				error: true,
				errorType: 'importError',
				errorMessage: message,
			};
		}
		case SITE_IMPORTER_IS_SITE_IMPORTABLE_START:
			return DEFAULT_ERROR_STATE;
		case SITE_IMPORTER_IMPORT_RESET:
			return DEFAULT_ERROR_STATE;
		case SITE_IMPORTER_VALIDATION_ERROR_SET: {
			const { message } = action;

			return {
				error: true,
				errorType: 'validationError',
				errorMessage: message,
			};
		}
	}

	return state;
};

const importStage = ( state = DEFAULT_IMPORT_STAGE, action ) => {
	switch ( action.type ) {
		case SITE_IMPORTER_IS_SITE_IMPORTABLE_SUCCESS:
			return 'importable';
		case SITE_IMPORTER_IMPORT_RESET:
			return DEFAULT_IMPORT_STAGE;
	}

	return state;
};

const importData = ( state = DEFAULT_IMPORT_DATA, action ) => {
	switch ( action.type ) {
		case SITE_IMPORTER_IS_SITE_IMPORTABLE_SUCCESS: {
			const { response } = action;

			return {
				title: response.site_title,
				supported: response.supported_content,
				unsupported: response.unsupported_content,
				favicon: response.favicon,
				engine: response.engine,
				url: response.url,
			};
		}
	}

	return state;
};

const validatedSiteUrl = ( state = '', action ) => {
	switch ( action.type ) {
		case SITE_IMPORTER_IS_SITE_IMPORTABLE_SUCCESS: {
			const { response } = action;
			return get( response, 'site_url', '' );
		}
	}

	return state;
};

export default combineReducers( {
	isLoading,
	error,
	importStage,
	importData,
	validatedSiteUrl,
} );