Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
38 changes: 38 additions & 0 deletions code/01-starting-project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "react-complete-guide",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.6",
"@testing-library/react": "^11.2.2",
"@testing-library/user-event": "^12.5.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-scripts": "^5.0.1",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Binary file added code/01-starting-project/public/favicon.ico
Binary file not shown.
44 changes: 44 additions & 0 deletions code/01-starting-project/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.

Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="overlays"></div>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.

You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.

To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added code/01-starting-project/public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added code/01-starting-project/public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions code/01-starting-project/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions code/01-starting-project/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
30 changes: 30 additions & 0 deletions code/01-starting-project/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useState } from 'react';

import Header from './components/Layout/Header';
import Meals from './components/Meals/Meals';
import Cart from './components/Cart/Cart';
import CartProvider from './store/CartProvider';

function App() {
const [cartIsShown, setCartIsShown] = useState(false);

const showCartHandler = () => {
setCartIsShown(true);
};

const hideCartHandler = () => {
setCartIsShown(false);
};

return (
<CartProvider>
{cartIsShown && <Cart onClose={hideCartHandler} />}
<Header onShowCart={showCartHandler} />
<main>
<Meals />
</main>
</CartProvider>
);
}

export default App;
Binary file added code/01-starting-project/src/assets/meals.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions code/01-starting-project/src/components/Cart/Cart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useContext } from 'react';

import Modal from '../UI/Modal';
import CartItem from './CartItem';
import classes from './Cart.module.css';
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 cartItemRemoveHandler = (id) => {
cartCtx.removeItem(id);
};

const cartItemAddHandler = (item) => {
cartCtx.addItem(item);
};

const cartItems = (
<ul className={classes['cart-items']}>
{cartCtx.items.map((item) => (
<CartItem
key={item.id}
name={item.name}
amount={item.amount}
price={item.price}
onRemove={cartItemRemoveHandler.bind(null, item.id)}
onAdd={cartItemAddHandler.bind(null, item)}
/>
))}
</ul>
);

return (
<Modal onClose={props.onClose}>
{cartItems}
<div className={classes.total}>
<span>Total Amount</span>
<span>{totalAmount}</span>
</div>
<div className={classes.actions}>
<button className={classes['button--alt']} onClick={props.onClose}>
Close
</button>
{hasItems && <button className={classes.button}>Order</button>}
</div>
</Modal>
);
};

export default Cart;
46 changes: 46 additions & 0 deletions code/01-starting-project/src/components/Cart/Cart.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
.cart-items {
list-style: none;
margin: 0;
padding: 0;
max-height: 20rem;
overflow: auto;
}

.total {
display: flex;
justify-content: space-between;
align-items: center;
font-weight: bold;
font-size: 1.5rem;
margin: 1rem 0;
}

.actions {
text-align: right;
}

.actions button {
font: inherit;
cursor: pointer;
background-color: transparent;
border: 1px solid #8a2b06;
padding: 0.5rem 2rem;
border-radius: 25px;
margin-left: 1rem;
}

.actions button:hover,
.actions button:active {
background-color: #5a1a01;
border-color: #5a1a01;
color: white;
}

.actions .button--alt {
color: #8a2b06;
}

.actions .button {
background-color: #8a2b06;
color: white;
}
13 changes: 13 additions & 0 deletions code/01-starting-project/src/components/Cart/CartIcon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const CartIcon = () => {
return (
<svg
xmlns='http://www.w3.org/2000/svg'
viewBox='0 0 20 20'
fill='currentColor'
>
<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' />
</svg>
);
};

export default CartIcon;
23 changes: 23 additions & 0 deletions code/01-starting-project/src/components/Cart/CartItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import classes from './CartItem.module.css';

const CartItem = (props) => {
const price = `$${props.price.toFixed(2)}`;

return (
<li className={classes['cart-item']}>
<div>
<h2>{props.name}</h2>
<div className={classes.summary}>
<span className={classes.price}>{price}</span>
<span className={classes.amount}>x {props.amount}</span>
</div>
</div>
<div className={classes.actions}>
<button onClick={props.onRemove}>−</button>
<button onClick={props.onAdd}>+</button>
</div>
</li>
);
};

export default CartItem;
65 changes: 65 additions & 0 deletions code/01-starting-project/src/components/Cart/CartItem.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
.cart-item {
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 2px solid #8a2b06;
padding: 1rem 0;
margin: 1rem 0;
}

.cart-item h2 {
margin: 0 0 0.5rem 0;
color: #363636;
}

.summary {
width: 10rem;
display: flex;
justify-content: space-between;
align-items: center;
}

.price {
font-weight: bold;
color: #8a2b06;
}

.amount {
font-weight: bold;
border: 1px solid #ccc;
padding: 0.25rem 0.75rem;
border-radius: 6px;
color: #363636;
}

.actions {
display: flex;
flex-direction: column;
}

@media (min-width: 768px) {
.actions {
flex-direction: row;
}
}

.cart-item button {
font: inherit;
font-weight: bold;
font-size: 1.25rem;
color: #8a2b06;
border: 1px solid #8a2b06;
width: 3rem;
text-align: center;
border-radius: 6px;
background-color: transparent;
cursor: pointer;
margin-left: 1rem;
margin: 0.25rem;
}

.cart-item button:hover,
.cart-item button:active {
background-color: #8a2b06;
color: white;
}
21 changes: 21 additions & 0 deletions code/01-starting-project/src/components/Layout/Header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Fragment } from 'react';

import HeaderCartButton from './HeaderCartButton';
import mealsImage from '../../assets/meals.jpg';
import classes from './Header.module.css';

const Header = (props) => {
return (
<Fragment>
<header className={classes.header}>
<h1>ReactMeals</h1>
<HeaderCartButton onClick={props.onShowCart} />
</header>
<div className={classes['main-image']}>
<img src={mealsImage} alt='A table full of delicious food!' />
</div>
</Fragment>
);
};

export default Header;
Loading