Skip to content

Commit 5ccc68b

Browse files
committed
working on redux convert
1 parent c70f33b commit 5ccc68b

File tree

12 files changed

+509
-80
lines changed

12 files changed

+509
-80
lines changed

.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
}
88
},
99
"rules": {
10-
"react/jsx-filename-extension": ["error", { "extensions": [".js", ".jsx"] }],
10+
"react/jsx-filename-extension": [
11+
"error",
12+
{ "extensions": [".js", ".jsx"] }
13+
],
1114
"global-require": "off",
1215
"no-console": "off",
1316
"import/no-extraneous-dependencies": "off",

src/web/components/QuestionGen.js renamed to src/actions/QuestionGen.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,24 @@ export function genRandomNumber() {
55
export function genRandomOperator() {
66
const randNum = Math.floor(Math.random() * 4);
77

8-
return ["+", "-", "*", "/"][randNum];
8+
return ['+', '-', '*', '/'][randNum];
99
}
1010

1111
export function generateQuestion() {
1212
const randomOperator = genRandomOperator();
1313
let randomNumber1 = genRandomNumber();
1414
let randomNumber2 = genRandomNumber();
15-
let generatedQuestion =
16-
randomNumber1 + " " + randomOperator + " " + randomNumber2;
15+
let generatedQuestion = `${randomNumber1} ${randomOperator} ${randomNumber2}`;
1716

18-
if (randomOperator === "/") {
19-
randomNumber1 = randomNumber1 * randomNumber2;
20-
generatedQuestion =
21-
randomNumber1 + " " + randomOperator + " " + randomNumber2;
17+
if (randomOperator === '/') {
18+
randomNumber1 *= randomNumber2;
19+
generatedQuestion = `${randomNumber1} ${randomOperator} ${randomNumber2}`;
2220
} else {
2321
while (eval(generatedQuestion) < 0) {
2422
randomNumber1 = genRandomNumber();
2523
randomNumber2 = genRandomNumber();
2624
// Don't change the randomOperator in order to maintain operator randomness freqeuncy
27-
generatedQuestion =
28-
randomNumber1 + " " + randomOperator + " " + randomNumber2;
25+
generatedQuestion = `${randomNumber1} ${randomOperator} ${randomNumber2}`;
2926
}
3027
}
3128
return generatedQuestion;

src/actions/game.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { generateQuestion } from './QuestionGen.js';
2+
3+
/**
4+
* Generate New Questions
5+
*/
6+
export function generateQuestions(currentQuestions) {
7+
currentQuestions[0] = generateQuestion();
8+
currentQuestions[1] = currentQuestions[0];
9+
return dispatch => new Promise(resolve => resolve(dispatch({
10+
type: 'GENERATE_NEW_QUESTIONS',
11+
data: currentQuestions,
12+
})));
13+
}
14+
15+
16+
/**
17+
* Set an Error Message
18+
*/
19+
export function setError(message) {
20+
return dispatch => new Promise(resolve => resolve(dispatch({
21+
type: 'RECIPES_ERROR',
22+
data: message,
23+
})));
24+
}

src/containers/Game.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
3+
import { connect } from 'react-redux';
4+
5+
import { login } from '../actions/member';
6+
import { generateQuestions } from '../actions/game';
7+
8+
9+
class Game extends Component {
10+
static propTypes = {
11+
Layout: PropTypes.func.isRequired,
12+
locale: PropTypes.string,
13+
member: PropTypes.shape({}).isRequired,
14+
onFormSubmit: PropTypes.func.isRequired,
15+
isLoading: PropTypes.bool.isRequired,
16+
successMessage: PropTypes.string.isRequired,
17+
answer: PropTypes.number.isRequired,
18+
};
19+
20+
static defaultProps = {
21+
locale: null,
22+
};
23+
24+
state = {
25+
errorMessage: null,
26+
};
27+
28+
onFormSubmit = (data) => {
29+
const { onFormSubmit } = this.props;
30+
return onFormSubmit(data).catch((err) => {
31+
this.setState({ errorMessage: err });
32+
throw err;
33+
});
34+
};
35+
36+
render = () => {
37+
const {
38+
member, locale, Layout, isLoading, successMessage, currentQuestions, answer,
39+
} = this.props;
40+
41+
const { errorMessage } = this.state;
42+
43+
console.log('in the container render currentQuestions', currentQuestions);
44+
45+
return (
46+
<Layout
47+
member={member}
48+
locale={locale}
49+
loading={isLoading}
50+
error={errorMessage}
51+
success={successMessage}
52+
onFormSubmit={this.onFormSubmit}
53+
currentQuestions={currentQuestions}
54+
answer={answer}
55+
generateQuestions={generateQuestions}
56+
/>
57+
);
58+
};
59+
}
60+
61+
const mapStateToProps = state => ({
62+
recipes: state.recipes || {},
63+
member: state.member || {},
64+
locale: state.locale || null,
65+
isLoading: state.status.loading || false,
66+
successMessage: state.status.success || '',
67+
currentQuestions: state.game.currentQuestions,
68+
answer: state.game.answer,
69+
});
70+
71+
const mapDispatchToProps = {
72+
onFormSubmit: login,
73+
generateQuestions,
74+
};
75+
76+
export default connect(
77+
mapStateToProps,
78+
mapDispatchToProps,
79+
)(Game);

src/reducers/game.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Store from '../store/recipes';
2+
3+
export const initialState = Store;
4+
5+
export default function gameReducer(state = initialState, action) {
6+
switch (action.type) {
7+
case 'GENERATE_NEW_QUESTIONS': {
8+
console.log('in the reduce', action.data);
9+
return {
10+
...state,
11+
currentQuestions: action.data,
12+
answer: eval(action.data[0]),
13+
};
14+
}
15+
case 'MEALS_REPLACE': {
16+
return {
17+
...state,
18+
error: null,
19+
loading: false,
20+
meals: action.data,
21+
};
22+
}
23+
case 'RECIPES_ERROR': {
24+
return {
25+
...state,
26+
error: action.data,
27+
};
28+
}
29+
30+
default:
31+
return state;
32+
}
33+
}

src/reducers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import status from './status';
22
import member from './member';
33
import recipes from './recipes';
44
import locale from './locale';
5+
import game from './game';
56

67
const rehydrated = (state = false, action) => {
78
switch (action.type) {
@@ -18,4 +19,5 @@ export default {
1819
member,
1920
recipes,
2021
locale,
22+
game,
2123
};

src/reducers/member.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import Store from '../store/member';
1+
import Store from "../store/member";
22

33
export const initialState = Store;
44

55
export default function userReducer(state = initialState, action) {
66
switch (action.type) {
7-
case 'USER_LOGIN': {
7+
case "USER_LOGIN": {
88
if (action.data) {
99
return {
1010
...state,
1111
loading: false,
1212
error: null,
1313
uid: action.data.uid,
1414
email: action.data.email,
15-
emailVerified: action.data.emailVerified,
15+
emailVerified: action.data.emailVerified
1616
};
1717
}
1818
return initialState;
1919
}
20-
case 'USER_DETAILS_UPDATE': {
20+
case "USER_DETAILS_UPDATE": {
2121
if (action.data) {
2222
return {
2323
...state,
@@ -26,22 +26,22 @@ export default function userReducer(state = initialState, action) {
2626
firstName: action.data.firstName,
2727
lastName: action.data.lastName,
2828
signedUp: action.data.signedUp,
29-
role: action.data.role,
29+
role: action.data.role
3030
};
3131
}
3232
return initialState;
3333
}
34-
case 'USER_ERROR': {
34+
case "USER_ERROR": {
3535
if (action.data) {
3636
return {
3737
...state,
3838
loading: false,
39-
error: action.data,
39+
error: action.data
4040
};
4141
}
4242
return initialState;
4343
}
44-
case 'USER_RESET': {
44+
case "USER_RESET": {
4545
return initialState;
4646
}
4747
default:

src/store/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { persistStore, persistCombineReducers } from 'redux-persist';
44
import storage from 'redux-persist/es/storage'; // default: localStorage if web, AsyncStorage if react-native
55
import thunk from 'redux-thunk';
66
import reducers from '../reducers';
7+
import { generateQuestion } from '../actions/QuestionGen.js';
78

89
// Redux Persist config
910
const config = {
@@ -16,14 +17,27 @@ const reducer = persistCombineReducers(config, reducers);
1617

1718
const middleware = [thunk];
1819

20+
const questions = [];
21+
for (let i = 0; i < 2; i += 1) {
22+
questions.push(generateQuestion());
23+
}
24+
1925
const configureStore = () => {
2026
const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
2127

2228
const store = createStore(
2329
reducer,
30+
{
31+
game: {
32+
currentQuestions: questions,
33+
answer: eval(questions[0]),
34+
},
35+
},
2436
composeEnhancer(applyMiddleware(...middleware)),
2537
);
2638

39+
console.log('created store', store);
40+
2741
const persistor = persistStore(
2842
store,
2943
null,

0 commit comments

Comments
 (0)