forked from jackielii/simplest-redux-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (141 loc) · 3.52 KB
/
index.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import 'babel-polyfill'
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import ReactDOM from 'react-dom'
import { createStore, combineReducers, applyMiddleware } from 'redux'
import { Provider, connect } from 'react-redux'
import createSagaMiddleware from 'redux-saga'
import { call, put, fork, takeEvery } from 'redux-saga/effects'
import request from 'superagent'
// React component
class Counter extends Component {
render() {
const { value, onIncreaseClick, onDecreaseClick } = this.props
return (
<div>
<span>{value}</span>
<button onClick={onIncreaseClick}>Increase</button>
<button onClick={onDecreaseClick}>Decrease</button>
</div>
)
}
}
Counter.propTypes = {
value: PropTypes.number.isRequired,
onIncreaseClick: PropTypes.func.isRequired,
onDecreaseClick: PropTypes.func.isRequired,
}
class Clock extends Component {
render() {
const { time, onClickForTime } = this.props
return(
<div>
<span>{time}</span>
<button onClick={onClickForTime}>GET CURRENT TIME</button>
</div>
)
}
}
Clock.propTypes = {
time: PropTypes.string.isRequired,
onClickForTime: PropTypes.func.isRequired
}
// Counter Action Creater
const increaseAction = { type: 'increase' }
const decreaseAction = { type: 'decrease' }
// Clock Action Creater
const getCurrentTimeAction = { type: 'manage_api' }
// Reducer
function counter(state = { count: 0 }, action) {
const count = state.count
switch (action.type) {
case 'increase':
return { count: count + 1 }
case 'decrease':
return { count: count - 1 }
default:
return state
}
}
function clock(state = { time: 'YYYY-MM-DD' }, action) {
switch (action.type) {
case 'get_current_time':
return { time: action.result.time }
default:
return state
}
}
const reducer = combineReducers({
clock,
counter
})
// Store with Saga
const sagaMiddleware = createSagaMiddleware();
const store = createStore(
reducer,
{},
applyMiddleware(sagaMiddleware)
);
function checkoutSuccess(result) {
return {
type: 'get_current_time',
result
}
}
function* runCurrentTimeRequest(action) {
const response = yield call(request.get, 'http://date.jsontest.com/')
yield put(checkoutSuccess(response.body))
}
function* handleCurrentTime() {
yield takeEvery('manage_api', runCurrentTimeRequest)
}
function* rootSaga() {
yield fork(handleCurrentTime)
}
sagaMiddleware.run(rootSaga)
// Map Redux state to component props
function mapCounterStateToProps(state) {
return {
value: state.counter.count
}
}
// Map Redux actions to component props
function mapCounterDispatchToProps(dispatch) {
return {
onIncreaseClick: () => dispatch(increaseAction),
onDecreaseClick: () => dispatch(decreaseAction)
}
}
// Map Redux state to component props
function mapClockStateToProps(state) {
return {
time: state.clock.time
}
}
// Map Redux actions to component props
function mapClockDispatchToProps(dispatch) {
return {
onClickForTime: () => dispatch(getCurrentTimeAction)
}
}
// Connected Component == Container
const CounterContainer = connect(
mapCounterStateToProps,
mapCounterDispatchToProps
)(Counter)
const ClockContainer = connect(
mapClockStateToProps,
mapClockDispatchToProps
)(Clock)
const App = () => (
<div>
<CounterContainer />
<ClockContainer />
</div>
)
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)