-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathreducer.js
78 lines (65 loc) · 1.85 KB
/
reducer.js
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
/**
* External dependencies
*/
import filter from 'lodash/collection/filter';
import React from 'react/addons';
import escapeRegExp from 'lodash/string/escapeRegExp';
/**
* Internal dependencies
*/
import { action as ActionTypes } from 'lib/upgrades/constants';
function updateDomainState( state, domainName, dns ) {
const command = {
[ domainName ]: { $set: dns }
};
return React.addons.update( state, command );
}
function addDns( state, domainName, record ) {
const domainSuffix = new RegExp( '\\.' + escapeRegExp( domainName ) + '\\.$' ),
newRecord = Object.assign( {}, record, {
name: record.name.replace( domainSuffix, '' )
} );
return React.addons.update( state, {
[ domainName ]: { records: { $push: [ newRecord ] } }
} );
}
function deleteDns( state, domainName, record ) {
const command = {},
records = filter( state[ domainName ].records, function( item ) {
return record.id !== item.id || record.name !== item.name || record.data !== item.data || record.type !== item.type;
} );
command[ domainName ] = { records: { $set: records } };
return React.addons.update( state, command );
}
function reducer( state, payload ) {
const { action } = payload;
switch ( action.type ) {
case ActionTypes.DELETING_DNS:
if ( ! action.error ) {
state = deleteDns( state, action.domainName, action.record );
}
break;
case ActionTypes.ADD_DNS:
if ( ! action.error ) {
state = addDns( state, action.domainName, action.record );
}
break;
case ActionTypes.FETCH_DNS:
if ( ! state[ action.domainName ] ) {
state = updateDomainState( state, action.domainName, {
hasLoadedFromServer: false
} );
}
break;
case ActionTypes.RECEIVE_DNS:
state = updateDomainState( state, action.domainName, {
records: action.records,
hasLoadedFromServer: true
} );
break;
}
return state;
}
export {
reducer
};