-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
113 lines (88 loc) · 4.41 KB
/
index.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>pure redux-observable</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.0/redux.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.2.1/rxjs.umd.min.js"></script>
<script src="https://unpkg.com/redux-observable@latest/dist/redux-observable.min.js"></script>
<style>
.item { border: .1px solid #666; }
h4 { margin: 0}
</style>
</head>
<body>
<div id="app"></div>
<script>
console.log('Redux', Redux);
console.log('ReduxObservable', ReduxObservable);
var createStore = Redux.createStore,
applyMiddleware = Redux.applyMiddleware,
createEpicMiddleware = ReduxObservable.createEpicMiddleware;
console.log('createStore\n\n',createStore);
console.log('applyMiddleware\n\n', applyMiddleware);
console.log('createEpicMiddleware\n\n', createEpicMiddleware);
/*------- Actions ---------------------------------------------*/
var GET_BOARDS = '[Boards] GetBoards',
GET_BOARDS_SUCCESS = '[Boards] GetBoardsSuccess';
var GetBoards = () => ({ type : GET_BOARDS }),
GetBoardsSuccess = (boards) => {
console.log('GetBoardsSuccess', boards);
return ({ type : GET_BOARDS_SUCCESS, ...boards })
};
/*------- epic ---------------------------------------------*/
var getBoards = (action$) => action$.pipe(
ReduxObservable.ofType(GET_BOARDS),
rxjs.operators.delay(1000),
rxjs.operators.mergeMap(action => rxjs.ajax.ajax.getJSON('https://cdn.rawgit.com/u4bi/pure-redux-observable/master/model/data.json')),
rxjs.operators.map(v => {
console.log('epic then', v);
return GetBoardsSuccess(v)
})
);
/*------- Reducer ---------------------------------------------*/
var initialState = {
pending: false,
boards : []
},
newState = (state, newData) => Object.assign({}, state, newData);
var boardsReducer = (state = initialState, action) => {
console.log('action', action);
switch (action.type) {
case GET_BOARDS:
return newState(state, { pending: true });
case GET_BOARDS_SUCCESS:
return newState(state, { pending: false, boards : action.boards });
default : state;
}
};
/*------- App ---------------------------------------------*/
var epic = createEpicMiddleware(),
store = createStore(boardsReducer, applyMiddleware(epic)),
element = document.getElementById('app');
store.subscribe( () => {
let data = store.getState();
console.log('Subscribe then\n\n', data, '\n\n');
/*------- View Rendering ---------------------------------------------*/
data.pending ? element.innerHTML = '<h1>Loading</h1>' : [
element.innerHTML = '',
data.boards.reduce((elem, v) => {
let childrenElement = document.createElement('div');
childrenElement.className = 'item'
childrenElement.innerHTML = `
<h1>${ v.title }</h1>
<h4>${ v.id }</h4>
<h4>${ v.writer }</h4>
<h4>${ v.content}</h4>
`;
elem.appendChild(childrenElement);
return elem;
}, element)
];
/*--------------------------------------------------------------------*/
});
epic.run(getBoards), // multiple ? createEpicMiddleware(ReduxObservable.combineEpics(getBoards, getBoards)))
store.dispatch(GetBoards());
</script>
</body>
</html>