diff --git a/src/containers/about/index.js b/src/containers/about/index.js index d8216f5..78a7b49 100644 --- a/src/containers/about/index.js +++ b/src/containers/about/index.js @@ -1,10 +1,10 @@ import React from 'react' const About = () => ( -
-

About Page

-

Did you get here via Redux?

-
-) +
+

About Page

+

Did you get here via Redux?

+
+); export default About diff --git a/src/containers/app/index.js b/src/containers/app/index.js index b4d0a65..e7ceda3 100644 --- a/src/containers/app/index.js +++ b/src/containers/app/index.js @@ -1,20 +1,27 @@ import React from 'react'; -import { Route, Link } from 'react-router-dom' +import {Link, Route} from 'react-router-dom' import Home from '../home' import About from '../about' +import History from '../history' + +export const HOME_PATH = '/'; +export const ABOUT_PATH = '/about'; +export const HISTORY_PATH = '/history'; const App = () => ( -
-
- Home - About -
+
+
+ Home + About + History +
-
- - -
-
-) +
+ + + +
+
+); export default App diff --git a/src/containers/history/index.js b/src/containers/history/index.js new file mode 100644 index 0000000..1e98602 --- /dev/null +++ b/src/containers/history/index.js @@ -0,0 +1,26 @@ +import React from 'react' +import {connect} from "react-redux"; + +const History = props => ( +

History

+ {props.clickHistory.length > 0 && ( + + + + + + {props.clickHistory.map((entry, index) => + ( + + + ))} +
Click onDate time
{entry.name}{entry.dateTime}
+ )} +
+); + +const mapStateToProps = state => ({ + clickHistory: state.counter.clickHistory +}); + +export default connect(mapStateToProps)(History) diff --git a/src/containers/home/index.js b/src/containers/home/index.js index 2384644..b0b03a5 100644 --- a/src/containers/home/index.js +++ b/src/containers/home/index.js @@ -1,48 +1,56 @@ -import React from 'react' -import { push } from 'react-router-redux' -import { bindActionCreators } from 'redux' -import { connect } from 'react-redux' +import React from 'react'; +import {bindActionCreators} from 'redux'; +import {connect} from 'react-redux'; import { - increment, - incrementAsync, - decrement, - decrementAsync -} from '../../modules/counter' + boundAbout, + boundDecrement, + boundDecrementAsync, + boundIncrement, + boundIncrementAsync, + boundReset +} from '../../modules/counter'; +import {withRouter} from 'react-router'; const Home = props => ( -
-

Home

-

Count: {props.count}

+
+

Home

+

Count: {props.count}

-

- - -

+

+ + +

-

- - -

+

+ + +

-

-
-) +

+ +

+ +

+ +

+
+); const mapStateToProps = state => ({ - count: state.counter.count, - isIncrementing: state.counter.isIncrementing, - isDecrementing: state.counter.isDecrementing -}) + count: state.counter.count, + isIncrementing: state.counter.isIncrementing, + isDecrementing: state.counter.isDecrementing +}); const mapDispatchToProps = dispatch => bindActionCreators({ - increment, - incrementAsync, - decrement, - decrementAsync, - changePage: () => push('/about-us') -}, dispatch) - -export default connect( - mapStateToProps, - mapDispatchToProps -)(Home) + increment: boundIncrement, + incrementAsync: boundIncrementAsync, + decrement: boundDecrement, + decrementAsync: boundDecrementAsync, + about: boundAbout, + reset: boundReset +}, dispatch); + +export default withRouter( + connect(mapStateToProps, mapDispatchToProps)(Home) +); diff --git a/src/index.css b/src/index.css index c180748..436dbe4 100644 --- a/src/index.css +++ b/src/index.css @@ -1,15 +1,33 @@ html { - font-size: 100%; + font-size: 100%; } body { - margin: 0; - padding: 0; - font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif; - font-size: 1rem; - line-height: 1.5; + margin: 0; + padding: 0; + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif; + font-size: 1rem; + line-height: 1.5; } button:disabled { - opacity: 0.5; + opacity: 0.5; +} + +a { + margin-right: 15px; +} + +table { + margin-left: 10px; +} + +td, th { + border: 1px solid black; + text-align: left; + padding: 8px; +} + +tr:nth-child(even) { + background-color: #dddddd; } diff --git a/src/index.js b/src/index.js index 69a6603..37ec8e9 100644 --- a/src/index.js +++ b/src/index.js @@ -1,22 +1,19 @@ -import React from 'react' -import { render } from 'react-dom' -import { Provider } from 'react-redux' -import { ConnectedRouter } from 'react-router-redux' -import store, { history } from './store' -import App from './containers/app' - +import React from 'react'; +import {render} from 'react-dom'; +import {Provider} from 'react-redux'; +import {ConnectedRouter} from 'react-router-redux'; +import store, {history} from './store'; +import App from './containers/app'; import 'sanitize.css/sanitize.css' import './index.css' -const target = document.querySelector('#root') +const target = document.getElementById('root'); render( - - -
- -
-
-
, - target -) + + + + + , + target +); diff --git a/src/modules/counter.js b/src/modules/counter.js index d2d14a4..5e4cb8f 100644 --- a/src/modules/counter.js +++ b/src/modules/counter.js @@ -1,95 +1,138 @@ -export const INCREMENT_REQUESTED = 'counter/INCREMENT_REQUESTED' -export const INCREMENT = 'counter/INCREMENT' -export const DECREMENT_REQUESTED = 'counter/DECREMENT_REQUESTED' -export const DECREMENT = 'counter/DECREMENT' +import {push} from 'react-router-redux'; +import {ABOUT_PATH} from '../containers/app/index' -const initialState = { - count: 0, - isIncrementing: false, - isDecrementing: false -} +export const INCREMENT_REQUESTED = 'counter/INCREMENT_REQUESTED'; +export const INCREMENT = 'counter/INCREMENT'; +export const DECREMENT_REQUESTED = 'counter/DECREMENT_REQUESTED'; +export const DECREMENT = 'counter/DECREMENT'; +export const ABOUT = 'counter/ABOUT'; +export const RESET = 'counter/RESET'; -export default (state = initialState, action) => { - switch (action.type) { - case INCREMENT_REQUESTED: - return { - ...state, - isIncrementing: true - } - - case INCREMENT: - return { - ...state, - count: state.count + 1, - isIncrementing: !state.isIncrementing - } - - case DECREMENT_REQUESTED: - return { - ...state, - isDecrementing: true - } - - case DECREMENT: - return { - ...state, - count: state.count - 1, - isDecrementing: !state.isDecrementing - } - - default: - return state - } -} - -export const increment = () => { - return dispatch => { +const + options = { + weekday: 'long', + year: 'numeric', + month: 'long', + day: 'numeric', + timeZone: 'America/New_York' + }, + initialState = { + count: 0, + isIncrementing: false, + isDecrementing: false, + isResetting: false, + clickHistory: [] + }, + addClickHistory = (clickHistory, name) => [...clickHistory, { + dateTime: new Date().toLocaleTimeString('en-US', options), + name: name + }]; + +export const boundIncrement = () => dispatch => { + dispatch({ + type: INCREMENT_REQUESTED + }); + + dispatch({ + type: INCREMENT, + name: 'Increment' + }); +}; + +export const boundIncrementAsync = () => dispatch => { dispatch({ - type: INCREMENT_REQUESTED - }) + type: INCREMENT_REQUESTED + }); + + return setTimeout(() => dispatch({ + type: INCREMENT, + name: 'Increment Async' + }), 2000); +}; +export const boundDecrement = () => dispatch => { dispatch({ - type: INCREMENT - }) - } -} + type: DECREMENT_REQUESTED + }); -export const incrementAsync = () => { - return dispatch => { dispatch({ - type: INCREMENT_REQUESTED - }) - - return setTimeout(() => { - dispatch({ - type: INCREMENT - }) - }, 3000) - } -} - -export const decrement = () => { - return dispatch => { + type: DECREMENT, + name: 'Decrement' + }); +}; + +export const boundDecrementAsync = () => dispatch => { dispatch({ - type: DECREMENT_REQUESTED - }) + type: DECREMENT_REQUESTED + }); + return setTimeout(() => dispatch({ + type: DECREMENT, + name: 'Decrement Async' + }), 2000); +}; + +export const boundAbout = () => dispatch => { dispatch({ - type: DECREMENT - }) - } -} + type: ABOUT, + name: 'Go to about page via redux' + }); + + dispatch(push(ABOUT_PATH)); +}; -export const decrementAsync = () => { - return dispatch => { +export const boundReset = () => dispatch => { dispatch({ - type: DECREMENT_REQUESTED - }) - - return setTimeout(() => { - dispatch({ - type: DECREMENT - }) - }, 3000) - } -} + type: RESET, + name: 'Reset' + }); +}; + +export default (state = initialState, action) => { + + switch (action.type) { + case INCREMENT_REQUESTED: + return { + ...state, + isIncrementing: true + }; + + case INCREMENT: + return { + ...state, + count: state.count + 1, + isIncrementing: !state.isIncrementing, + clickHistory: addClickHistory(state.clickHistory, action.name) + }; + + case DECREMENT_REQUESTED: + return { + ...state, + isDecrementing: true + }; + + case DECREMENT: + return { + ...state, + count: state.count - 1, + isDecrementing: !state.isDecrementing, + clickHistory: addClickHistory(state.clickHistory, action.name) + }; + + case ABOUT: + return { + ...state, + clickHistory: addClickHistory(state.clickHistory, action.name) + }; + + case RESET: + return { + ...state, + count: state.count = 0, + clickHistory: addClickHistory(state.clickHistory, action.name) + }; + + default: + return state + } +}; diff --git a/src/modules/index.js b/src/modules/index.js index b3881fe..a021e0f 100644 --- a/src/modules/index.js +++ b/src/modules/index.js @@ -1,8 +1,8 @@ -import { combineReducers } from 'redux' -import { routerReducer } from 'react-router-redux' -import counter from './counter' +import {combineReducers} from 'redux'; +import {routerReducer} from 'react-router-redux'; +import counter from './counter'; export default combineReducers({ - router: routerReducer, - counter + router: routerReducer, + counter }) diff --git a/src/store.js b/src/store.js index 46dae36..db40893 100644 --- a/src/store.js +++ b/src/store.js @@ -1,33 +1,34 @@ -import { createStore, applyMiddleware, compose } from 'redux' -import { routerMiddleware } from 'react-router-redux' +import {applyMiddleware, compose, createStore} from 'redux' +import {routerMiddleware} from 'react-router-redux' import thunk from 'redux-thunk' import createHistory from 'history/createBrowserHistory' import rootReducer from './modules' -export const history = createHistory() +export const history = createHistory(); -const initialState = {} -const enhancers = [] -const middleware = [ - thunk, - routerMiddleware(history) -] +const + initialState = {}, + enhancers = [], + middleware = [ + thunk, + routerMiddleware(history) + ]; if (process.env.NODE_ENV === 'development') { - const devToolsExtension = window.devToolsExtension + const devToolsExtension = window.devToolsExtension; - if (typeof devToolsExtension === 'function') { - enhancers.push(devToolsExtension()) - } + if (typeof devToolsExtension === 'function') { + enhancers.push(devToolsExtension()) + } } const composedEnhancers = compose( - applyMiddleware(...middleware), - ...enhancers -) + applyMiddleware(...middleware), + ...enhancers +); export default createStore( - rootReducer, - initialState, - composedEnhancers + rootReducer, + initialState, + composedEnhancers )