This repository has been archived by the owner on May 10, 2023. It is now read-only.
/
index.js
148 lines (125 loc) · 3.53 KB
/
index.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {
ACTION_LOGOUT,
ACTION_LOGIN_REQUEST,
ACTION_LOGIN_SUCCESS,
ACTION_LOGIN_FAILURE,
ACTION_LOGIN_CHECK_USERNAME_FAILED,
ACTION_LOGIN_ENABLE,
ACTION_LOGIN_DISABLE,
ACTION_LOGIN_CHECK_USERNAME_SUCCESS,
ACTION_ADD_LANGUAGE_REQUEST,
ACTION_ADD_LANGUAGE_SUCCESS,
ACTION_ADD_LANGUAGE_FAILURE,
ACTION_REMOVE_LANGUAGE_REQUEST,
ACTION_REMOVE_LANGUAGE_SUCCESS,
ACTION_REMOVE_LANGUAGE_FAILURE,
ACTION_SUBMIT_SENTENCES_REQUEST,
ACTION_SUBMIT_SENTENCES_SUCCESS,
ACTION_SUBMIT_SENTENCES_FAILURE_SINGLE,
} from '../actions';
import {
ACTION_PARSE_SENTENCES_FAILURE,
} from '../actions/parsing';
export const INITIAL_STATE = {
authed: false,
username: null,
password: null,
loginDisabled: true,
languages: [],
pendingLanguages: false,
sentences: [],
errorMessage: null,
sentenceSubmissionFailures: [],
};
function copyInto(oldObj, newObj) {
return Object.assign({}, oldObj, newObj);
}
function mergeArray(arr1, arr2) {
arr1 && arr1.forEach(item => (arr2.indexOf(item) === -1 && arr2.push(item)));
return arr2;
}
export default function reducer(state = INITIAL_STATE, action) {
switch(action.type) {
case ACTION_LOGOUT:
return copyInto(state, INITIAL_STATE);
case ACTION_LOGIN_CHECK_USERNAME_SUCCESS:
return copyInto(state, {
errorMessage: null,
});
case ACTION_LOGIN_CHECK_USERNAME_FAILED:
return Object.assign({}, state, INITIAL_STATE, {
errorMessage: 'Please only use alphanumeric usernames.',
});
case ACTION_LOGIN_DISABLE:
return copyInto(state, {
loginDisabled: true,
});
case ACTION_LOGIN_ENABLE:
return copyInto(state, {
loginDisabled: false,
});
case ACTION_LOGIN_SUCCESS:
return copyInto(state, {
authed: true,
loginDisabled: false,
username: action.username,
password: action.password,
languages: action.languages,
errorMessage: null,
});
case ACTION_LOGIN_FAILURE:
return Object.assign({}, state, INITIAL_STATE, {
errorMessage: 'Login failed.',
loginDisabled: true,
});
case ACTION_LOGIN_REQUEST:
return copyInto(state, {
authed: false,
loginDisabled: true,
});
case ACTION_ADD_LANGUAGE_REQUEST:
return copyInto(state, {
pendingLanguages: true,
});
case ACTION_ADD_LANGUAGE_SUCCESS:
return copyInto(state, {
pendingLanguages: false,
languages: action.languages,
});
case ACTION_ADD_LANGUAGE_FAILURE:
return copyInto(state, {
pendingLanguages: false,
});
case ACTION_REMOVE_LANGUAGE_REQUEST:
return copyInto(state, {
pendingLanguages: true,
});
case ACTION_REMOVE_LANGUAGE_SUCCESS:
return copyInto(state, {
pendingLanguages: false,
languages: action.languages,
});
case ACTION_REMOVE_LANGUAGE_FAILURE:
return copyInto(state, {
pendingLanguages: false,
});
case ACTION_SUBMIT_SENTENCES_REQUEST:
return copyInto(state, {
sentenceSubmissionFailures: [],
});
case ACTION_SUBMIT_SENTENCES_FAILURE_SINGLE:
return copyInto(state, {
sentenceSubmissionFailures: action.errors,
});
case ACTION_SUBMIT_SENTENCES_SUCCESS:
return copyInto(state, {
sentences: mergeArray(state.sentences, action.sentences),
});
case ACTION_PARSE_SENTENCES_FAILURE:
return copyInto(state, {
errorMessage: action.error.message,
});
default:
return state;
}
}