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 #34

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Ht3 #34

Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions src/components/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import Restaurants from '../restaurants';
import Header from '../header';
import Basket from '../basket';

export default class App extends PureComponent {
render() {
return (
<div>
<Header />
<Basket restaurants={this.props.restaurants} />
<Restaurants restaurants={this.props.restaurants} />
</div>
);
Expand Down
99 changes: 99 additions & 0 deletions src/components/basket/basket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import React, { useMemo, useState } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { clear, increment, decrement } from '../../redux/actions';
import MinusIcon from './icons/minus.svg';
import PlusIcon from './icons/plus.svg';
import popup from '../../hocs/popup';

import styles from './basket.module.css';

const Basket = ({
order = {},
restaurants,
clearBasket,
increment,
decrement,
isOpenBasket,
hideBasket,
openBasket,
}) => {
const products = useMemo(() => {
Copy link
Owner

Choose a reason for hiding this comment

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

👍

return restaurants.flatMap(({ menu }) => menu);
}, [restaurants]);

const getCountProduct = (productId) => {
return order[productId] || 0;
};

return (
<div className={styles.basket}>
<button
className={styles.button}
onClick={isOpenBasket ? hideBasket : openBasket}
data-id="basket-clear"
>
</button>
{isOpenBasket && (
<div className={styles.products}>
{products.map((product) => (
Copy link
Owner

Choose a reason for hiding this comment

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

на сколько я понимаю тут всегда показываются все продукты, даже те, которые мы не заказывали?

<div key={product.id} className={styles.product}>
{product.name}
<div>Price {product.price} $ </div>
<div>Count {getCountProduct(product.id)}</div>
<div>
Total coast {getCountProduct(product.id) * product.price}
</div>
<div className={styles.buttons}>
<button
className={styles.button}
onClick={() => decrement(product.id)}
data-id="product-decrement"
>
<img src={MinusIcon} alt="minus" />
</button>
<button
className={styles.button}
onClick={() => increment(product.id)}
data-id="product-increment"
>
<img src={PlusIcon} alt="plus" />
</button>
</div>
</div>
))}
<button
className={styles.button}
onClick={clearBasket}
data-id="basket-clear"
>
x
</button>
</div>
)}
</div>
);
};

Basket.propTypes = {
order: PropTypes.object.isRequired,
clearBasket: PropTypes.func.isRequired,
increment: PropTypes.func.isRequired,
decrement: PropTypes.func.isRequired,
hideBasket: PropTypes.func.isRequired,
openBasket: PropTypes.func.isRequired,
isOpenBasket: PropTypes.bool.isRequired,
};

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

const mapDispatchToProps = (dispatch) => ({
clearBasket: () => dispatch(clear()),
increment: (productId) => dispatch(increment(productId)),
decrement: (productId) => dispatch(decrement(productId)),
});

export default connect(mapStateToProps, mapDispatchToProps)(popup(Basket));
Copy link
Owner

Choose a reason for hiding this comment

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

тут лучше просто usePopup использовать в компоненте - намного очевиднее будет

49 changes: 49 additions & 0 deletions src/components/basket/basket.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.basket {
position: relative;
padding: 15px;
}

.products {
position: absolute;
padding: 15px;
margin-top: 10px;
width: 50%;
height: 300px;
z-index: 5;
border: 1px solid var(--grey);
background: var(--white);
overflow-y: scroll;
}

.product {
padding: 5px;
margin: 5px 0;
border: 1px solid var(--grey);
}

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

.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;
}
1 change: 1 addition & 0 deletions src/components/basket/icons/minus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/components/basket/icons/plus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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';
7 changes: 7 additions & 0 deletions src/hocs/popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';
import usePopup from '../hooks/use-popup';

export default (WrappedComponent) => ({ initialState, ...props }) => {
const popupProps = usePopup(initialState);
return <WrappedComponent {...props} {...popupProps} />;
};
9 changes: 9 additions & 0 deletions src/hooks/use-popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useState } from 'react';

export default function usePopup(initialState = false) {
const [isOpenBasket, toggleBasket] = useState(initialState);
const hideBasket = () => toggleBasket(false);
const openBasket = () => toggleBasket(true);

return { isOpenBasket, hideBasket, openBasket };
}
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, CLEAR } from './constants';

export const increment = (id) => ({ type: INCREMENT, payload: { id } });
export const decrement = (id) => ({ type: DECREMENT, payload: { id } });
export const clear = () => ({ type: CLEAR });
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 CLEAR = 'CLEAR';
6 changes: 4 additions & 2 deletions src/redux/reducers/order.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { DECREMENT, INCREMENT } from '../constants';
import { DECREMENT, INCREMENT, CLEAR } from '../constants';

// { [productId]: amount }
export default (state = 0, action) => {
export default (state = {}, action) => {
const { type, payload } = action;
switch (type) {
case INCREMENT:
return { ...state, [payload.id]: (state[payload.id] || 0) + 1 };
case DECREMENT:
return { ...state, [payload.id]: (state[payload.id] || 0) - 1 };
case CLEAR:
Copy link
Owner

Choose a reason for hiding this comment

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

тут нужно было удалять только один конкретный продукт, а не все, что есть в корзине

return {};
default:
return state;
}
Expand Down