Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HT3 #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

HT3 #32

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/components/basket/basket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from 'react';
import { connect } from 'react-redux';
import Order from '../order';
import styles from './basket.module.css';

export const basket = ({ restaurants, orderProduct }) => {
if (Object.keys(orderProduct).length === 0) {
return (
<div className={styles.basket}>
<div className={styles.title}>Basket is empty</div>
</div>
);
}

let products = restaurants.reduce(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

это вычисление нужно мемоизировать

(accumulator, currentValue) => [...accumulator, ...currentValue.menu],
[]
);

let orderProducts = products.filter((product) => orderProduct[product.id]);

let totalPrice = `${orderProducts.reduce(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

в целом все эти вычисления лучше перенести в mapStateToProps

(accumulator, currentValue) =>
accumulator + orderProduct[currentValue.id] * currentValue.price,
0
)}$`;

return (
<div className={styles.basket}>
<div className={styles.title}>Basket</div>
<div>
{orderProducts.map((product) => (
<Order key={product.id} product={product}></Order>
))}
</div>
<div className={styles.totalPrice}>
Total price: <span>{totalPrice}</span>
</div>
</div>
);
};

const mapStateToProps = (state) => {
return {
orderProduct: state.order,
};
};

export default connect(mapStateToProps)(basket);
18 changes: 18 additions & 0 deletions src/components/basket/basket.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.basket {
padding: 32px;
border-top: 1px solid var(--black);
border-bottom: 1px solid var(--black);
}
.title {
text-align: center;
margin-bottom: 16px;
}
.totalPrice {
margin-top: 16px;
padding: 0 32px;
font-weight: 300;
font-size: 24px;
}
.totalPrice span {
font-weight: 500;
}
1 change: 1 addition & 0 deletions src/components/basket/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './basket';
1 change: 1 addition & 0 deletions src/components/order/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './order';
46 changes: 46 additions & 0 deletions src/components/order/order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import { connect } from 'react-redux';
import styles from './order.module.css';
import { decrement, increment, remove } from '../../redux/actions';

const Order = ({ product, amount, increment, decrement, remove }) => {
return (
<div className={styles.product}>
<div className={styles.content}>
<div className={styles.productInfo}>
<h4 className={styles.productName}>{product.name}</h4>
<div className={styles.productPrice}>{product.price} $</div>
<div className={styles.productSumPrice}>
Sum price: <span>{product.price * amount}$</span>
</div>
</div>
<div className={styles.productControls}>
<div className={styles.productCount}>{amount}</div>
<div className={styles.buttons}>
<button className={styles.button} onClick={decrement}>
-
</button>
<button className={styles.button} onClick={increment}>
+
</button>
<button className={styles.buttonRemove} onClick={remove}>
X
</button>
</div>
</div>
</div>
</div>
);
};

const mapStateToProps = (state, ownProps) => ({
amount: state.order[ownProps.product.id] || 0,
});

const mapDispatchToProps = (dispatch, ownProps) => ({
increment: () => dispatch(increment(ownProps.product.id)),
decrement: () => dispatch(decrement(ownProps.product.id)),
remove: () => dispatch(remove(ownProps.product.id)),
});

export default connect(mapStateToProps, mapDispatchToProps)(Order);
113 changes: 113 additions & 0 deletions src/components/order/order.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
.product {
padding: 16px 32px;
background: rgba(240, 173, 78, 0.1);
margin-bottom: 8px;
}

.content {
display: flex;
align-items: flex-start;
justify-content: space-between;
}

.productName {
font-size: 18px;
line-height: 28px;
font-weight: 600;
margin-top: 0;
margin-bottom: 8px;
}

.productPrice {
margin-top: 8px;
font-size: 18px;
line-height: 28px;
font-weight: 600;
color: var(--yellow);
}

.productControls {
padding: 5px 0;
text-align: center;
display: inline-block;
}

.productCount {
color: var(--grey-dark);
font-weight: 600;
margin-bottom: 5px;
font-size: 18px;
line-height: 28px;
}

.buttons {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
width: auto;
}

.button {
color: var(--white);
background-color: var(--yellow);
border: 1px solid var(--yellow);
border-radius: 2px;
width: 36px;
height: 36px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 0;
font-weight: 700;
font-size: 24px;
outline: none;
box-shadow: none;
cursor: pointer;
margin: 0 1px;
}

.button:hover,
.button:focus,
.button:active {
border-color: var(--yellow-light);
background-color: var(--yellow-light);
}

.buttonRemove {
color: var(--white);
background-color: rgba(255, 0, 0, 0.6);
border: 1px solid rgba(255, 0, 0, 0.6);
border-radius: 2px;
width: 36px;
height: 36px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 0;
font-weight: 700;
font-size: 24px;
outline: none;
box-shadow: none;
cursor: pointer;
margin: 0 1px;
}

.buttonRemove:hover,
.buttonRemove:focus,
.buttonRemove:active {
border-color: rgba(255, 0, 0, 0.4);
background-color: rgba(255, 0, 0, 0.4);
}

.footer {
padding-top: 16px;
display: flex;
justify-content: flex-end;
}

.productSumPrice {
font-size: 20px;
}
2 changes: 2 additions & 0 deletions src/components/restaurants/restaurants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useMemo } from 'react';
import PropTypes from 'prop-types';
import Restaurant from '../restaurant';
import Navigation from '../navigation';
import Basket from '../basket';

const Restaurants = ({ restaurants }) => {
const [activeRestaurantId, setActiveRestaurant] = useState(restaurants[0].id);
Expand All @@ -17,6 +18,7 @@ const Restaurants = ({ restaurants }) => {
restaurants={restaurants}
onRestaurantClick={setActiveRestaurant}
/>
<Basket restaurants={restaurants}></Basket>
<Restaurant restaurant={activeRestaurant} />
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion src/redux/actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { INCREMENT, DECREMENT } from './constants';
import { INCREMENT, DECREMENT, REMOVE } from './constants';

export const increment = (id) => ({ type: INCREMENT, payload: { id } });
export const decrement = (id) => ({ type: DECREMENT, payload: { id } });
export const remove = (id) => ({ type: REMOVE, payload: { id } });
1 change: 1 addition & 0 deletions src/redux/constants.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';
export const REMOVE = 'REMOVE';
9 changes: 8 additions & 1 deletion src/redux/reducers/order.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DECREMENT, INCREMENT } from '../constants';
import { DECREMENT, INCREMENT, REMOVE } from '../constants';

// { [productId]: amount }
export default (state = 0, action) => {
Expand All @@ -7,7 +7,14 @@ export default (state = 0, action) => {
case INCREMENT:
return { ...state, [payload.id]: (state[payload.id] || 0) + 1 };
case DECREMENT:
if (state[payload.id] === undefined || state[payload.id] - 1 <= 0) {
delete state[payload.id];
return { ...state };
}
return { ...state, [payload.id]: (state[payload.id] || 0) - 1 };
case REMOVE:
delete state[payload.id];
return { ...state };
default:
return state;
}
Expand Down