-
Notifications
You must be signed in to change notification settings - Fork 0
Add cart functionality #2
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
Draft
mmena1
wants to merge
2
commits into
main
Choose a base branch
from
add-cart-functionality
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { useContext } from 'react'; | ||
|
|
||
| import Button from '@mui/material/Button'; | ||
| import Dialog from '@mui/material/Dialog'; | ||
| import DialogActions from '@mui/material/DialogActions'; | ||
| import DialogContent from '@mui/material/DialogContent'; | ||
| import DialogContentText from '@mui/material/DialogContentText'; | ||
| import DialogTitle from '@mui/material/DialogTitle'; | ||
|
|
||
| import List from '@mui/material/List'; | ||
| import ListItem from '@mui/material/ListItem'; | ||
| import ListItemText from '@mui/material/ListItemText'; | ||
|
|
||
| import CartContext from '../../store/cart-context'; | ||
|
|
||
| const Cart = props => { | ||
| const cartCtx = useContext(CartContext); | ||
| const totalAmount = `$${cartCtx.totalAmount.toFixed(2)}`; | ||
| const hasItems = cartCtx.items.length > 0; | ||
| const cartItems = ( | ||
| <List> | ||
| {cartCtx.items.map(item => ( | ||
| <ListItem disablePadding> | ||
| <ListItemText primary={item.name} /> | ||
| </ListItem> | ||
| ))} | ||
| {/* <ListItem disablePadding> | ||
| <ListItemText primary="Hello" /> | ||
| </ListItem> | ||
| <ListItem disablePadding> | ||
| <ListItemText primary="World" /> | ||
| </ListItem> */} | ||
| </List> | ||
| ); | ||
| return ( | ||
| <Dialog open={props.showCart} onClose={props.onClose}> | ||
| <DialogTitle>Shopping cart</DialogTitle> | ||
| <DialogContent> | ||
| {cartItems} | ||
| {/* {cartCtx.items[0].name} */} | ||
|
Comment on lines
+39
to
+40
Owner
Author
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. If I comment the first line and uncomment the second one, |
||
| <DialogContentText> | ||
| Total: {totalAmount} | ||
| </DialogContentText> | ||
| </DialogContent> | ||
| <DialogActions> | ||
| <Button color="error" onClick={props.onClose}>Cancel</Button> | ||
| {hasItems && <Button onClick={props.onClose}>Checkout</Button>} | ||
| </DialogActions> | ||
| </Dialog> | ||
| ); | ||
| } | ||
|
|
||
| export default Cart; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import Badge from '@mui/material/Badge'; | ||
| import ShoppingCartIcon from '@mui/icons-material/ShoppingCart'; | ||
| import IconButton from '@mui/material/IconButton'; | ||
| import { useContext } from 'react'; | ||
| import CartContext from '../../../store/cart-context'; | ||
|
|
||
| const HeaderCartButton = props => { | ||
| const cartCtx = useContext(CartContext); | ||
| const itemsQuantity = cartCtx.items.length; | ||
| return ( | ||
| <IconButton size="large" aria-label="show 4 new mails" color="inherit" onClick={props.onClick}> | ||
| <Badge badgeContent={itemsQuantity} color="error"> | ||
| <ShoppingCartIcon /> | ||
| </Badge> | ||
| </IconButton> | ||
| ); | ||
| }; | ||
|
|
||
| export default HeaderCartButton; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import AddShoppingCartIcon from '@mui/icons-material/AddShoppingCart'; | ||
| import IconButton from '@mui/material/IconButton'; | ||
|
|
||
| const ProductItemForm = props => { | ||
| const onSubmit = event => { | ||
| event.preventDefault(); | ||
|
|
||
| props.onAddToCart(1) | ||
| } | ||
|
|
||
| return ( | ||
| <form onSubmit={onSubmit}> | ||
| {/* <TextField ref={amountInputRef} type="number" variant="standard" label="Quantity" inputProps={{ | ||
| defaultValue: 1, | ||
| step: 1, | ||
| min: 1, | ||
| max: 5, | ||
| }} InputLabelProps={{ | ||
| shrink: true, | ||
| }}/> */} | ||
| <IconButton color="secondary" aria-label="add to cart" type='submit'> | ||
| <AddShoppingCartIcon /> | ||
| </IconButton> | ||
| </form> | ||
|
|
||
| ); | ||
| }; | ||
|
|
||
| export default ProductItemForm; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { useReducer } from "react"; | ||
| import CartContext from "./cart-context"; | ||
|
|
||
| const defaultCartState = { | ||
| items: [], | ||
| totalAmount: 0 | ||
| } | ||
|
|
||
| const cartReducer = (state, action) => { | ||
| if (action.type === "ADD") { | ||
| const updatedItems = state.items.concat(action.item); | ||
| const updatedTotal = state.totalAmount + action.item.price * action.item.amount; | ||
| return { | ||
| items: updatedItems, | ||
| totalAmount: updatedTotal | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const CartProvider = props => { | ||
| const [cartState, dispatchCartAction] = useReducer(cartReducer, defaultCartState); | ||
|
|
||
| const addItemToCartHandler = item => { | ||
| dispatchCartAction({type: "ADD", item}); | ||
| }; | ||
| const removeItemFromCartHandler = id => { | ||
| dispatchCartAction({type: "REMOVE", id}); | ||
| }; | ||
|
|
||
| const cartContext = { | ||
| items: cartState.items, | ||
| totalAmount: cartState.totalAmount, | ||
| addItem: addItemToCartHandler, | ||
| removeItem: removeItemFromCartHandler | ||
| }; | ||
|
|
||
| return <CartContext.Provider value={cartContext}> | ||
| {props.children} | ||
| </CartContext.Provider> | ||
| }; | ||
|
|
||
| export default CartProvider; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import React from "react" | ||
|
|
||
| const CartContext = React.createContext({ | ||
| items: [], | ||
| totalAmount: 0, | ||
| addItem: item => {}, | ||
| removeItem: id => {} | ||
| }); | ||
|
|
||
| export default CartContext; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { configDefaults, defineConfig } from 'vitest/config' | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| exclude: [...configDefaults.exclude, 'packages/template/*'], | ||
| }, | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This, on the other hand, works as expected, so I'm not sure what's wrong with
cartCtx.items.map