File size: 6,085 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import update from 'immutability-helper';
import { filter, find, findIndex, matches, pick, reject, some, startsWith, without } from 'lodash';
import {
	DOMAINS_DNS_ADD,
	DOMAINS_DNS_ADD_COMPLETED,
	DOMAINS_DNS_ADD_FAILED,
	DOMAINS_DNS_APPLY_TEMPLATE_COMPLETED,
	DOMAINS_DNS_DELETE,
	DOMAINS_DNS_DELETE_COMPLETED,
	DOMAINS_DNS_DELETE_FAILED,
	DOMAINS_DNS_FETCH,
	DOMAINS_DNS_FETCH_COMPLETED,
	DOMAINS_DNS_FETCH_FAILED,
	DOMAINS_DNS_UPDATE,
	DOMAINS_DNS_UPDATE_COMPLETED,
	DOMAINS_DNS_UPDATE_FAILED,
} from 'calypso/state/action-types';

function isWpcomRecord( record ) {
	return startsWith( record.id, 'wpcom:' );
}

function isRootARecord( domain ) {
	return matches( { type: 'A', name: `${ domain }.` } );
}

function isRootAaaaRecord( domain ) {
	return matches( { type: 'AAAA', name: `${ domain }.` } );
}

function isNsRecord( domain ) {
	return matches( { type: 'NS', name: `${ domain }.` } );
}

function removeDuplicateWpcomRecords( domain, records ) {
	const rootARecords = filter( records, isRootARecord( domain ) );
	const wpcomARecord = find( rootARecords, isWpcomRecord );
	const customARecord = find( rootARecords, ( record ) => ! isWpcomRecord( record ) );
	const customRootAaaaRecords = filter( records, isRootAaaaRecord( domain ) );

	if ( wpcomARecord && ( customARecord || customRootAaaaRecords ) ) {
		return without( records, wpcomARecord );
	}

	return records;
}

function addMissingWpcomRecords( domain, records ) {
	let newRecords = records;

	if ( ! some( records, isRootARecord( domain ) ) ) {
		const defaultRootARecord = {
			domain,
			id: `wpcom:A:${ domain }.:${ domain }`,
			name: `${ domain }.`,
			protected_field: true,
			type: 'A',
		};

		newRecords = newRecords.concat( [ defaultRootARecord ] );
	}

	if ( ! some( records, isNsRecord( domain ) ) ) {
		const defaultNsRecord = {
			domain,
			id: `wpcom:NS:${ domain }.:${ domain }`,
			name: `${ domain }.`,
			protected_field: true,
			type: 'NS',
		};

		newRecords = newRecords.concat( [ defaultNsRecord ] );
	}

	return newRecords;
}

export const initialState = {
	isFetching: false,
	hasLoadedFromServer: false,
	isSubmittingForm: false,
	records: null,
};

function updateDomainState( state, domainName, dns ) {
	const command = {
		[ domainName ]: {
			$set: Object.assign( {}, state[ domainName ] || initialState, dns ),
		},
	};

	return update( state, command );
}

function addDns( state, domainName, record ) {
	const newRecord = Object.assign( {}, record, {
		isBeingAdded: true,
	} );

	return update( state, {
		[ domainName ]: {
			isSubmittingForm: { $set: true },
			records: {
				$apply: ( records ) => {
					const added = records.concat( [ newRecord ] );
					return removeDuplicateWpcomRecords( domainName, added );
				},
			},
		},
	} );
}

function deleteDns( state, domainName, record ) {
	const index = findDnsIndex( state[ domainName ].records, record );

	if ( index === -1 ) {
		return state;
	}

	const command = {
		[ domainName ]: {
			records: {
				$apply: ( records ) => {
					const deleted = reject( records, ( _, current ) => {
						return index === current;
					} );

					return addMissingWpcomRecords( domainName, deleted );
				},
			},
		},
	};

	return update( state, command );
}

function updateDnsState( state, domainName, record, updatedFields ) {
	const index = findDnsIndex( state[ domainName ].records, record );
	const updatedRecord = Object.assign( {}, record, updatedFields );

	if ( index === -1 ) {
		return state;
	}

	const command = {
		[ domainName ]: {
			records: {
				[ index ]: {
					$merge: updatedRecord,
				},
			},
		},
	};

	return update( state, command );
}

function findDnsIndex( records, record ) {
	const matchingFields = pick( record, [ 'id', 'data', 'name', 'type' ] );
	return findIndex( records, matchingFields );
}

export default function reducer( state = {}, action ) {
	switch ( action.type ) {
		case DOMAINS_DNS_FETCH:
			state = updateDomainState( state, action.domainName, {
				isFetching: true,
			} );
			break;
		case DOMAINS_DNS_FETCH_COMPLETED:
			state = updateDomainState( state, action.domainName, {
				records: action.records,
				isFetching: false,
				hasLoadedFromServer: true,
			} );
			break;
		case DOMAINS_DNS_FETCH_FAILED:
			state = updateDomainState( state, action.domainName, {
				isFetching: false,
			} );
			break;
		case DOMAINS_DNS_ADD:
			state = addDns( state, action.domainName, action.record );
			break;
		case DOMAINS_DNS_ADD_COMPLETED:
			state = updateDomainState( state, action.domainName, {
				records: action.records,
				isSubmittingForm: false,
			} );
			state = updateDnsState( state, action.domainName, action.record, {
				isBeingAdded: false,
			} );
			break;
		case DOMAINS_DNS_APPLY_TEMPLATE_COMPLETED:
			state = updateDomainState( state, action.domainName, {
				records: action.records,
				isFetching: false,
				hasLoadedFromServer: true,
			} );
			break;
		case DOMAINS_DNS_ADD_FAILED:
			state = updateDomainState( state, action.domainName, {
				isSubmittingForm: false,
			} );
			state = deleteDns( state, action.domainName, action.record );
			break;
		case DOMAINS_DNS_DELETE:
			state = updateDnsState( state, action.domainName, action.record, {
				isBeingDeleted: true,
			} );
			break;
		case DOMAINS_DNS_DELETE_COMPLETED:
			state = updateDomainState( state, action.domainName, { records: action.records } );
			break;
		case DOMAINS_DNS_DELETE_FAILED:
			state = updateDnsState( state, action.domainName, action.record, {
				isBeingDeleted: false,
			} );
			break;
		case DOMAINS_DNS_UPDATE:
			state = updateDomainState( state, action.domainName, {
				isSubmittingForm: true,
				isFetching: true,
			} );
			break;
		case DOMAINS_DNS_UPDATE_COMPLETED:
			state = updateDomainState( state, action.domainName, {
				records: action.records,
				isFetching: false,
				hasLoadedFromServer: true,
				isSubmittingForm: false,
			} );
			break;
		case DOMAINS_DNS_UPDATE_FAILED:
			state = updateDomainState( state, action.domainName, {
				isFetching: false,
				isSubmittingForm: false,
			} );
			break;
	}

	return state;
}