forked from zoltantothcom/Design-Patterns-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmit.js
More file actions
36 lines (29 loc) · 973 Bytes
/
submit.js
File metadata and controls
36 lines (29 loc) · 973 Bytes
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
import { SUBMIT } from '../static/constants/actions';
import { randomFromRange } from '../helpers/randomFromRange';
export const submitMiddleware = ({ getState }) => next => action => {
if (action.type === SUBMIT) {
const { progress } = getState();
// remove code fields - not necessary in progress.answers
const filteredKeys = ['uuid', 'name', 'type'];
const filtered = filteredKeys.reduce(
(obj, key) => ({ ...obj, [key]: progress.current[key] }),
{}
);
const recentlyAnswered = {
...filtered,
answered: true,
answerId: action.payload,
correct: action.payload === progress.current.uuid
};
const remainingPatterns = progress.remaining.filter(
pattern => pattern.uuid !== progress.current.uuid
);
const currentIndex = randomFromRange(remainingPatterns.length);
action.payload = {
recentlyAnswered,
remainingPatterns,
currentIndex
};
}
next(action);
};