Skip to content

Commit

Permalink
Handling purchases and updating the UI. Fix bug with price in BurgerB…
Browse files Browse the repository at this point in the history
…uilder.
  • Loading branch information
RodrigoTomeES committed Dec 13, 2019
1 parent bb39b14 commit 9a74cee
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 11 deletions.
10 changes: 6 additions & 4 deletions src/containers/BurgerBuilder/BurgerBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Modal from '../../components/UI/Modal/Modal';
import OrderSummary from '../../components/Burger/OrderSummary/OrderSummary';
import Spinner from '../../components/UI/Spinner/Spinner';
import withErrorHandler from '../../hoc/withErrorHandler/withErrorHandler';
import * as burgerBuilderActions from '../../store/actions/index';
import * as actions from '../../store/actions/index';

class BurgerBuilder extends Component {
state = {
Expand Down Expand Up @@ -42,6 +42,7 @@ class BurgerBuilder extends Component {
}

purchaseContinuedHandler = () => {
this.props.onInitPurchase();
this.props.history.push('/checkout');
}

Expand Down Expand Up @@ -97,9 +98,10 @@ const mapStateToProps = state => {

const mapDispatchToProps = dispatch => {
return {
onIngredientAdded: (ingName) => dispatch(burgerBuilderActions.addIngredient(ingName)),
onIngredientRemoved: (ingName) => dispatch(burgerBuilderActions.removeIngredient(ingName)),
onInitIngredients: () => dispatch(burgerBuilderActions.initIngredients())
onIngredientAdded: (ingName) => dispatch(actions.addIngredient(ingName)),
onIngredientRemoved: (ingName) => dispatch(actions.removeIngredient(ingName)),
onInitIngredients: () => dispatch(actions.initIngredients()),
onInitPurchase: () => dispatch(actions.purchaseBurgerInit())
};
};

Expand Down
8 changes: 5 additions & 3 deletions src/containers/Checkout/Checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import CheckoutSmmary from '../../components/Order/CheckoutSummary/CheckoutSumma
import ContactData from './ContactData/ContactData';

class Checkout extends Component {

checkoutCancelledHandler = () => {
this.props.history.goBack();
}
Expand All @@ -16,8 +17,8 @@ class Checkout extends Component {

render() {
let summary = <Redirect to="/" />

if (this.props.ings) {
if (this.props.ings && !this.props.purchased) {
summary = (
<div>
<CheckoutSmmary
Expand All @@ -39,7 +40,8 @@ class Checkout extends Component {

const mapStateToProps = state => {
return {
ings: state.burgerBuilder.ingredients
ings: state.burgerBuilder.ingredients,
purchased: state.order.purchased
};
};

Expand Down
1 change: 1 addition & 0 deletions src/store/actions/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export const FETCH_INGREDIENTS_FAILED = 'FETCH_INGREDIENTS_FAILED';
export const PURCHASE_BURGER_START = 'PURCHASE_BURGER_START';
export const PURCHASE_BURGER_SUCCESS = 'PURCHASE_BURGER_SUCCESS';
export const PURCHASE_BURGER_FAIL = 'PURCHASE_BURGER_FAIL';
export const PURCHASE_BURGER_INIT = 'PURCHASE_INIT';
3 changes: 2 additions & 1 deletion src/store/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export {
} from './burgerBuilder';

export {
purchaseBurger
purchaseBurger,
purchaseBurgerInit
} from './order';
6 changes: 6 additions & 0 deletions src/store/actions/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ export const purchaseBurger = (orderData) => {
});
};
};

export const purchaseBurgerInit = () => {
return {
type: actionTypes.PURCHASE_BURGER_INIT
};
};
6 changes: 4 additions & 2 deletions src/store/reducers/burgerBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ const INGREDIENT_PRICES = {
meat: 1.3
}

const BASE_PRICE = 4;

const initialState = {
ingredients: null,
totalPrice: 5,
totalPrice: BASE_PRICE,
error: false
};

Expand Down Expand Up @@ -53,7 +55,7 @@ const removeIngredient = (state, action, updatedState) => {
}

const setIngredient = (state, action, updatedState) => {
let price = state.totalPrice;
let price = BASE_PRICE;
for (let ingredient in action.ingredients) {
price += INGREDIENT_PRICES[ingredient] * action.ingredients[ingredient];
}
Expand Down
13 changes: 12 additions & 1 deletion src/store/reducers/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import * as actionTypes from '../actions/actionTypes';

const initialState = {
orders: [],
loading: false
loading: false,
purchased: false
}

const reducer = (state = initialState, action) => {
let updatedState = JSON.parse(JSON.stringify(state));

switch ( action.type ) {
case actionTypes.PURCHASE_BURGER_INIT:
updatedState = purchaseBurgerInit(state, action, updatedState);
break;
case actionTypes.PURCHASE_BURGER_START:
updatedState = purchaseBurgerStart(state, action, updatedState);
break;
Expand All @@ -28,6 +32,12 @@ const reducer = (state = initialState, action) => {

export default reducer;

// Auxiliary Functions
const purchaseBurgerInit = (state, action, updatedState) => {
updatedState.purchased = false;
return updatedState;
}

const purchaseBurgerStart = (state, action, updatedState) => {
updatedState.loading = true;
return updatedState;
Expand All @@ -39,6 +49,7 @@ const purchaseBurgerSuccess = (state, action, updatedState) => {
...action.orderData
};
updatedState.loading = false;
updatedState.purchased = true;
updatedState.orders = state.orders.concat(newOrder);
return updatedState;
}
Expand Down

0 comments on commit 9a74cee

Please sign in to comment.