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
|
import * as rulesAPI from '../api/rules';
import invariant from 'invariant';
import { createSelector } from 'reselect';
export const getAllRules = s => s.rules.allRules;
export const getSearchText = s => s.rules.searchText;
export const getRules = createSelector(
getSearchText,
getAllRules,
(searchText, allRules) => {
if (searchText === '') return allRules;
return allRules.filter(r => r.payload.indexOf(searchText) >= 0);
}
);
export function updateSearchText(text) {
return dispatch => {
dispatch('rulesUpdateSearchText', s => {
s.rules.searchText = text.toLowerCase();
});
};
}
export function fetchRules(apiConfig) {
return async dispatch => {
const res = await rulesAPI.fetchRules(apiConfig);
const json = await res.json();
invariant(
json.rules && json.rules.length >= 0,
'there is no valid rules list in the rules API response'
);
// attach an id
const allRules = json.rules.map((r, i) => {
r.id = i;
return r;
});
dispatch('rulesFetchRules', s => {
s.rules.allRules = allRules;
});
};
}
export function fetchRulesOnce(apiConfig) {
return async (dispatch, getState) => {
const allRules = getAllRules(getState());
if (allRules.length === 0) return await dispatch(fetchRules(apiConfig));
};
}
// {"type":"FINAL","payload":"","proxy":"Proxy"}
// {"type":"IPCIDR","payload":"172.16.0.0/12","proxy":"DIRECT"}
export const initialState = {
// filteredRules: [],
allRules: [],
searchText: ''
};
|