Skip to content

Commit bd4dadb

Browse files
committed
created cart component
1 parent b54326f commit bd4dadb

File tree

7 files changed

+76
-5
lines changed

7 files changed

+76
-5
lines changed

src/App.styles.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
import styled from 'styled-components';
2+
import IconButton from '@material-ui/core/IconButton';
23

3-
export const Wrapper = styled.div``;
4+
export const Wrapper = styled.div`
5+
margin: 40px;
6+
`;
7+
8+
export const StyledButton = styled(IconButton)`
9+
position: fixed;
10+
z-index: 100;
11+
right: 20px;
12+
top: 20px;
13+
`;

src/App.tsx

+21-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { useState } from 'react';
22
import { useQuery } from 'react-query';
33
// Components
44
import Item from './Item/Item';
5+
import Cart from './Cart/Cart';
56
import Drawer from '@material-ui/core/Drawer';
67
import LinearProgress from '@material-ui/core/LinearProgress';
78
import Grid from '@material-ui/core/Grid';
89
import AddShoppingCartIcon from '@material-ui/icons/AddShoppingCart';
910
import Badge from '@material-ui/core/Badge';
1011
// styles
11-
import { Wrapper } from './App.styles';
12+
import { Wrapper, StyledButton } from './App.styles';
13+
1214
// Types
1315
export type CartItemType = {
1416
id: number;
@@ -17,20 +19,24 @@ export type CartItemType = {
1719
image: string;
1820
price: number;
1921
tittle: string;
20-
ammount: number;
22+
amount: number;
2123
};
2224

2325
const getProducts = async (): Promise<CartItemType[]> =>
2426
await (await fetch('https://fakestoreapi.com/products')).json();
2527

2628
const App = () => {
29+
const [cartOpen, setCartOpen] = useState(false);
30+
const [cartItems, setCartItems] = useState([] as CartItemType[]);
31+
2732
const { data, isLoading, error } = useQuery<CartItemType[]>(
2833
'products',
2934
getProducts
3035
);
3136
console.log(data);
3237

33-
const getTotalItems = () => null;
38+
const getTotalItems = (items: CartItemType[]) =>
39+
items.reduce((ack: number, item) => ack + item.amount, 0);
3440

3541
const handleAddToCart = (clickedItem: CartItemType) => null;
3642

@@ -41,6 +47,18 @@ const App = () => {
4147

4248
return (
4349
<Wrapper>
50+
<Drawer anchor='right' open={cartOpen} onClose={() => setCartOpen(false)}>
51+
<Cart
52+
cartItems={cartItems}
53+
addToCart={handleAddToCart}
54+
removeFromCart={handleRemvoeFromCart}
55+
/>
56+
</Drawer>
57+
<StyledButton onClick={() => setCartOpen(true)}>
58+
<Badge badgeContent={getTotalItems(cartItems)} color='error'>
59+
<AddShoppingCartIcon />
60+
</Badge>
61+
</StyledButton>
4462
<Grid container spacing={3}>
4563
{data?.map((item) => (
4664
<Grid item key={item.id} xs={12} sm={4}>

src/Cart/Cart.styles.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import styled from 'styled-components';
2+
3+
export const Wrapper = styled.aside`
4+
width: 500px;
5+
padding: 20px;
6+
`;

src/Cart/Cart.tsx

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import CartItem from '../CartItem/CartItem';
2+
//Styles
3+
import { Wrapper } from './Cart.styles';
4+
// Types
5+
import { CartItemType } from '../App';
6+
7+
type Props = {
8+
cartItems: CartItemType[];
9+
addToCart: (clickedItem: CartItemType) => void;
10+
removeFromCart: (id: number) => void;
11+
};
12+
13+
const Cart: React.FC<Props> = ({ cartItems, addToCart, removeFromCart }) => {
14+
return (
15+
<Wrapper>
16+
<h2>Your Shopping Cart</h2>
17+
{cartItems.length === 0 ? <p>No items in cart.</p> : null}
18+
{cartItems.map((item) => (
19+
<CartItem />
20+
))}
21+
</Wrapper>
22+
);
23+
};
24+
25+
export default Cart;

src/CartItem/CartItem.styles.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import styled from 'styled-components';
2+
3+
export const Wrapper = styled.div``;

src/CartItem/CartItem.tsx

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Button from '@material-ui/core/Button';
2+
// types
3+
import { CartItemType } from '../App';
4+
// Styles
5+
import { Wrapper } from './CartItem.styles';
6+
7+
const CartItem: React.FC = () => <div>Cart item</div>;
8+
9+
export default CartItem;

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
"noEmit": true,
1717
"jsx": "react-jsx"
1818
},
19-
"include": ["src"]
19+
"include": ["./**/*"]
2020
}

0 commit comments

Comments
 (0)