Skip to content

Commit 761de0e

Browse files
committed
Implement Redux example for counter
1 parent fbf67cc commit 761de0e

File tree

10 files changed

+215
-17
lines changed

10 files changed

+215
-17
lines changed

package-lock.json

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
}
3232
],
3333
"devDependencies": {
34-
"@pawjs/pawjs": "^2.2.3"
34+
"@pawjs/pawjs": "^2.2.3",
35+
"@pawjs/redux": "^2.1.6",
36+
"react-redux": "^5.0.7",
37+
"redux": "^4.0.1"
3538
},
3639
"license": "MIT",
3740
"bugs": {

src/app/components/home.js

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

src/app/components/home/actions.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { INCREMENT_COUNT, DECREMENT_COUNT } from './reducer';
2+
3+
export const incrementCounter = () => ({
4+
type: INCREMENT_COUNT,
5+
});
6+
export const decrementCounter = () => ({
7+
type: DECREMENT_COUNT,
8+
});

src/app/components/home/home.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import React, { Component } from 'react';
2+
import _ from 'lodash';
3+
import PropTypes from 'prop-types';
4+
import { connect } from 'react-redux';
5+
import { decrementCounter, incrementCounter } from './actions';
6+
7+
8+
const styles = {
9+
container: {
10+
textAlign: 'center',
11+
marginTop: '30px',
12+
},
13+
title: {
14+
fontSize: '38px',
15+
},
16+
btn: {
17+
margin: '18px',
18+
backgroundColor: '#008CBA',
19+
border: 'none',
20+
padding: '12px 28px',
21+
color: '#ffffff',
22+
fontSize: '16px',
23+
fontWeight: '800',
24+
},
25+
value: {
26+
fontSize: '24px',
27+
fontWeight: '900',
28+
},
29+
30+
};
31+
32+
export default @connect(state => ({
33+
counterValue: _.get(state.counter, 'count', 0),
34+
}))
35+
class Home extends Component {
36+
static propTypes = {
37+
dispatch: PropTypes.func,
38+
counterValue: PropTypes.number,
39+
};
40+
41+
static defaultProps = {
42+
dispatch: () => {},
43+
counterValue: 0,
44+
};
45+
46+
incrementCounter(e) {
47+
const { dispatch } = this.props;
48+
if (e && e.preventDefault) {
49+
e.preventDefault();
50+
}
51+
dispatch(incrementCounter());
52+
}
53+
54+
decrementCounter(e) {
55+
const { dispatch } = this.props;
56+
if (e && e.preventDefault) {
57+
e.preventDefault();
58+
}
59+
dispatch(decrementCounter());
60+
}
61+
62+
render() {
63+
const { counterValue } = this.props;
64+
return (
65+
<div style={styles.container}>
66+
<h1 style={styles.title}>Redux Counter</h1>
67+
<div>
68+
<button type="button" onClick={e => this.incrementCounter(e)} style={styles.btn}>
69+
Increment Counter
70+
</button>
71+
</div>
72+
<div style={styles.value}>
73+
{counterValue}
74+
</div>
75+
<div>
76+
<button type="button" onClick={e => this.decrementCounter(e)} style={styles.btn}>
77+
Decrement Counter
78+
</button>
79+
</div>
80+
</div>
81+
);
82+
}
83+
}

src/app/components/home/reducer.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import _ from 'lodash';
2+
3+
const initialState = {
4+
count: 5,
5+
};
6+
7+
export const INCREMENT_COUNT = 'INCREMENT_COUNT';
8+
export const DECREMENT_COUNT = 'DECREMENT_COUNT';
9+
10+
export const counter = (state = initialState, action) => {
11+
switch (action.type) {
12+
case INCREMENT_COUNT:
13+
return _.assignIn({}, state, {
14+
count: state.count + 1,
15+
});
16+
case DECREMENT_COUNT:
17+
return _.assignIn({}, state, {
18+
count: state.count - 1,
19+
});
20+
default:
21+
return state;
22+
}
23+
};

src/app/reducers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './components/home/reducer';

src/client.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
1-
export default class Client {}
1+
import ReduxClient from '@pawjs/redux/client';
2+
import * as AppReducers from './app/reducers';
3+
4+
5+
const AppInitialState = {
6+
counter: {
7+
count: 5,
8+
},
9+
};
10+
export default class Client {
11+
constructor({ addPlugin }) {
12+
const reduxClient = new ReduxClient({ addPlugin });
13+
reduxClient.setReducers(AppReducers);
14+
15+
addPlugin(reduxClient);
16+
}
17+
18+
apply(clientHandler) {
19+
clientHandler
20+
.hooks
21+
.reduxInitialState
22+
.tapPromise('ReduxInitialState', async ({ getInitialState, setInitialState }) => {
23+
const initialState = Object.assign({}, getInitialState(), AppInitialState);
24+
setInitialState(initialState);
25+
});
26+
}
27+
}

src/routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default class Routes {
1313
{
1414
path: '/',
1515
exact: true,
16-
component: import('./app/components/home'),
16+
component: import('./app/components/home/home'),
1717
},
1818
];
1919

src/server.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
import React from 'react';
1+
import ReduxServer from '@pawjs/redux/server';
2+
import * as AppReducers from './app/reducers';
3+
4+
const AppInitialState = {
5+
counter: {
6+
count: 5,
7+
},
8+
};
9+
210

311
export default class Server {
12+
constructor({ addPlugin }) {
13+
const reduxServer = new ReduxServer({ addPlugin });
14+
reduxServer.setReducers(AppReducers);
15+
addPlugin(reduxServer);
16+
}
17+
418
apply(serverHandler) {
5-
serverHandler.hooks.beforeHtmlRender.tap('AddFontAwesomeIcons', (Application) => {
6-
Application.htmlProps.head.push(
7-
<script key="Font-Awesome Icons" async src="https://use.fontawesome.com/releases/v5.0.3/js/all.js" />,
8-
);
9-
return Application;
10-
});
19+
serverHandler
20+
.hooks
21+
.reduxInitialState
22+
.tapPromise('AppInitialState', async ({ getInitialState, setInitialState }) => {
23+
const initialState = Object.assign({}, getInitialState(), AppInitialState);
24+
setInitialState(initialState);
25+
});
1126
}
1227
}

0 commit comments

Comments
 (0)