-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathreducer.ts
50 lines (46 loc) · 1.26 KB
/
reducer.ts
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
import { indexBy, prop } from 'ramda';
import { Action } from 'redux';
import { AnimalList, AnimalType } from '../model';
import { AnimalAPIAction, AnimalAPIActions } from './actions';
const INITIAL_STATE: AnimalList = {
items: {},
loading: false,
error: null,
};
// A higher-order reducer: accepts an animal type and returns a reducer
// that only responds to actions for that particular animal type.
export function createAnimalAPIReducer(animalType: AnimalType) {
return function animalReducer(
state: AnimalList = INITIAL_STATE,
a: Action,
): AnimalList {
const action = a as AnimalAPIAction;
if (!action.meta || action.meta.animalType !== animalType) {
return state;
}
switch (action.type) {
case AnimalAPIActions.LOAD_STARTED:
return {
...state,
items: {},
loading: true,
error: null,
};
case AnimalAPIActions.LOAD_SUCCEEDED:
return {
...state,
items: indexBy(prop('id'), action.payload),
loading: false,
error: null,
};
case AnimalAPIActions.LOAD_FAILED:
return {
...state,
items: {},
loading: false,
error: action.error,
};
}
return state;
};
}