forked from veteransaffairscanada/vac-benefits-directory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
119 lines (108 loc) · 3.37 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
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/hardcoded_strings";
stemmerSupport(lunr);
fr(lunr);
const initialState = {
enIdx: {},
favouriteBenefits: [],
frIdx: {},
searchString: "",
selectedNeeds: {},
cookiesDisabled: false,
language: "",
betaFeedback: ""
};
airtableConstants.tableNames.forEach(tableName => {
initialState[tableName] = [];
});
// REDUCERS
export const reducer = (state = initialState, action) => {
let benefits, benefitExamples, language, enIdx, frIdx, newState;
switch (action.type) {
case "INDEX_BENEFITS":
benefits = state.benefits;
benefitExamples = state.benefitExamples;
benefits.forEach(x => {
if (x.benefitExamples) {
const releventExamples = benefitExamples.filter(
y => x.benefitExamples.indexOf(y.id) > -1
);
x.benefitExamplesEn = releventExamples.map(y => y.english).join(" ");
x.benefitExamplesFr = releventExamples.map(y => y.french).join(" ");
}
});
enIdx = lunr(function() {
this.ref("id");
this.field("vacNameEn");
this.field("oneLineDescriptionEn");
this.field("benefitExamplesEn");
benefits.forEach(function(doc) {
this.add(doc);
}, this);
});
frIdx = lunr(function() {
this.ref("id");
this.use(lunr.fr);
this.field("vacNameFr");
this.field("oneLineDescriptionFr");
this.field("benefitExamplesFr");
benefits.forEach(function(doc) {
this.add(doc);
}, this);
});
return Object.assign({}, state, {
enIdx: JSON.stringify(enIdx),
frIdx: JSON.stringify(frIdx)
});
case "LOAD_DATA":
newState = {
favouriteBenefits:
action.data.favouriteBenefits !== undefined
? action.data.favouriteBenefits
: state.favouriteBenefits,
timestamp:
action.data.timestamp !== undefined
? action.data.timestamp
: state.timestamp
};
airtableConstants.tableNames.forEach(tableName => {
newState[tableName] =
action.data[tableName] !== undefined
? action.data[tableName]
: state[tableName];
});
newState["errors"] =
action.data["errors"] !== undefined
? action.data["errors"]
: state["errors"];
return Object.assign({}, state, newState);
case "SAVE_QUESTION_RESPONSE":
return Object.assign({}, state, action.data);
case "SET_LANGUAGE":
language = action.data;
language = language.indexOf("fr") != -1 ? "fr" : "en";
return Object.assign({}, state, { language: language });
case "SET_SEARCH_STRING":
return Object.assign({}, state, {
searchString: action.data
});
case "SET_SELECTED_NEEDS":
return Object.assign({}, state, {
selectedNeeds: action.data
});
case "SET_REFERRER":
return Object.assign({}, state, {
referrer: action.data
});
case "SET_COOKIES_DISABLED":
return Object.assign({}, state, { cookiesDisabled: action.data });
default:
return state;
}
};
export const initStore = (state = initialState) => {
return createStore(reducer, state);
};