forked from larkintuckerllc/redux-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimmutable.js
123 lines (122 loc) · 3.43 KB
/
immutable.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
/* eslint no-console: "off" */
// import { createStore, combineReducers } from 'redux';
import { createStore } from 'redux';
import { combineReducers } from 'redux-immutable';
import { normalize, schema } from 'normalizr';
import { createSelector } from 'reselect';
import { List, Map } from 'immutable';
const itemSchema = new schema.Entity('items');
const itemsSchema = new schema.Array(itemSchema);
// REDUCERS
const byId = ( /* state = {} */ state = Map({}), action) => {
switch (action.type) {
case 'FETCH':
case 'ADD':
case 'UPDATE':
/*
return {
...state,
...action.value.entities.items,
};
*/
return state.merge(action.value.entities.items);
case 'REMOVE':
/*
const newState = { ...state };
delete newState[action.value.result];
return newState;
*/
return state.delete(action.value.result);
default:
return state;
}
};
const ids = ( /* state = [] */ state = List([]), action) => {
switch (action.type) {
case 'FETCH':
// return action.value.result;
return List(action.value.result);
case 'ADD':
// return [...state, action.value.result];
return state.push(action.value.result);
case 'REMOVE':
/*
const newState = [...state];
newState.splice(state.indexOf(action.value.result), 1);
return newState;
*/
return state.delete(state.indexOf(action.value.result));
default:
return state;
}
};
const myReducer = combineReducers({
byId,
ids,
});
// SELECTORS
// const getItem = (state, id) => state.byId[id];
const getItem = (state, id) => state.get('byId').get(id);
// const getItemsIds = state => state.ids;
const getItemsIds = state => state.get('ids');
// const getItemsById = state => state.byId;
const getItemsById = state => state.get('byId');
const getItems = createSelector(
[getItemsIds, getItemsById],
// (itemsIds, itemsById) => itemsIds.map(id => itemsById[id]),
(itemsIds, itemsById) => itemsIds.map(id => itemsById.get(id)),
);
// ACTION CREATORS
const fetch = items => ({
type: 'FETCH',
value: normalize(items, itemsSchema),
});
const update = item => ({
type: 'UPDATE',
value: normalize(item, itemSchema),
});
// STORE
const store = createStore(myReducer);
let state = store.getState();
let lastItems = getItems(state);
store.subscribe(() => {
const newState = store.getState();
const newItems = getItems(newState);
console.log(newItems === lastItems);
lastItems = newItems;
});
// EXERCISING - FETCH
store.dispatch(fetch(
[{
id: 'm',
name: 'mango',
description: 'Sweet and sticky',
}, {
id: 'n',
name: 'nectarine',
description: 'Crunchy goodness',
}],
));
// EXERCISING - OUTPUT CURRENT VALUE
state = store.getState();
let mango = getItem(state, 'm');
console.log('BEFORE UPDATE ACTION');
console.log(mango);
// EXERCISING - UPDATE PROPER
// mango.description = 'Sweet and super sticky';
mango = mango.set('description','Sweet and super sticky');
// store.dispatch(update(mango));
store.dispatch(update(mango.toJS()));
// EXERCISING - OUTPUT CURRENT VALUE
state = store.getState();
mango = getItem(state, 'm');
console.log('AFTER UPDATE ACTION');
console.log(mango);
// EXERCISING - UPDATE IMPROPER
// mango.description = 'Unripe and sour';
mango = mango.set('description','Unripe and sour');
// EXERCISING - OUTPUT CURRENT VALUE
state = store.getState();
mango = getItem(state, 'm');
console.log('AFTER IMPROPER UPDATE');
console.log(mango);