-
Notifications
You must be signed in to change notification settings - Fork 26
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
basket, remove item, price, total price #45
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { connect } from 'react-redux'; | ||
import { decrement, increment, removeItem } from '../../redux/actions'; | ||
|
||
import { ReactComponent as DeleteIcon } from '../../icons/delete-icon.svg'; | ||
import Button from '../button'; | ||
import styles from './basket.module.css'; | ||
|
||
const Basket = ({ order, decrement, increment, restaurants, removeItem, handleBasket}) => { | ||
const dishes = [...restaurants].map(item => item.menu.map(item2 => item2)).flat(2); | ||
let selectedItems = []; | ||
|
||
for (let key in order) { | ||
dishes.forEach(item => { | ||
if (item.id === key){ | ||
selectedItems = [...selectedItems, item]; | ||
item.qty = order[key] | ||
} | ||
}) | ||
} | ||
const totalPrice = selectedItems.reduce(( prev, cur ) => prev + cur.price*cur.qty, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. и в целом все эти вычисления можно вынести в mapStateToProps |
||
|
||
return( | ||
<div className={styles.basket}> | ||
<DeleteIcon className={styles.closeIcon} onClick={handleBasket} /> | ||
<p>Selected Items:</p> | ||
{selectedItems.map(item => ( | ||
<div key={item.id} className={styles.basketItem}> | ||
<div> | ||
<p>{item.name}</p> | ||
<p>{item.qty*item.price}$</p> | ||
<p className={styles.qty}>qty: {item.qty}</p> | ||
</div> | ||
<div className={styles.wrapperButtons}> | ||
<DeleteIcon className={styles.deleteIcon} onClick={() => removeItem(item.id)} /> | ||
<div className={styles.buttons}> | ||
<Button | ||
onClick={() => decrement(item.id)} | ||
icon="minus" | ||
/> | ||
<Button | ||
onClick={() => increment(item.id)} | ||
icon="plus" | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
)} | ||
<p>Total price: {totalPrice}$</p> | ||
</div> | ||
) | ||
}; | ||
|
||
const mapStateToProps = (state, props) => ({ | ||
order: state.order | ||
}); | ||
|
||
const mapDispatchToProps = { | ||
decrement, | ||
increment, | ||
removeItem, | ||
}; | ||
export default connect(mapStateToProps, mapDispatchToProps)(Basket); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
.basket { | ||
position: fixed; | ||
width: 500px; | ||
top: 170px; | ||
border: 1px solid var(--grey); | ||
z-index: 3; | ||
left: calc(50% - 250px); | ||
padding: 15px; | ||
height: 300px; | ||
background: var(--grey); | ||
overflow-y: auto; | ||
} | ||
|
||
.basketItem{ | ||
padding-bottom: 7px; | ||
border-bottom: 1px solid darkgray; | ||
display: flex; | ||
justify-content: space-between; | ||
} | ||
|
||
.buttons { | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-between; | ||
align-items: center; | ||
width: auto; | ||
} | ||
|
||
.deleteIcon{ | ||
width: 20px; | ||
height: 20px; | ||
fill: var(--yellow); | ||
margin: 12px 5px 0 auto; | ||
cursor: pointer; | ||
} | ||
|
||
.wrapperButtons{ | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: space-between; | ||
} | ||
|
||
.closeIcon{ | ||
position: absolute; | ||
width: 20px; | ||
height: 20px; | ||
right: 10px; | ||
top: 10px; | ||
fill: darkgrey; | ||
cursor: pointer; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './basket'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { DECREMENT, INCREMENT } from './constants'; | ||
import { DECREMENT, INCREMENT, REMOVE_ITEM } from './constants'; | ||
|
||
export const increment = (id) => ({ type: INCREMENT, id }); | ||
export const decrement = (id) => ({ type: DECREMENT, id }); | ||
export const removeItem = (id) => ({ type: REMOVE_ITEM, id }); |
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_ITEM = 'REMOVE_ITEM'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { DECREMENT, INCREMENT } from '../constants'; | ||
import { DECREMENT, INCREMENT, REMOVE_ITEM } from '../constants'; | ||
|
||
// { [productId]: amount } | ||
export default function (state = {}, action) { | ||
|
@@ -8,6 +8,9 @@ export default function (state = {}, action) { | |
return { ...state, [id]: (state[id] || 0) + 1 }; | ||
case DECREMENT: | ||
return { ...state, [id]: (state[id] || 0) - 1 }; | ||
case REMOVE_ITEM: | ||
delete state[id]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это мутация данных предыдущего стейта, этого делать нельзя |
||
return { ...state }; | ||
default: | ||
return state; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
это вычисление лучше мемоизировать