File size: 3,075 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
import {
	DOMAIN_MANAGEMENT_WHOIS_RECEIVE,
	DOMAIN_MANAGEMENT_WHOIS_REQUEST,
	DOMAIN_MANAGEMENT_WHOIS_REQUEST_FAILURE,
	DOMAIN_MANAGEMENT_WHOIS_REQUEST_SUCCESS,
	DOMAIN_MANAGEMENT_WHOIS_SAVE,
	DOMAIN_MANAGEMENT_WHOIS_SAVE_FAILURE,
	DOMAIN_MANAGEMENT_WHOIS_SAVE_SUCCESS,
	DOMAIN_MANAGEMENT_WHOIS_UPDATE,
} from 'calypso/state/action-types';
import { combineReducers, keyedReducer, withSchemaValidation } from 'calypso/state/utils';
import { whoisType } from '../../../lib/domains/whois/constants';
import { domainWhoisSchema } from './schema';

export const isRequestingWhois = keyedReducer( 'domain', ( state = false, action ) => {
	switch ( action.type ) {
		case DOMAIN_MANAGEMENT_WHOIS_REQUEST:
			return true;
		case DOMAIN_MANAGEMENT_WHOIS_REQUEST_SUCCESS:
		case DOMAIN_MANAGEMENT_WHOIS_REQUEST_FAILURE:
			return false;
	}

	return state;
} );

/**
 * Returns the save request status after an action has been dispatched. The
 * state maps domain to the request status
 * @param  {Object} state  Current state
 * @param  {Object} action Action payload
 * @returns {Object}        Updated state
 */
export const isSaving = ( state = {}, action ) => {
	switch ( action.type ) {
		case DOMAIN_MANAGEMENT_WHOIS_SAVE: {
			const { domain } = action;

			return {
				...state,
				[ domain ]: { saving: true, status: 'pending', error: false },
			};
		}
		case DOMAIN_MANAGEMENT_WHOIS_SAVE_SUCCESS: {
			const { domain } = action;

			return {
				...state,
				[ domain ]: { saving: false, status: 'success', error: false },
			};
		}
		case DOMAIN_MANAGEMENT_WHOIS_SAVE_FAILURE: {
			const { domain, error } = action;

			return {
				...state,
				[ domain ]: { saving: false, status: 'error', error },
			};
		}
	}

	return state;
};

function mergeDomainRegistrantContactDetails( domainState, registrantContactDetails ) {
	return Array.isArray( domainState )
		? domainState.map( ( item ) => {
				if ( item.type === whoisType.REGISTRANT ) {
					return {
						...item,
						...registrantContactDetails,
					};
				}
				return item;
		  } )
		: [
				{
					...registrantContactDetails,
					type: whoisType.REGISTRANT,
				},
		  ];
}

/**
 * Returns the updated items state after an action has been dispatched. The
 * state maps domain to the domain's whoisData object.
 * @param  {Object} state  Current state
 * @param  {Object} action Action payload
 * @returns {Object}        Updated state
 */
export const items = withSchemaValidation( domainWhoisSchema, ( state = {}, action ) => {
	switch ( action.type ) {
		case DOMAIN_MANAGEMENT_WHOIS_RECEIVE: {
			const { domain, whoisData } = action;

			return {
				...state,
				[ domain ]: whoisData,
			};
		}
		case DOMAIN_MANAGEMENT_WHOIS_UPDATE: {
			const { domain, whoisData } = action;
			const domainState = state[ domain ];
			return Object.assign( {}, state, {
				[ domain ]: mergeDomainRegistrantContactDetails(
					domainState !== undefined ? domainState : {},
					whoisData
				),
			} );
		}
	}

	return state;
} );

export default combineReducers( {
	items,
	isRequestingWhois,
	isSaving,
} );