Skip to content

Commit 06fbd14

Browse files
authored
Merge pull request thinkswell#159 from thinkswell/ReactCart-thinkswell-branch
React Cart app added
2 parents fa90870 + c70cd69 commit 06fbd14

39 files changed

+16987
-0
lines changed

ReactCart/thinkswell/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

ReactCart/thinkswell/package-lock.json

Lines changed: 16023 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ReactCart/thinkswell/package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "react-complete-guide",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"react": "^17.0.1",
7+
"react-dom": "^17.0.1",
8+
"react-scripts": "^4.0.1",
9+
"web-vitals": "^0.2.4"
10+
},
11+
"scripts": {
12+
"start": "react-scripts start",
13+
"build": "react-scripts build",
14+
"test": "react-scripts test",
15+
"eject": "react-scripts eject"
16+
},
17+
"eslintConfig": {
18+
"extends": [
19+
"react-app",
20+
"react-app/jest"
21+
]
22+
},
23+
"browserslist": {
24+
"production": [
25+
">0.2%",
26+
"not dead",
27+
"not op_mini all"
28+
],
29+
"development": [
30+
"last 1 chrome version",
31+
"last 1 firefox version",
32+
"last 1 safari version"
33+
]
34+
}
35+
}
3.78 KB
Binary file not shown.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<!--
14+
manifest.json provides metadata used when your web app is installed on a
15+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16+
-->
17+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
<!--
19+
Notice the use of %PUBLIC_URL% in the tags above.
20+
It will be replaced with the URL of the `public` folder during the build.
21+
Only files inside the `public` folder can be referenced from the HTML.
22+
23+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24+
work correctly both with client-side routing and a non-root public URL.
25+
Learn how to configure a non-root public URL by running `npm run build`.
26+
-->
27+
<title>React App</title>
28+
</head>
29+
<body>
30+
<noscript>You need to enable JavaScript to run this app.</noscript>
31+
<div id="overlays"></div>
32+
<div id="root"></div>
33+
<!--
34+
This HTML file is a template.
35+
If you open it directly in the browser, you will see an empty page.
36+
37+
You can add webfonts, meta tags, or analytics to this file.
38+
The build step will place the bundled scripts into the <body> tag.
39+
40+
To begin the development, run `npm start` or `yarn start`.
41+
To create a production bundle, use `npm run build` or `yarn build`.
42+
-->
43+
</body>
44+
</html>
5.22 KB
Loading
9.44 KB
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
},
10+
{
11+
"src": "logo192.png",
12+
"type": "image/png",
13+
"sizes": "192x192"
14+
},
15+
{
16+
"src": "logo512.png",
17+
"type": "image/png",
18+
"sizes": "512x512"
19+
}
20+
],
21+
"start_url": ".",
22+
"display": "standalone",
23+
"theme_color": "#000000",
24+
"background_color": "#ffffff"
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# https://www.robotstxt.org/robotstxt.html
2+
User-agent: *
3+
Disallow:

ReactCart/thinkswell/src/App.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { useState } from "react";
2+
import Cart from "./components/Cart/Cart";
3+
import Header from "./components/Layout/Header";
4+
import Meals from "./components/Meals/Meals";
5+
import CartProvider from "./store/CartProvider";
6+
7+
function App() {
8+
const [cartIsShown, setCartIsShown] = useState(false);
9+
10+
const showCartHandler = () => {
11+
setCartIsShown(true);
12+
};
13+
const hideCartHandler = () => {
14+
setCartIsShown(false);
15+
};
16+
17+
return (
18+
<CartProvider>
19+
{cartIsShown && <Cart onClose={hideCartHandler} />}
20+
<Header onShowCart={showCartHandler} />
21+
<main>
22+
<Meals />
23+
</main>
24+
</CartProvider>
25+
);
26+
}
27+
28+
export default App;
407 KB
Loading
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { useContext } from "react";
2+
import Modal from "../UI/Modal";
3+
import classes from "./Cart.module.css";
4+
import CartContext from "../../store/cart-context";
5+
import CartItem from "./CartItem";
6+
7+
const Cart = (props) => {
8+
const cartCtx = useContext(CartContext);
9+
10+
const totalAmount = `$${cartCtx.totalAmount.toFixed(2)}`;
11+
const hasItems = cartCtx.items.length > 0;
12+
13+
const cartItemRemoveHandler = (id) => {
14+
cartCtx.removeItem(id);
15+
};
16+
17+
const cartItemAddHandler = (item) => {
18+
cartCtx.addItem({ ...item, amount: 1 });
19+
};
20+
21+
const cartItems = (
22+
<ul className={classes["cart-items"]}>
23+
{cartCtx.items.map((item) => (
24+
<CartItem
25+
key={item.id}
26+
{...item}
27+
onRemove={cartItemRemoveHandler.bind(null, item.id)}
28+
onAdd={cartItemAddHandler.bind(null, item)}
29+
/>
30+
))}
31+
</ul>
32+
);
33+
34+
return (
35+
<Modal onClose={props.onClose}>
36+
{cartItems}
37+
<div className={classes.total}>
38+
<span>Total Amount</span>
39+
<span>{totalAmount}</span>
40+
</div>
41+
<div className={classes.actions}>
42+
<button className={classes["button--alt"]} onClick={props.onClose}>
43+
Close
44+
</button>
45+
{hasItems && <button className={classes.button}>Order</button>}
46+
</div>
47+
</Modal>
48+
);
49+
};
50+
51+
export default Cart;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
.cart-items {
2+
list-style: none;
3+
margin: 0;
4+
padding: 0;
5+
max-height: 20rem;
6+
overflow: auto;
7+
}
8+
9+
.total {
10+
display: flex;
11+
justify-content: space-between;
12+
align-items: center;
13+
font-weight: bold;
14+
font-size: 1.5rem;
15+
margin: 1rem 0;
16+
}
17+
18+
.actions {
19+
text-align: right;
20+
}
21+
22+
.actions button {
23+
font: inherit;
24+
cursor: pointer;
25+
background-color: transparent;
26+
border: 1px solid #8a2b06;
27+
padding: 0.5rem 2rem;
28+
border-radius: 25px;
29+
margin-left: 1rem;
30+
}
31+
32+
.actions button:hover,
33+
.actions button:active {
34+
background-color: #5a1a01;
35+
border-color: #5a1a01;
36+
color: white;
37+
}
38+
39+
.actions .button--alt {
40+
color: #8a2b06;
41+
}
42+
43+
.actions .button {
44+
background-color: #8a2b06;
45+
color: white;
46+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const CartIcon = () => {
2+
return (
3+
<svg
4+
xmlns="http://www.w3.org/2000/svg"
5+
viewBox="0 0 20 20"
6+
fill="currentColor"
7+
>
8+
<path d="M3 1a1 1 0 000 2h1.22l.305 1.222a.997.997 0 00.01.042l1.358 5.43-.893.892C3.74 11.846 4.632 14 6.414 14H15a1 1 0 000-2H6.414l1-1H14a1 1 0 00.894-.553l3-6A1 1 0 0017 3H6.28l-.31-1.243A1 1 0 005 1H3zM16 16.5a1.5 1.5 0 11-3 0 1.5 1.5 0 013 0zM6.5 18a1.5 1.5 0 100-3 1.5 1.5 0 000 3z" />
9+
</svg>
10+
);
11+
};
12+
13+
export default CartIcon;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import classes from "./CartItem.module.css";
2+
3+
const CartItem = (props) => {
4+
const price = `$${props.price.toFixed(2)}`;
5+
6+
return (
7+
<li className={classes["cart-item"]}>
8+
<div>
9+
<h2>{props.name}</h2>
10+
<div className={classes.summary}>
11+
<span className={classes.price}>{price}</span>
12+
<span className={classes.amount}>x {props.amount}</span>
13+
</div>
14+
</div>
15+
<div className={classes.actions}>
16+
<button onClick={props.onRemove}></button>
17+
<button onClick={props.onAdd}>+</button>
18+
</div>
19+
</li>
20+
);
21+
};
22+
23+
export default CartItem;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
.cart-item {
2+
display: flex;
3+
justify-content: space-between;
4+
align-items: center;
5+
border-bottom: 2px solid #8a2b06;
6+
padding: 1rem 0;
7+
margin: 1rem 0;
8+
}
9+
10+
.cart-item h2 {
11+
margin: 0 0 0.5rem 0;
12+
color: #363636;
13+
}
14+
15+
.summary {
16+
width: 10rem;
17+
display: flex;
18+
justify-content: space-between;
19+
align-items: center;
20+
}
21+
22+
.price {
23+
font-weight: bold;
24+
color: #8a2b06;
25+
}
26+
27+
.amount {
28+
font-weight: bold;
29+
border: 1px solid #ccc;
30+
padding: 0.25rem 0.75rem;
31+
border-radius: 6px;
32+
color: #363636;
33+
}
34+
35+
.actions {
36+
display: flex;
37+
flex-direction: column;
38+
}
39+
40+
@media (min-width: 768px) {
41+
.actions {
42+
flex-direction: row;
43+
}
44+
}
45+
46+
.cart-item button {
47+
font: inherit;
48+
font-weight: bold;
49+
font-size: 1.25rem;
50+
color: #8a2b06;
51+
border: 1px solid #8a2b06;
52+
width: 3rem;
53+
text-align: center;
54+
border-radius: 6px;
55+
background-color: transparent;
56+
cursor: pointer;
57+
margin-left: 1rem;
58+
margin: 0.25rem;
59+
}
60+
61+
.cart-item button:hover,
62+
.cart-item button:active {
63+
background-color: #8a2b06;
64+
color: white;
65+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from "react";
2+
import HeaderCartButton from "./HeaderCartButton";
3+
import mealsImage from "./../../assets/meals.jpg";
4+
import classes from "./Header.module.css";
5+
6+
const Header = (props) => {
7+
return (
8+
<React.Fragment>
9+
<header className={classes.header}>
10+
<h1>ReactMeals</h1>
11+
<HeaderCartButton onClick={props.onShowCart} />
12+
</header>
13+
<div className={classes["main-image"]}>
14+
<img src={mealsImage} alt="Table full of delicious food." />
15+
</div>
16+
</React.Fragment>
17+
);
18+
};
19+
20+
export default Header;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.header {
2+
position: fixed;
3+
top: 0;
4+
left: 0;
5+
width: 100%;
6+
height: 5rem;
7+
background-color: #8a2b06;
8+
color: white;
9+
display: flex;
10+
justify-content: space-between;
11+
align-items: center;
12+
padding: 0 10%;
13+
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
14+
z-index: 10;
15+
}
16+
17+
.main-image {
18+
width: 100%;
19+
height: 25rem;
20+
z-index: 0;
21+
overflow: hidden;
22+
}
23+
24+
.main-image img {
25+
width: 110%;
26+
height: 100%;
27+
object-fit: cover;
28+
transform: rotateZ(-5deg) translateY(-4rem) translateX(-1rem);
29+
}

0 commit comments

Comments
 (0)