-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
45,270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"presets": [ | ||
"@babel/react", | ||
"@babel/env" | ||
], | ||
"plugins": [ | ||
"react-hot-loader/babel" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import '@babel/polyfill' | ||
import React from 'react' | ||
import { hydrate } from 'react-dom' | ||
import { Provider } from 'react-redux' | ||
import configureStore from '../common/store/configureStore' | ||
import App from '../common/containers/App' | ||
|
||
const { __PRELOADED_STATE__: preloadedState } = window | ||
const store = configureStore(preloadedState) | ||
const rootElement = document.getElementById('app') | ||
|
||
hydrate( | ||
<Provider store={store}> | ||
<App /> | ||
</Provider>, | ||
rootElement, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export const SET_COUNTER = 'SET_COUNTER' | ||
export const INCREMENT_COUNTER = 'INCREMENT_COUNTER' | ||
export const DECREMENT_COUNTER = 'DECREMENT_COUNTER' | ||
|
||
export const set = value => ({ | ||
type: SET_COUNTER, | ||
payload: value, | ||
}) | ||
|
||
export const increment = () => ({ | ||
type: INCREMENT_COUNTER, | ||
}) | ||
|
||
export const decrement = () => ({ | ||
type: DECREMENT_COUNTER, | ||
}) | ||
|
||
export const incrementIfOdd = () => (dispatch, getState) => { | ||
const { counter } = getState() | ||
|
||
if (counter % 2 === 0) { | ||
return | ||
} | ||
|
||
dispatch(increment()) | ||
} | ||
|
||
export const incrementAsync = (delay = 1000) => (dispatch) => { | ||
setTimeout(() => { | ||
dispatch(increment()) | ||
}, delay) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const getRandomInt = (min, max) => ( | ||
Math.floor(Math.random() * (max - min)) + min | ||
) | ||
|
||
export const fetchCounter = (callback) => { | ||
// Rather than immediately returning, we delay our code with a timeout to | ||
// simulate asynchronous behavior | ||
setTimeout(() => { | ||
callback(getRandomInt(1, 100)) | ||
}, 500) | ||
|
||
// In the case of a real world API call, you'll normally run into a Promise | ||
// like this: API.getUser().then(user => callback(user)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
|
||
const Counter = ({ | ||
increment, incrementIfOdd, incrementAsync, decrement, counter, | ||
}) => ( | ||
<p> | ||
Clicked: {counter} times | ||
{' '} | ||
<button onClick={increment}>+</button> | ||
{' '} | ||
<button onClick={decrement}>-</button> | ||
{' '} | ||
<button onClick={incrementIfOdd}>Increment if odd</button> | ||
{' '} | ||
<button onClick={() => incrementAsync()}>Increment async</button> | ||
</p> | ||
) | ||
|
||
Counter.propTypes = { | ||
counter: PropTypes.number.isRequired, | ||
decrement: PropTypes.func.isRequired, | ||
increment: PropTypes.func.isRequired, | ||
incrementAsync: PropTypes.func.isRequired, | ||
incrementIfOdd: PropTypes.func.isRequired, | ||
} | ||
|
||
export default Counter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react' | ||
import { bindActionCreators } from 'redux' | ||
import { MobileScreen, DesktopScreen } from 'react-responsive-redux' | ||
import { connect } from 'react-redux' | ||
import { hot } from 'react-hot-loader' | ||
import Counter from '../components/Counter' | ||
import * as CounterActions from '../actions' | ||
|
||
const mapStateToProps = state => ({ | ||
counter: state.counter, | ||
}) | ||
|
||
const mapDispatchToProps = dispatch => bindActionCreators(CounterActions, dispatch) | ||
const App = (props = {}) => | ||
<div> | ||
<div> | ||
<MobileScreen> | ||
<div>You are a mobile device</div> | ||
</MobileScreen> | ||
<DesktopScreen> | ||
<div>You are a desktop</div> | ||
</DesktopScreen> | ||
</div> | ||
<Counter {...props} /> | ||
</div> | ||
|
||
export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(App)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { SET_COUNTER, INCREMENT_COUNTER, DECREMENT_COUNTER } from '../actions' | ||
|
||
const counter = (state = 0, action) => { | ||
switch (action.type) { | ||
case SET_COUNTER: | ||
return action.payload | ||
case INCREMENT_COUNTER: | ||
return state + 1 | ||
case DECREMENT_COUNTER: | ||
return state - 1 | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
export default counter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { combineReducers } from 'redux' | ||
import { reducer as responsiveReducer } from 'react-responsive-redux' | ||
|
||
import counter from './counter' | ||
|
||
const rootReducer = combineReducers({ | ||
counter, | ||
responsive: responsiveReducer, | ||
}) | ||
|
||
export default rootReducer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { createStore, applyMiddleware, compose} from 'redux' | ||
import thunk from 'redux-thunk' | ||
import rootReducer from '../reducers' | ||
|
||
const composeEnhancers = typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose | ||
|
||
const configureStore = (preloadedState) => { | ||
const store = createStore( | ||
rootReducer, | ||
preloadedState, | ||
composeEnhancers( | ||
applyMiddleware(thunk) | ||
) | ||
) | ||
|
||
if (module.hot) { | ||
// Enable Webpack hot module replacement for reducers | ||
module.hot.accept('../reducers', () => { | ||
store.replaceReducer(rootReducer) | ||
}) | ||
} | ||
|
||
return store | ||
} | ||
|
||
export default configureStore |
Oops, something went wrong.