Skip to content

Commit b6cbe86

Browse files
author
Iryna Kashyntseva
committed
3. - Links from Basket Item to Restaurant
1 parent d6cdcc4 commit b6cbe86

File tree

7 files changed

+29
-16
lines changed

7 files changed

+29
-16
lines changed

src/components/basket/basket-item/basket-item.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { connect } from 'react-redux';
22
import cn from 'classnames';
33
import { increment, decrement, remove } from '../../../redux/actions';
4+
import { Link } from 'react-router-dom';
45
import Button from '../../button';
56
import styles from './basket-item.module.css';
67

78
function BasketItem({
9+
restId,
810
product,
911
amount,
1012
subtotal,
@@ -15,7 +17,7 @@ function BasketItem({
1517
return (
1618
<div className={styles.basketItem}>
1719
<div className={styles.name}>
18-
<span>{product.name}</span>
20+
<Link to={`/restaurants/${restId}`}>{product.name}</Link>
1921
</div>
2022
<div className={styles.info}>
2123
<div className={styles.counter}>

src/components/basket/basket.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ function Basket({ title = 'Basket', total, orderProducts }) {
1919
return (
2020
<div className={styles.basket}>
2121
<h4 className={styles.title}>{title}</h4>
22-
{orderProducts.map(({ product, amount, subtotal }) => (
22+
{orderProducts.map(({ product, amount, restId, subtotal }) => (
2323
<BasketItem
2424
product={product}
2525
amount={amount}
26+
restId={restId}
2627
key={product.id}
2728
subtotal={subtotal}
2829
/>

src/components/menu/menu.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Menu extends Component {
5757
<div className={styles.menu}>
5858
<div>
5959
{menu.map((id) => (
60-
<Product key={id} id={id} />
60+
<Product key={id} id={id} restId={this.props.restId} />
6161
))}
6262
</div>
6363
<div>

src/components/product/product.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const mapStateToProps = (state, props) => ({
5757
});
5858

5959
const mapDispatchToProps = (dispatch, props) => ({
60-
increment: () => dispatch(increment(props.id)),
60+
increment: () => dispatch(increment(props.id, props.restId)),
6161
decrement: () => dispatch(decrement(props.id)),
6262
});
6363

src/redux/actions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
reviewsLoadedSelector,
2121
} from './selectors';
2222

23-
export const increment = (id) => ({ type: INCREMENT, id });
23+
export const increment = (id, restId) => ({ type: INCREMENT, id, restId });
2424
export const decrement = (id) => ({ type: DECREMENT, id });
2525
export const remove = (id) => ({ type: REMOVE, id });
2626

src/redux/reducer/order.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
import produce from 'immer';
12
import { DECREMENT, INCREMENT, REMOVE } from '../constants';
23

34
// { [productId]: amount }
4-
export default function (state = {}, action) {
5-
const { type, id } = action;
5+
export default produce((draft = {}, action) => {
6+
const { type, id, restId } = action;
67
switch (type) {
78
case INCREMENT:
8-
return { ...state, [id]: (state[id] || 0) + 1 };
9+
draft[id] = { amount: (draft[id]?.amount || 0) + 1, restId };
10+
break;
911
case DECREMENT:
10-
return { ...state, [id]: state[id] > 0 ? (state[id] || 0) - 1 : 0 };
12+
draft[id] = {
13+
amount: draft[id]?.amount > 0 ? (draft[id]?.amount || 0) - 1 : 0,
14+
};
15+
break;
1116
case REMOVE:
12-
return { ...state, [id]: 0 };
17+
draft[id] = { amount: 0 };
18+
break;
1319
default:
14-
return state;
20+
return draft;
1521
}
16-
}
22+
});

src/redux/selectors.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,22 @@ export const restaurantSelector = (state, { id }) =>
3232
restaurantsSelector(state)[id];
3333
export const productSelector = (state, { id }) => productsSelector(state)[id];
3434
export const reviewSelector = (state, { id }) => reviewsSelector(state)[id];
35-
export const amountSelector = (state, { id }) => orderSelector(state)[id] || 0;
35+
export const amountSelector = (state, { id }) =>
36+
orderSelector(state)[id]?.amount || 0;
37+
export const productRestIdSelector = (state, id) =>
38+
orderSelector(state)[id]?.restId;
3639
export const orderProductsSelector = createSelector(
3740
orderSelector,
3841
productsSelector,
3942
(order, products) =>
4043
Object.keys(order)
41-
.filter((productId) => order[productId] > 0)
44+
.filter((productId) => order[productId].amount > 0)
4245
.map((productId) => products[productId])
4346
.map((product) => ({
4447
product,
45-
amount: order[product.id],
46-
subtotal: order[product.id] * product.price,
48+
restId: order[product.id].restId,
49+
amount: order[product.id].amount,
50+
subtotal: order[product.id].amount * product.price,
4751
}))
4852
);
4953

0 commit comments

Comments
 (0)