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
|
import * as R from 'ramda';
import * as actions from '/imports/ui/actions/search-interested-parties';
const defaultState = {
listeners: [],
searchTerm: null,
searchAutoCompleteTerm: null,
searchAutoCompleteFutureId: null
};
function reducer(state = defaultState, action) {
let newListeners;
switch (action.type) {
case actions.ADD_SEARCH_INTERESTED_PARTY:
newListeners = R.unionWith(
R.eqBy(R.prop('action')),
state.listeners,
[{ action: action.payload.listener }]);
return R.assoc('listeners', newListeners, state);
case actions.REMOVE_SEARCH_INTERESTED_PARTY:
newListeners = R.differenceWith(
R.eqBy(R.prop('action')),
state.listeners,
[{ action:action.payload.listener }]);
return R.assoc('listeners', newListeners, state);
case actions.SET_SEARCH_TERM:
asyncCall(() => {
notifyListeners(action.payload.searchTerm, state.listeners);
});
return R.assoc('searchTerm', action.payload.searchTerm, state);
case actions.SET_SEARCH_AUTO_COMPLETE_TERM:
return R.assoc('searchAutoCompleteTerm', action.payload.searchTerm, state);
case actions.RESET_SEARCH_AUTO_COMPLETE_FUTURE:
if (! R.isNil(state.searchAutoCompleteFutureId)) {
clearTimeout(state.searchAutoCompleteFutureId);
}
return R.assoc('searchAutoCompleteFutureId', null, state);
case actions.SET_SEARCH_AUTO_COMPLETE_FUTURE:
if (! R.isNil(state.searchAutoCompleteFutureId)) {
clearTimeout(state.searchAutoCompleteFutureId);
}
return R.assoc('searchAutoCompleteFutureId', action.payload.futureId, state);
default:
return state;
}
}
function asyncCall(fnObject) {
setTimeout(() => {
fnObject.call(null);
}, 0);
}
function notifyListeners(searchTerm, listeners) {
R.forEach((listenerItem) => {
listenerItem.action.call(null, searchTerm);
}, listeners);
}
export const searchInterestedParties = reducer;
|