Skip to content

Commit

Permalink
Challenge and Code: Guess Redux Logic and Component
Browse files Browse the repository at this point in the history
  • Loading branch information
15Dkatz committed Oct 24, 2022
1 parent ec34051 commit ad4615f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 1 deletion.
9 changes: 9 additions & 0 deletions evens-or-odds/src/actions/guess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { SET_GUESS } from './types';

export const setGuessEven = () => {
return { type: SET_GUESS, guess: 'even' };
}

export const setGuessOdd = () => {
return { type: SET_GUESS, guess: 'odd' };
}
1 change: 1 addition & 0 deletions evens-or-odds/src/actions/types.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const SET_GAME_STARTED = 'SET_GAME_STARTED';
export const SET_INSTRUCTIONS_EXPANDED = 'SET_INSTRUCTIONS_EXPANDED';
export const SET_GUESS = 'SET_GUESS';

export const DECK = {
FETCH_SUCCESS: 'DECK_FETCH_SUCCESS',
Expand Down
3 changes: 3 additions & 0 deletions evens-or-odds/src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fetchStates from '../reducers/fetchStates';
import Instructions from './Instructions';
import DrawCard from './DrawCard';
import Card from './Card';
import Guess from './Guess';

class App extends Component {
startGame = () => {
Expand Down Expand Up @@ -33,6 +34,8 @@ class App extends Component {
<div>
<h3>The game is on!</h3>
<br />
<Guess />
<br />
<DrawCard />
<hr />
<Card />
Expand Down
27 changes: 27 additions & 0 deletions evens-or-odds/src/components/Guess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import { connect } from 'react-redux';
import { setGuessEven, setGuessOdd } from '../actions/guess';

const Guess = ({ guess, setGuessEven, setGuessOdd}) => {
return (
<div>
<h3>Will it be even or odd?</h3>
<div>
<button
onClick={setGuessEven}
style={guess === 'even' ? { border: '2px solid #43a047' } : null}
>Even</button>
{' '}
<button
onClick={setGuessOdd}
style={guess === 'odd' ? { border: '2px solid #43a047' } : null}
>Odd</button>
</div>
</div>
)
}

export default connect(
({ gameState: { guess }}) => ({ guess }),
{ setGuessEven, setGuessOdd }
)(Guess);
16 changes: 16 additions & 0 deletions evens-or-odds/src/reducers/gameState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { SET_GUESS, SET_GAME_STARTED } from '../actions/types';

const DEFAULT_GAME_STATE = { guess: '' };

const gameStateReducer = (state = DEFAULT_GAME_STATE, action) => {
switch(action.type) {
case SET_GUESS:
return { ...state, guess: action.guess };
case SET_GAME_STARTED:
return DEFAULT_GAME_STATE;
default:
return state;
}
}

export default gameStateReducer;
4 changes: 3 additions & 1 deletion evens-or-odds/src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import settingsReducer from './settings';
import deckReducer from './deck';
import gameStateReducer from './gameState';

export default {
deck: deckReducer,
settings: settingsReducer
settings: settingsReducer,
gameState: gameStateReducer
};

0 comments on commit ad4615f

Please sign in to comment.