forked from veteransaffairscanada/vac-benefits-directory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
130 lines (120 loc) · 3.41 KB
/
store.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
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
import lunr from "lunr";
import stemmerSupport from "lunr-languages/lunr.stemmer.support.js";
import fr from "lunr-languages/lunr.fr.js";
import { createStore } from "redux";
import airtableConstants from "./utils/airtable_constants";
stemmerSupport(lunr);
fr(lunr);
const initialState = {
enIdx: {},
favouriteBenefits: [],
frIdx: {},
patronType: "",
searchString: "",
selectedNeeds: {},
serviceType: "",
statusAndVitals: "",
serviceHealthIssue: "",
closestAreaOffice: {},
selectedAreaOffice: {},
userLocation: { lat: 49, lng: -104 },
pageWidth: 1000,
mapView: { lat: 49, lng: -104, zoom: 1 }
};
airtableConstants.tableNames.forEach(tableName => {
initialState[tableName] = [];
});
// REDUCERS
export const reducer = (state = initialState, action) => {
let benefits;
let enIdx;
let frIdx;
let newState;
switch (action.type) {
case "INDEX_BENEFITS":
benefits = state.benefits;
enIdx = lunr(function() {
this.ref("id");
this.field("vacNameEn");
this.field("oneLineDescriptionEn");
benefits.forEach(function(doc) {
this.add(doc);
}, this);
});
frIdx = lunr(function() {
this.ref("id");
this.use(lunr.fr);
this.field("vacNameFr");
this.field("oneLineDescriptionFr");
benefits.forEach(function(doc) {
this.add(doc);
}, this);
});
return Object.assign({}, state, {
enIdx: JSON.stringify(enIdx),
frIdx: JSON.stringify(frIdx)
});
case "LOAD_GITHUBDATA":
return Object.assign({}, state, {
githubData: action.data
});
case "LOAD_DATA":
newState = {
storeHydrated: action.data.storeHydrated || state.storeHydrated,
favouriteBenefits:
action.data.favouriteBenefits || state.favouriteBenefits,
timestamp: action.data.timestamp || state.timestamp
};
airtableConstants.tableNames.forEach(tableName => {
newState[tableName] = action.data[tableName] || state[tableName];
});
return Object.assign({}, state, newState);
case "SET_PATRON_TYPE":
return Object.assign({}, state, {
patronType: action.data
});
case "SET_SEARCH_STRING":
return Object.assign({}, state, {
searchString: action.data
});
case "SET_SELECTED_NEEDS":
return Object.assign({}, state, {
selectedNeeds: action.data
});
case "SET_SERVICE_TYPE":
return Object.assign({}, state, {
serviceType: action.data
});
case "SET_STATUS_TYPE":
return Object.assign({}, state, {
statusAndVitals: action.data
});
case "SET_HEALTH_ISSUE":
return Object.assign({}, state, {
serviceHealthIssue: action.data
});
case "SET_CLOSEST_OFFICE":
return Object.assign({}, state, {
closestAreaOffice: action.data
});
case "SET_SELECTED_OFFICE":
return Object.assign({}, state, {
selectedAreaOffice: action.data
});
case "SET_USER_LOCATION":
return Object.assign({}, state, {
userLocation: action.data
});
case "SET_MAP_VIEW":
return Object.assign({}, state, {
mapView: action.data
});
case "SET_PAGEWIDTH":
return Object.assign({}, state, { pageWidth: action.data });
default:
return state;
}
};
export const initStore = (state = initialState) => {
return createStore(reducer, state);
};