-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a74cee
commit 61b2a55
Showing
5 changed files
with
106 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,49 @@ | ||
import React, {Component} from 'react'; | ||
import axios from '../../axios-orders'; | ||
import {connect} from 'react-redux'; | ||
|
||
import Order from '../../components/Order/Order'; | ||
import withErrorHandler from '../../hoc/withErrorHandler/withErrorHandler'; | ||
import * as actions from '../../store/actions/index'; | ||
import Spinner from '../../components/UI/Spinner/Spinner'; | ||
|
||
class Orders extends Component { | ||
state = { | ||
orders: [], | ||
loading: true | ||
} | ||
|
||
componentDidMount() { | ||
axios.get('/orders.json') | ||
.then(res => { | ||
const fetchedOrders = []; | ||
for (let key in res.data) { | ||
fetchedOrders.push({ | ||
id: key, | ||
...res.data[key] | ||
}); | ||
} | ||
this.setState({ | ||
loading: false, | ||
orders: fetchedOrders | ||
}); | ||
}) | ||
.catch(err => { | ||
this.setState({loading: false}); | ||
}); | ||
this.props.onFetchOrders(); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
{this.state.orders.map(order => ( | ||
<Order | ||
key={order.id} | ||
ingredients={order.ingredients} | ||
price={order.price} | ||
/> | ||
))} | ||
</div> | ||
); | ||
let orders = <Spinner />; | ||
|
||
if (!this.props.loading) { | ||
orders = ( | ||
<div> | ||
{this.props.orders.map(order => ( | ||
<Order | ||
key={order.id} | ||
ingredients={order.ingredients} | ||
price={order.price} | ||
/> | ||
))} | ||
</div> | ||
); | ||
} | ||
|
||
return orders; | ||
} | ||
} | ||
|
||
export default withErrorHandler(Orders, axios); | ||
const mapStateToProps = state => { | ||
return { | ||
orders: state.order.orders, | ||
loading: state.order.loading | ||
}; | ||
}; | ||
|
||
const mapDispatchToProps = dispatch => { | ||
return { | ||
onFetchOrders: () => dispatch(actions.fetchOrders()) | ||
}; | ||
}; | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(withErrorHandler(Orders, axios)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ export { | |
|
||
export { | ||
purchaseBurger, | ||
purchaseBurgerInit | ||
purchaseBurgerInit, | ||
fetchOrders | ||
} from './order'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters