Skip to content

Commit 1705922

Browse files
authored
Merge pull request Atyantik#1 from zeel123/master
Implement Redux example for counter
2 parents fbf67cc + 48c2dc6 commit 1705922

File tree

12 files changed

+214
-62
lines changed

12 files changed

+214
-62
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.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.container {
2+
text-align: center;
3+
margin-top: 30px;
4+
}
5+
.title {
6+
font-size: 38px;
7+
}
8+
.btn {
9+
margin: 18px;
10+
background-color: #008CBA;
11+
border: none;
12+
padding: 12px 28px;
13+
color: #ffffff;
14+
font-size: 16px;
15+
font-weight: 800;
16+
}
17+
.value {
18+
font-size: 24px;
19+
font-weight: 900;
20+
}

src/app/components/home/home.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
// import custom CSS
8+
import styles from './home.css';
9+
10+
export default @connect(state => ({
11+
counterValue: _.get(state.counter, 'count', 0),
12+
}))
13+
class Home extends Component {
14+
static propTypes = {
15+
dispatch: PropTypes.func,
16+
counterValue: PropTypes.number,
17+
};
18+
19+
static defaultProps = {
20+
dispatch: () => {},
21+
counterValue: 0,
22+
};
23+
24+
incrementCounter(e) {
25+
const { dispatch } = this.props;
26+
if (e && e.preventDefault) {
27+
e.preventDefault();
28+
}
29+
dispatch(incrementCounter());
30+
}
31+
32+
decrementCounter(e) {
33+
const { dispatch } = this.props;
34+
if (e && e.preventDefault) {
35+
e.preventDefault();
36+
}
37+
dispatch(decrementCounter());
38+
}
39+
40+
render() {
41+
const { counterValue } = this.props;
42+
return (
43+
<div className={styles.container}>
44+
<h1 className={styles.title}>Redux Counter</h1>
45+
<div>
46+
<button type="button" onClick={e => this.incrementCounter(e)} className={styles.btn}>
47+
Increment Counter
48+
</button>
49+
</div>
50+
<div className={styles.value}>
51+
{counterValue}
52+
</div>
53+
<div>
54+
<button type="button" onClick={e => this.decrementCounter(e)} className={styles.btn}>
55+
Decrement Counter
56+
</button>
57+
</div>
58+
</div>
59+
);
60+
}
61+
}

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

0 commit comments

Comments
 (0)