Skip to content
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

Cart page #19

Merged
merged 18 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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
1 change: 1 addition & 0 deletions client/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
42 changes: 0 additions & 42 deletions client/src/App.css

This file was deleted.

36 changes: 11 additions & 25 deletions client/src/App.tsx
larkinds marked this conversation as resolved.
Show resolved Hide resolved
larkinds marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
import { useState } from "react";
import reactLogo from "./assets/react.svg";
import viteLogo from "/vite.svg";
import "./App.css";
import Cart from "./pages/cart/Cart";
import cartStyles from "./pages/cart/cart.module.css";

function App() {
const [count, setCount] = useState(0);

const [isCartOpen, setIsCartOpen] = useState(false);
return (
<>
<div>
<a href="https://vitejs.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
{/* TODO: Add button to main navigation */}
<button
className={cartStyles.cartBtn}
onClick={() => setIsCartOpen(true)}
>
🛒
</button>
<Cart isOpen={isCartOpen} onClose={() => setIsCartOpen(false)} />
</>
);
}
Expand Down
20 changes: 20 additions & 0 deletions client/src/components/Button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type ButtonProps<T> = {
action: (args?: T) => void;
children: React.ReactNode;
className?: string;
};

export default function Button<T>({
action,
children,
className,
}: ButtonProps<T>) {
function handleClick() {
action();
larkinds marked this conversation as resolved.
Show resolved Hide resolved
}
return (
<button className={className} onClick={handleClick}>
{children}
</button>
);
}
47 changes: 47 additions & 0 deletions client/src/components/Modal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useEffect, useRef } from "react";

type ModalProps = {
isOpen: boolean;
onClose: () => void;
children: React.ReactNode;
className?: string;
};

export default function Modal({
onClose,
isOpen,
children,
className,
}: ModalProps) {
const modalRef = useRef<HTMLDialogElement | null>(null);

useEffect(() => {
const { current: el } = modalRef;

if (!el) return;

if (isOpen) {
el.showModal();
} else {
el.close();
}
}, [isOpen]);
return (
<dialog ref={modalRef} className={className}>
<button
onClick={onClose}
style={{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to keep my inline styles to less than 3 - what do you think of using that as our standard for the project, and moving these styles into a css file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moving these still out of here and passing the classes in as a prop instead.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks!

border: "none",
backgroundColor: "inherit",
position: "absolute",
right: "1rem",
marginBottom: "1rem",
cursor: "pointer",
}}
>
X
</button>
{children}
</dialog>
);
}
4 changes: 4 additions & 0 deletions client/src/components/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Button from "./Button";
import Modal from "./Modal";

export { Button, Modal };
2 changes: 1 addition & 1 deletion client/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App.tsx";
import "./index.css";
// import "./index.css";

ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
Expand Down
137 changes: 137 additions & 0 deletions client/src/pages/cart/Cart.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { useState } from "react";
import { Button, Modal } from "../../components";
import styles from "./cart.module.css";

type Item = {
id: number;
image: string;
flavor: string;
price: number;
quantity: number;
};

const itemList: Item[] = [
{
id: 1,
image:
"https://media.istockphoto.com/id/980474978/vector/strawberry-ice-cream-cone-flat-design-dessert-icon.jpg?s=612x612&w=0&k=20&c=kY7enczOhemyXVu5Jp2pmVbv5SfQPj03zcqb27fJv4I=",
flavor: "strawberry",
price: 0,
quantity: 1,
},
{
id: 2,
image:
"https://img.freepik.com/free-vector/chocolate-ice-creame-waffle-cone-sticker_1308-68693.jpg?w=2000",
flavor: "chocolate",
price: 0,
quantity: 3,
},
{
id: 3,
image:
"https://thumbs.dreamstime.com/b/ice-cream-cone-vector-cartoon-illustration-vanilla-213405239.jpg",
flavor: "vanilla",
price: 0,
quantity: 2,
},
];

type CartProps = {
isOpen: boolean;
onClose: () => void;
};

export default function Cart({ isOpen, onClose }: CartProps) {
const [items, setItems] = useState(itemList);

function handleDeleteItem(id: Item["id"]) {
setItems((items) => items.filter((item) => item.id !== id));
}

function handleUpdateQuantity(id: Item["id"], newQuantity: Item["quantity"]) {
const updatedItems = items.map((item) => {
if (item.id === id) {
return { ...item, quantity: newQuantity };
}
return item;
});

setItems(updatedItems);
}

return (
<Modal isOpen={isOpen} onClose={onClose} className={styles.modal}>
{/* Currently getting data from a constant but will eventually either need to make this dynamic */}
{items.map((item) => (
<CartItem key={item.id}>
<ItemDetails item={item} />
<div className={styles.actionBtns}>
<RemoveBtn itemId={item.id} onDeleteItem={handleDeleteItem} />
<Quantity item={item} onUpdate={handleUpdateQuantity} />
</div>
</CartItem>
))}
</Modal>
);
}

function CartItem({ children }: { children: React.ReactNode }) {
return <div className={styles.cartItem}>{children}</div>;
}

function RemoveBtn({
onDeleteItem,
itemId,
}: {
onDeleteItem: (id: Item["id"]) => void;
itemId: Item["id"];
}) {
return (
<Button className={styles.removeBtn} action={() => onDeleteItem(itemId)}>
Remove
</Button>
);
}

function ItemDetails({ item }: { item: Item }) {
return (
<div className={styles.details}>
<div className={styles.flavor}>
<img src={item.image} alt={item.flavor} />
<span> {item.flavor} </span>
</div>
<p> ${item.price} </p>
</div>
);
}

function Quantity({
item,
onUpdate,
}: {
item: Item;
onUpdate: (id: Item["id"], newQuantity: Item["quantity"]) => void;
}) {
function handleIncrement() {
onUpdate(item.id, item.quantity + 1);
}

function handleDecrement() {
if (item.quantity > 0) {
onUpdate(item.id, item.quantity - 1);
}
}

return (
<div className={styles.quantity}>
<button className={styles.minus} onClick={handleDecrement}>
-
</button>
<span> {item.quantity} </span>
<button className={styles.plus} onClick={handleIncrement}>
+
</button>
</div>
);
}
88 changes: 88 additions & 0 deletions client/src/pages/cart/cart.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
.modal {
height: 100vh;
width: 100%;
left: inherit;
}

.cartBtn {
position: absolute;
top: 0;
right: 10px;
background: transparent;
border: none;
font-size: 2rem;
cursor: pointer;
outline: none;
padding: 0.5rem;
}

.cartBtn:hover {
background: #f5f5f5;
}

.cartItem {
margin: 30px 0;
}

.details {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
font-size: 1.25rem;
}

.details p {
margin: 0 15px 0 0;
}

.flavor {
display: flex;
flex-direction: row;
align-items: center;
width: 100%;
}

.flavor img {
width: 65px;
height: 65px;
object-fit: contain;
}

.actionBtns {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}

.actionBtns button {
background: transparent;
border: none;
font-size: 1rem;
cursor: pointer;
outline: none;
}

.removeBtn {
margin-left: 60px;
}

.removeBtn:hover {
background: #f5f5f5;
}

.quantity span {
border: 1px solid #333;
border-radius: 50%;
}

.plus {
padding-right: 0;
}

@media (min-width: 768px) {
.modal {
width: 300px;
}
}