diff --git a/src/containers/BurgerBuilder/BurgerBuilder.js b/src/containers/BurgerBuilder/BurgerBuilder.js index febf659..b60d1aa 100644 --- a/src/containers/BurgerBuilder/BurgerBuilder.js +++ b/src/containers/BurgerBuilder/BurgerBuilder.js @@ -19,16 +19,22 @@ const INGREDIENT_PRICE = { class BurgerBuilder extends Component { state = { - ingredients: { - meat: 0, - cheese: 0, - salad: 0, - bacon: 0, - }, + ingredients: null, totalPrice: 0, purchasable: false, purchasing: false, loading: false, + error: false, + } + + componentDidMount() { + axios.get('https://react-my-burger-e4645.firebaseio.com/ingredients.json') // remove .json to throw error + .then(res => { + this.setState({ ingredients: res.data }); + }) + .catch(error => { + this.setState({ error: true}); + }) } updatePurchaseState(ingredients) { @@ -108,7 +114,7 @@ class BurgerBuilder extends Component { } }; - axios.post('/order.json', order) + axios.post('/order.json', order) // remove .json to throw error .then(response => { this.setState({ loading: false, purchasing: false }); // set purchasing: false can close Order Summary dialog }) @@ -126,11 +132,29 @@ class BurgerBuilder extends Component { disabledInfo[key] = disabledInfo[key] <= 0; } - let orderSummary = ; + let orderSummary = null; + let burger = this.state.error ?

Ingredients can't be loaded!

: ; + + if (this.state.ingredients) { + orderSummary = ; + + burger = ( + + + + + ); + } if (this.state.loading) { orderSummary = @@ -143,14 +167,7 @@ class BurgerBuilder extends Component { modalClosed={this.purchaseCancelHandler}> {orderSummary} - - + {burger} ); }; diff --git a/src/hoc/withErrorHandler/withErrorHandler.js b/src/hoc/withErrorHandler/withErrorHandler.js index 0ea765d..c9178f0 100644 --- a/src/hoc/withErrorHandler/withErrorHandler.js +++ b/src/hoc/withErrorHandler/withErrorHandler.js @@ -9,7 +9,7 @@ const withErrorHandler = (WrappedComponent, axios) => { error: null, } - componentDidMount() { + componentWillMount() { // This will happen before child component rendering axios.interceptors.request.use(req => { this.setState({ error: null }); return req;