File size: 2,143 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
import { withStorageKey } from '@automattic/state-utils';
import {
	AUTOMATED_TRANSFER_ELIGIBILITY_UPDATE as ELIGIBILITY_UPDATE,
	AUTOMATED_TRANSFER_STATUS_SET as SET_STATUS,
	AUTOMATED_TRANSFER_STATUS_REQUEST as REQUEST_STATUS,
	AUTOMATED_TRANSFER_STATUS_REQUEST_FAILURE as REQUEST_STATUS_FAILURE,
} from 'calypso/state/action-types';
import {
	THEME_TRANSFER_INITIATE_REQUEST as INITIATE,
	THEME_TRANSFER_INITIATE_FAILURE as INITIATE_FAILURE,
	THEME_TRANSFER_STATUS_RECEIVE as TRANSFER_UPDATE,
} from 'calypso/state/themes/action-types';
import {
	combineReducers,
	keyedReducer,
	withSchemaValidation,
	withPersistence,
} from 'calypso/state/utils';
import { transferStates } from './constants';
import eligibility from './eligibility/reducer';
import { automatedTransfer as schema } from './schema';

export const status = withPersistence( ( state = null, action ) => {
	switch ( action.type ) {
		case ELIGIBILITY_UPDATE:
			return state || transferStates.INQUIRING;
		case INITIATE:
			return transferStates.START;
		case INITIATE_FAILURE:
			return transferStates.FAILURE;
		case SET_STATUS:
			return action.status;
		case TRANSFER_UPDATE:
			return 'complete' === action.status ? transferStates.COMPLETE : state;
		case REQUEST_STATUS_FAILURE:
			// TODO : [MARKETPLACE] rely on a tangible status from the backend instead of this message
			return action.error === 'An invalid transfer ID was passed.'
				? transferStates.NONE
				: transferStates.REQUEST_FAILURE;
	}

	return state;
} );

export const fetchingStatus = ( state = false, action ) => {
	switch ( action.type ) {
		case REQUEST_STATUS:
			return true;
		case SET_STATUS:
			return false;
		case REQUEST_STATUS_FAILURE:
			return false;

		default:
			return state;
	}
};

export const siteReducer = combineReducers( {
	eligibility,
	status,
	fetchingStatus,
} );

// state is a map of transfer sub-states
// keyed by the associated site id
const validatedReducer = withSchemaValidation( schema, keyedReducer( 'siteId', siteReducer ) );

const automatedTransferReducer = withStorageKey( 'automatedTransfer', validatedReducer );

export default automatedTransferReducer;