Skip to content

Commit

Permalink
Async Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
15Dkatz committed Oct 23, 2022
1 parent bde201f commit 59f57fb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
8 changes: 7 additions & 1 deletion evens-or-odds/src/actions/deck.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ export const fetchDeckResult = deckJson => {
const { remaining, deck_id } = deckJson;

return { type: FETCH_DECK_RESULT, remaining, deck_id };
}
}

export const fetchNewDeck = dispatch => {
return fetch('https://deck-of-cards-api-wrapper.appspot.com/deck/new/shuffle')
.then(response => response.json())
.then(json => dispatch(fetchDeckResult(json)));
}
30 changes: 17 additions & 13 deletions evens-or-odds/src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { startGame, cancelGame } from '../actions/settings';
import { fetchDeckResult } from '../actions/deck';
import { fetchNewDeck } from '../actions/deck';
import Instructions from './Instructions';

class App extends Component {
startGame = () => {
this.props.startGame();

fetch('https://deck-of-cards-api-wrapper.appspot.com/deck/new/shuffle')
.then(response => response.json())
.then(json => this.props.fetchDeckResult(json));
this.props.fetchNewDeck();
}

render() {
Expand Down Expand Up @@ -47,14 +44,21 @@ const mapStateToProps = state => {
return { gameStarted: state.gameStarted };
}

const mapDispatchToProps = dispatch => {
return {
startGame: () => dispatch(startGame()),
cancelGame: () => dispatch(cancelGame()),
fetchDeckResult: deckJson => dispatch(fetchDeckResult(deckJson))
};
}
// const mapDispatchToProps = dispatch => {
// return {
// startGame: () => dispatch(startGame()),
// cancelGame: () => dispatch(cancelGame()),
// fetchNewDeck: () => fetchNewDeck(dispatch)
// };
// }

const componentConnector = connect(mapStateToProps, mapDispatchToProps);
const componentConnector = connect(
mapStateToProps,
{
startGame,
cancelGame,
fetchNewDeck
}
);

export default componentConnector(App);

0 comments on commit 59f57fb

Please sign in to comment.