-
Notifications
You must be signed in to change notification settings - Fork 51
/
suggestions-reducer.js
126 lines (109 loc) · 3.32 KB
/
suggestions-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
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
import updateObject from 'immutability-helper'
import {
DIFF_SETTING_CHANGED,
PHRASE_SUGGESTION_STARTED_COPYING,
PHRASE_SUGGESTION_FINISHED_COPYING,
PHRASE_SUGGESTIONS_UPDATED,
SET_SUGGESTION_SEARCH_TYPE,
SHOW_DETAIL_FOR_SUGGESTION_BY_INDEX,
SUGGESTION_SEARCH_TEXT_CHANGE,
TEXT_SUGGESTION_STARTED_COPYING,
TEXT_SUGGESTION_FINISHED_COPYING,
TEXT_SUGGESTIONS_UPDATED
} from '../actions/suggestions-action-types'
const defaultState = {
searchType: 'phrase',
showDetailModalForIndex: undefined,
showDiff: true,
textSearch: {
loading: false,
searchStrings: [],
suggestions: [],
timestamp: 0
},
// searchStrings is just the source text that was used for the lookup
// usually won't change, but could if new source is uploaded
// { phraseId: { loading, searchStrings, suggestions, timestamp } }
searchByPhrase: {},
search: {
input: {
text: '',
focused: false
}
}
}
const suggestions = (state = defaultState, action) => {
switch (action.type) {
case DIFF_SETTING_CHANGED:
return update({showDiff: {$set: !state.showDiff}})
// could add action.copying and combine started+finished actions
case PHRASE_SUGGESTION_FINISHED_COPYING:
return update({
searchByPhrase: {
[action.payload.phraseId]: {
suggestions: {
[action.payload.index]: {copying: {$set: false}}
}
}
}
})
case PHRASE_SUGGESTION_STARTED_COPYING:
return update({
searchByPhrase: {
[action.payload.phraseId]: {
suggestions: {
[action.payload.index]: {copying: {$set: true}}
}
}
}
})
case PHRASE_SUGGESTIONS_UPDATED:
return update({
searchByPhrase: {
// must $set a new object because the key may not yet be defined
[action.payload.phraseId]: {$set: {
loading: action.payload.loading,
searchStrings: action.payload.searchStrings,
suggestions: action.payload.suggestions,
timestamp: action.payload.timestamp
}}
}
})
case SET_SUGGESTION_SEARCH_TYPE:
return update({searchType: {$set: action.payload}})
case SHOW_DETAIL_FOR_SUGGESTION_BY_INDEX:
return update({showDetailModalForIndex: {$set: action.index}})
case SUGGESTION_SEARCH_TEXT_CHANGE:
return update({search: {input: {text: {$set: action.payload}}}})
case TEXT_SUGGESTION_FINISHED_COPYING:
return update({
textSearch: {
suggestions: {[action.payload]: {copying: {$set: false}}}
}
})
case TEXT_SUGGESTION_STARTED_COPYING:
return update({
textSearch: {
suggestions: {[action.payload]: {copying: {$set: true}}}
}
})
case TEXT_SUGGESTIONS_UPDATED:
return update({textSearch: {
loading: {$set: action.payload.loading},
searchStrings: {$set: action.payload.searchStrings},
suggestions: {$set: action.payload.suggestions},
timestamp: {$set: action.payload.timestamp}
}})
default:
return state
}
/**
* Apply the given commands to state.
*
* Just a shortcut to avoid having to pass state to update over and over.
*/
function update (commands) {
return updateObject(state, commands)
}
}
export default suggestions