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 17 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: 2 additions & 34 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,35 +1,3 @@
import { useState } from "react";
import reactLogo from "./assets/react.svg";
import viteLogo from "/vite.svg";
import "./App.css";

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

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>
</>
);
export default function App() {
return <div></div>;
}

export default App;
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>
);
}
39 changes: 39 additions & 0 deletions client/src/components/Modal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useEffect, useRef } from "react";

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

export default function Modal({
onClose,
isOpen,
containerClassNames,
buttonClassNames,
children,
}: 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={containerClassNames}>
<button onClick={onClose} className={buttonClassNames}>
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 };
1 change: 0 additions & 1 deletion client/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App.tsx";
import "./index.css";

ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
Expand Down
142 changes: 142 additions & 0 deletions client/src/pages/cart/Cart.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
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}
containerClassNames={styles.modal}
buttonClassNames={styles.modalCloseBtn}
>
{/* 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>
);
}
10 changes: 10 additions & 0 deletions client/src/pages/cart/CartBtn.tsx
Copy link
Owner

Choose a reason for hiding this comment

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

Oh my gosh, this icon is so cute!

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import styles from "./cart.module.css";

// TODO: This eventually needs to be part of a header component
export default function CartBtn({ handleClick }: { handleClick: () => void }) {
return (
<button className={styles.cartBtn} onClick={handleClick}>
🛒
</button>
);
}
14 changes: 14 additions & 0 deletions client/src/pages/cart/CartPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useState } from "react";
import Cart from "./Cart";
import CartBtn from "./CartBtn";

export default function CartPage() {
const [isCartOpen, setIsCartOpen] = useState(false);

return (
<>
<CartBtn handleClick={() => setIsCartOpen(true)} />
<Cart isOpen={isCartOpen} onClose={() => setIsCartOpen(false)} />
</>
);
}
Loading