Skip to content

Commit 6c26065

Browse files
authored
Merge pull request #60 from skellock/fix-dom-example
Updates the React DOM example to this century.
2 parents dd8f838 + 83617c6 commit 6c26065

File tree

15 files changed

+100
-128
lines changed

15 files changed

+100
-128
lines changed

examples/ReactDomExample/App/Actions/Creators.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import createAction from './CreateAction'
22
import Types from './Types'
33

44
const startup = () => createAction(Types.STARTUP)
5-
const requestTemperature = (city) => createAction(Types.TEMPERATURE_REQUEST, { city })
6-
const receiveTemperature = (temperature) => createAction(Types.TEMPERATURE_RECEIVE, { temperature })
7-
const receiveTemperatureFailure = () => createAction(Types.TEMPERATURE_FAILURE)
5+
const requestGithub = (city) => createAction(Types.GITHUB_REQUEST, { city })
6+
const receiveGithub = (message) => createAction(Types.GITHUB_RECEIVE, { message })
7+
const receiveGithubFailure = () => createAction(Types.GITHUB_FAILURE)
88

99
/**
1010
Makes available all the action creators we've created.
1111
*/
1212
export default {
1313
startup,
14-
requestTemperature,
15-
receiveTemperature,
16-
receiveTemperatureFailure
14+
requestGithub,
15+
receiveGithub,
16+
receiveGithubFailure
1717
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default {
22
STARTUP: 'STARTUP',
3-
TEMPERATURE_REQUEST: 'TEMPERATURE_REQUEST',
4-
TEMPERATURE_RECEIVE: 'TEMPERATURE_RECEIVE',
5-
TEMPERATURE_FAILURE: 'TEMPERATURE_FAILURE'
3+
GITHUB_REQUEST: 'GITHUB_REQUEST',
4+
GITHUB_RECEIVE: 'GITHUB_RECEIVE',
5+
GITHUB_FAILURE: 'GITHUB_FAILURE'
66
}

examples/ReactDomExample/App/App.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { Component } from 'react'
2-
import Layout from './Layout'
32
import Root from './Root'
43
import { Provider } from 'react-redux'
54
import configureStore from './Store/Store'
@@ -10,9 +9,7 @@ export default class App extends Component {
109
render () {
1110
return (
1211
<Provider store={store}>
13-
<Layout>
14-
<Root />
15-
</Layout>
12+
<Root />
1613
</Provider>
1714
)
1815
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import Reactotron from '../../client' // in a real app, you would use 'reactotron'
22

33
Reactotron.connect({
4-
enabled: true,
54
name: 'ReactDomExample'
65
})
76

7+
// a little easier (albiet ghetto) way to make Reactotron available in other
8+
// places wihout having to import it all the time.
9+
console.tron = Reactotron

examples/ReactDomExample/App/Layout.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

examples/ReactDomExample/App/Reducers/WeatherReducer.js renamed to examples/ReactDomExample/App/Reducers/GithubReducer.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,33 @@ import Immutable from 'seamless-immutable'
33
import createReducer from './CreateReducer'
44

55
export const INITIAL_STATE = Immutable({
6-
temperature: null,
6+
message: null,
77
fetching: false,
8-
error: null,
9-
city: null
8+
error: null
109
})
1110

12-
// request temp
1311
const request = (state, action) =>
14-
state.merge({
15-
fetching: true,
16-
city: action.city,
17-
temperature: null
18-
})
12+
state.merge({ fetching: true, message: null, error: null })
1913

20-
// receive temp
2114
const receive = (state, action) =>
2215
state.merge({
2316
fetching: false,
2417
error: null,
25-
temperature: action.temperature
18+
message: action.message
2619
})
2720

28-
// temp failure
2921
const failure = (state, action) =>
3022
state.merge({
3123
fetching: false,
3224
error: true,
33-
temperature: null
25+
message: null
3426
})
3527

3628
// map our types to our handlers
3729
const ACTION_HANDLERS = {
38-
[Types.TEMPERATURE_REQUEST]: request,
39-
[Types.TEMPERATURE_RECEIVE]: receive,
40-
[Types.TEMPERATURE_FAILURE]: failure
30+
[Types.GITHUB_REQUEST]: request,
31+
[Types.GITHUB_RECEIVE]: receive,
32+
[Types.GITHUB_FAILURE]: failure
4133
}
4234

4335
export default createReducer(INITIAL_STATE, ACTION_HANDLERS)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { combineReducers } from 'redux'
2-
import WeatherReducer from './WeatherReducer'
2+
import GithubReducer from './GithubReducer'
33

44
// glue all the reducers together into 1 root reducer
55
export default combineReducers({
6-
weather: WeatherReducer
6+
github: GithubReducer
77
})

examples/ReactDomExample/App/Root.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import React, { Component, PropTypes } from 'react'
22
import { connect } from 'react-redux'
33
import Actions from './Actions/Creators'
4-
import Reactotron from '../client'
54

6-
export default class RootContainer extends Component {
5+
class RootContainer extends Component {
76

87
constructor (props) {
98
super(props)
@@ -13,23 +12,22 @@ export default class RootContainer extends Component {
1312
handlePress (e) {
1413
e.preventDefault()
1514
const {dispatch} = this.props
16-
Reactotron.log('A touchable was pressed.')
17-
dispatch(Actions.requestTemperature('Toronto'))
15+
console.tron.log('A touchable was pressed. 🦄')
16+
dispatch(Actions.requestGithub())
1817
}
1918

20-
componentWillMount () {
19+
componentDidMount () {
2120
const { dispatch } = this.props
2221
dispatch(Actions.startup())
2322
}
2423

2524
render () {
26-
const {city, temperature, fetching} = this.props
25+
const {message} = this.props
2726
return (
2827
<div style={Styles.container}>
2928
<a onClick={this.handlePress}>
30-
<p style={Styles.weather}>
31-
{`The weather in ${city} is ${fetching ? 'loading' : temperature}.`}
32-
</p>
29+
<h3>Last Commit Message</h3>
30+
<p style={Styles.message}>{message}</p>
3331
</a>
3432
</div>
3533
)
@@ -44,7 +42,7 @@ const Styles = {
4442
justifyContent: 'center',
4543
alignItems: 'center'
4644
},
47-
weather: {
45+
message: {
4846
fontFamily: 'sans-serif',
4947
fontSize: '25px',
5048
textAlign: 'center'
@@ -53,16 +51,12 @@ const Styles = {
5351

5452
RootContainer.propTypes = {
5553
dispatch: PropTypes.func,
56-
city: PropTypes.string,
57-
temperature: PropTypes.number,
58-
fetching: PropTypes.bool
54+
message: PropTypes.string
5955
}
6056

6157
const mapStateToProps = (state) => {
6258
return {
63-
city: state.weather.city,
64-
temperature: state.weather.temperature,
65-
fetching: state.weather.fetching
59+
message: state.github.message
6660
}
6761
}
6862

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { take, call, put } from 'redux-saga/effects'
2+
import RS from 'ramdasauce'
3+
import Types from '../Actions/Types'
4+
import Actions from '../Actions/Creators'
5+
6+
export function * getGithub (api) {
7+
// make the call to github
8+
const response = yield call(api.get, '/repos/skellock/reactotron/commits')
9+
10+
// are we good?
11+
if (response.ok) {
12+
const message = RS.dotPath('data.0.commit.message', response)
13+
// record the last commit's message
14+
yield put(Actions.receiveGithub(message))
15+
} else {
16+
yield put(Actions.receiveGithubFailure())
17+
}
18+
}
19+
20+
export default (api) => {
21+
function * watchGithubRequest () {
22+
while (true) {
23+
yield take(Types.GITHUB_REQUEST)
24+
yield call(getGithub, api)
25+
}
26+
}
27+
28+
return {
29+
watchGithubRequest
30+
}
31+
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
import { take, put, select } from 'redux-saga/effects'
1+
import { take, put } from 'redux-saga/effects'
22
import Types from '../Actions/Types'
33
import Actions from '../Actions/Creators'
4-
import R from 'ramda'
54

65
// process STARTUP actions
76
export function * watchStartup () {
87
while (true) {
98
yield take(Types.STARTUP)
10-
const temp = yield select((state) => state.weather.temperature)
11-
// only fetch new temps when we don't have one yet
12-
if (!R.is(Number, temp)) {
13-
yield put(Actions.requestTemperature('San Francisco'))
14-
}
9+
yield put(Actions.requestGithub())
1510
}
1611
}

0 commit comments

Comments
 (0)