Skip to content

Commit

Permalink
Fetching orders by user from the server
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoTomeES committed Dec 18, 2019
1 parent 58f4fb6 commit 9e159ae
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/containers/Checkout/ContactData/ContactData.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ class ContactData extends Component {
const order = {
ingredients: this.props.ings,
price: this.props.price,
orderData: formData
orderData: formData,
userId: this.props.userId
}

this.props.onOrderBurger(order, this.props.token);
Expand Down Expand Up @@ -211,7 +212,8 @@ const mapStateToProps = state => {
ings: state.burgerBuilder.ingredients,
price: state.burgerBuilder.totalPrice,
loading: state.order.loading,
token: state.auth.token
token: state.auth.token,
userId: state.auth.userId
};
};

Expand Down
7 changes: 4 additions & 3 deletions src/containers/Orders/Orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Spinner from '../../components/UI/Spinner/Spinner';

class Orders extends Component {
componentDidMount() {
this.props.onFetchOrders(this.props.token);
this.props.onFetchOrders(this.props.token, this.props.userId);
}

render() {
Expand Down Expand Up @@ -37,13 +37,14 @@ const mapStateToProps = state => {
return {
orders: state.order.orders,
loading: state.order.loading,
token: state.auth.token
token: state.auth.token,
userId: state.auth.userId
};
};

const mapDispatchToProps = dispatch => {
return {
onFetchOrders: (token) => dispatch(actions.fetchOrders(token))
onFetchOrders: (token, userId) => dispatch(actions.fetchOrders(token, userId))
};
};

Expand Down
5 changes: 3 additions & 2 deletions src/store/actions/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ export const fetchOrdersStart = () => {
};
};

export const fetchOrders = (token) => {
export const fetchOrders = (token, userId) => {
return dispatch => {
dispatch(fetchOrdersStart());
axios.get('/orders.json?auth=' + token)
const queryParams = '?auth=' + token + '&orderBy="userId"&equalTo="' + userId + '"';
axios.get('/orders.json' + queryParams)
.then(res => {
const fetchedOrders = [];
for (let key in res.data) {
Expand Down

0 comments on commit 9e159ae

Please sign in to comment.