File size: 1,336 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
import { withStorageKey } from '@automattic/state-utils';
import {
	COUNTRY_STATES_RECEIVE,
	COUNTRY_STATES_REQUEST,
	COUNTRY_STATES_REQUEST_FAILURE,
	COUNTRY_STATES_REQUEST_SUCCESS,
} from 'calypso/state/action-types';
import { combineReducers, withSchemaValidation } from 'calypso/state/utils';
import { itemSchema } from './schema';

// Stores the complete list of states, indexed by locale key
export const items = withSchemaValidation( itemSchema, ( state = {}, action ) => {
	switch ( action.type ) {
		case COUNTRY_STATES_RECEIVE:
			return {
				...state,
				[ action.countryCode ]: action.countryStates,
			};
	}

	return state;
} );

// Tracks states list fetching state
export const isFetching = ( state = {}, action ) => {
	switch ( action.type ) {
		case COUNTRY_STATES_REQUEST: {
			const { countryCode } = action;

			return {
				...state,
				[ countryCode ]: true,
			};
		}
		case COUNTRY_STATES_REQUEST_SUCCESS: {
			const { countryCode } = action;

			return {
				...state,
				[ countryCode ]: false,
			};
		}
		case COUNTRY_STATES_REQUEST_FAILURE: {
			const { countryCode } = action;

			return {
				...state,
				[ countryCode ]: false,
			};
		}
	}

	return state;
};

const combinedReducer = combineReducers( {
	isFetching,
	items,
} );

export default withStorageKey( 'countryStates', combinedReducer );