Skip to content

Commit 3611018

Browse files
committed
section17ReduxAdvanceCompletion
1 parent 51d3ac3 commit 3611018

File tree

15 files changed

+436
-179
lines changed

15 files changed

+436
-179
lines changed

package-lock.json

Lines changed: 5 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"react-redux": "^7.2.1",
1414
"react-router-dom": "^5.2.0",
1515
"react-scripts": "3.4.1",
16-
"redux": "^4.0.5"
16+
"redux": "^4.0.5",
17+
"redux-thunk": "^2.3.0"
1718
},
1819
"scripts": {
1920
"start": "react-scripts start",

src/containers/BurgerBuilder/BurgerBuilder.js

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,18 @@ import Modal from '../../components/UI/Modal/Modal'
88
import OrderSummary from '../../components/Burger/OrderSummary/OrderSummary'
99
import axios from '../../axios-orders'
1010
import Spinner from '../../components/UI/Spinner/Spinner'
11-
import * as actionTypes from '../../store/actions';
11+
import * as actions from '../../store/actions/index';
1212

1313

1414
class BurgerBuilder extends Component {
1515
state = {
1616
purchasing: false,
17-
loading: false,
18-
error: false
17+
// loading: false,
18+
// error: false
1919
}
2020

2121
componentDidMount() {
22-
// axios.get('https://myburger-72558.firebaseio.com/ingredients.json')
23-
// .then(response => {
24-
// this.setState({ ingredients: response.data });
25-
// })
26-
// .catch(error => {
27-
// this.setState({error: true});
28-
// })
22+
this.props.onInitIngedients();
2923
}
3024

3125
updatePurchaseState(ingredients) {
@@ -36,7 +30,6 @@ class BurgerBuilder extends Component {
3630
.reduce((sum, curVal) => {
3731
return sum = sum + curVal;
3832
}, 0);
39-
debugger;
4033
return sum > 0;
4134
}
4235

@@ -49,7 +42,7 @@ class BurgerBuilder extends Component {
4942
}
5043

5144
purchaseContiueHandler = () => {
52-
45+
this.props.onInitPurchase();
5346
this.props.history.push({
5447
pathname:'/checkout'
5548
});
@@ -62,7 +55,7 @@ class BurgerBuilder extends Component {
6255
for (let key in disabledInfo) {
6356
disabledInfo[key] = disabledInfo[key] <= 0;
6457
}
65-
let burger = this.state.error ? <p>Ingredients can't be loaded</p> : <Spinner/>
58+
let burger = this.props.error ? <p>Ingredients can't be loaded</p> : <Spinner/>
6659
let orderSummary = null;
6760
if(this.props.ings){
6861
burger = (
@@ -86,9 +79,9 @@ class BurgerBuilder extends Component {
8679
continuePurchase={this.purchaseContiueHandler} />;
8780
}
8881

89-
if (this.state.loading) {
90-
orderSummary = <Spinner />;
91-
}
82+
// if (this.state.loading) {
83+
// orderSummary = <Spinner />;
84+
// }
9285

9386
return (
9487
<Aux>
@@ -103,20 +96,17 @@ class BurgerBuilder extends Component {
10396

10497
const mapStateToProps = (state) => {
10598
return {
106-
ings: state.ingredients,
107-
totalSum : state.totalPrice
99+
ings: state.burgerBuilder.ingredients,
100+
totalSum : state.burgerBuilder.totalPrice,
101+
error: state.burgerBuilder.error
108102
}
109103
}
110104
const mapDispatchToProps = dispath => {
111105
return {
112-
onIngredientAdded : (ingName) => dispath({
113-
type: actionTypes.ADD_INGREDIENTS,
114-
ingredientName: ingName
115-
}),
116-
onIngredientRemoved : (ingName) => dispath({
117-
type: actionTypes.REMOVE_INGREDIENTS,
118-
ingredientName: ingName
119-
})
106+
onIngredientAdded : (ingName) => dispath(actions.addIngredient(ingName)),
107+
onIngredientRemoved : (ingName) => dispath(actions.removeIngredient(ingName)),
108+
onInitIngedients: () => dispath(actions.initIngredients()),
109+
onInitPurchase: () => dispath(actions.purchaseInit())
120110
}
121111
}
122112
export default connect(mapStateToProps, mapDispatchToProps)( WithErrorHandler(BurgerBuilder, axios))

src/containers/Checkout/Checkout.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component } from 'react'
2-
import { Route } from 'react-router-dom'
2+
import { Route, Redirect } from 'react-router-dom'
33
import {connect} from 'react-redux'
44
import CheckoutSummary from '../../components/Order/CheckoutSummary/CheckoutSummary'
55
import ContactData from './ContactData/ContactData'
@@ -24,6 +24,7 @@ class Checkout extends Component {
2424
// this.setState({ingredients:ingredients, price: price});
2525
// }
2626

27+
2728
checkoutCancelHandler = () => {
2829
this.props.history.goBack();
2930
}
@@ -32,24 +33,34 @@ class Checkout extends Component {
3233
}
3334

3435
render(){
35-
return(
36-
<div>
37-
<CheckoutSummary
38-
ingredients={this.props.ings}
39-
checkoutCancelled = {this.checkoutCancelHandler}
40-
checkoutContinued = {this.checkoutContinueHandler}
36+
let summary = <Redirect to="/"/>
37+
38+
if (this.props.ings) {
39+
const purchasedRedirect = this.props.purchased ? <Redirect to= "/"/> : null;
40+
summary = (
41+
<div>
42+
{purchasedRedirect}
43+
<CheckoutSummary
44+
ingredients={this.props.ings}
45+
checkoutCancelled={this.checkoutCancelHandler}
46+
checkoutContinued={this.checkoutContinueHandler}
4147
/>
42-
<Route
43-
path={this.props.match.path + '/contact-data'}
44-
component = {ContactData} />
45-
</div>
46-
)
48+
<Route
49+
path={this.props.match.path + '/contact-data'}
50+
component={ContactData} />
51+
52+
</div>);
53+
}
54+
return summary;
4755
}
4856
}
4957

5058
const mapStateToProps = (state) => {
5159
return {
52-
ings: state.ingredients,
60+
ings: state.burgerBuilder.ingredients,
61+
purchased: state.order.purchased
5362
}
5463
}
64+
65+
5566
export default connect(mapStateToProps)(Checkout);

0 commit comments

Comments
 (0)