-
Notifications
You must be signed in to change notification settings - Fork 0
/
reducer.js
39 lines (36 loc) · 1001 Bytes
/
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
const { produce } = require('immer');
const initialState = {
user: { id: 1, name: 'Katie', larpIds: [1] },
larps: [],
};
const reducer = produce((state = initialState, action) => {
switch (action.type) {
case 'DELETE_LARP':
state.larps = state.larps.filter(l => l.id !== action.payload);
break;
case 'CREATE_LARP':
state.larps.push(action.payload);
break;
case 'CREATE_CHARACTER':
const id = action.payload.larp_id;
const larpIdx = state.larps.findIndex(l => l.id === id);
state.larps[larpIdx].characters.push({
name: action.payload.name,
});
break;
case 'DELETE_CHARACTER':
state.larps = state.larps.filter(
larp =>
(larp.characters = larp.characters.filter(
character => character.id !== action.payload
))
);
break;
case 'SET_LARPS':
state.larps = action.payload;
break;
default:
return state;
}
});
module.exports = reducer;