forked from afonsopacifer/polymer-redux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.html
61 lines (45 loc) · 1.5 KB
/
store.html
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
<link rel="import" href="bower_components/polymer-redux/polymer-redux.html">
<script>
// Default state
const initialState = {
comments: [
{text: 'Hello Polymer 🦄', id: 0},
{text: 'Hello Redux 😎', id: 1}
]
}
// Reducer
const reducer = (state, action) => {
// Set deafult state
if (!state) return initialState;
// Actions
switch (action.type) {
// ADD_COMMENT
// ------------------------
case 'ADD_COMMENT':
// add new array item
const commentsList = state.comments.slice(0);
const generateId = commentsList.length ? commentsList[commentsList.length - 1].id + 1 : 0
commentsList.push({
text: action.comment,
id: generateId
});
// set new state
return Object.assign({}, state, { comments: commentsList });
break;
// DELETE_COMMENT
// ------------------------
case 'DELETE_COMMENT':
// remove item from array
const newList = state.comments.filter((c) => {
return c.id !== action.id
});
// set new state
return Object.assign({}, state, { comments: newList });
break;
}
}
// Store
const store = Redux.createStore(reducer);
// Create behavior for polymer elements
const ReduxBehavior = PolymerRedux(store);
</script>