From 9176b07e2662456c81627598092ed3d5d85d1b55 Mon Sep 17 00:00:00 2001 From: Julian Lechner Date: Tue, 7 May 2024 23:11:27 -0400 Subject: [PATCH] Add to cart w/ context --- frontend/src/components/shared/cart/Cart.tsx | 4 ++ .../ui/buttons/AddToCart/AddToCart.tsx | 47 +++++++++++-- .../ui/cards/ProductCard/ProductCard.tsx | 4 +- frontend/src/pages/product/ProductPage.tsx | 18 +++-- frontend/src/types/CartTypes.ts | 34 +++++++-- frontend/src/types/types.ts | 70 ++++++++++++------- 6 files changed, 136 insertions(+), 41 deletions(-) diff --git a/frontend/src/components/shared/cart/Cart.tsx b/frontend/src/components/shared/cart/Cart.tsx index 337b18b..ad8d9c7 100644 --- a/frontend/src/components/shared/cart/Cart.tsx +++ b/frontend/src/components/shared/cart/Cart.tsx @@ -8,6 +8,10 @@ import { CartContext } from "../../../App"; export default function Cart() { const [open, setOpen] = useState(false); const { cart, setCart } = useContext(CartContext); + + React.useEffect(() => { + console.log(cart); + }, [cart]); return ( <> + {product ? ( + ) : ( )} diff --git a/frontend/src/components/ui/cards/ProductCard/ProductCard.tsx b/frontend/src/components/ui/cards/ProductCard/ProductCard.tsx index c71de9e..c4b00aa 100644 --- a/frontend/src/components/ui/cards/ProductCard/ProductCard.tsx +++ b/frontend/src/components/ui/cards/ProductCard/ProductCard.tsx @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useState, createContext } from "react"; import Box from "@mui/material/Box"; import Card from "@mui/material/Card"; import CardActions from "@mui/material/CardActions"; @@ -9,6 +9,8 @@ import CardMedia from "@mui/material/CardMedia"; import { CardActionArea } from "@mui/material"; import { AWS_S3_BASE_URL } from "../../../../constants"; + + export default function ProductCard({ product }: any) { return ( (); - const [loading, setLoading] = React.useState(true); + const [productInfo, setProductInfo] = useState(); + const [color, setColor] = useState('red'); + const [size, setSize] = useState('sm'); + const [loading, setLoading] = useState(true); + const { cart, setCart } = useContext(CartContext); React.useEffect(() => { const fetchProduct = async () => { if (productID) { const response = await getProductInfo(productID); - setProductInfo(response); + const product = new Product(response); + setProductInfo(product); setLoading(false); } }; @@ -74,7 +82,7 @@ export default function ProductPage() {
- +

diff --git a/frontend/src/types/CartTypes.ts b/frontend/src/types/CartTypes.ts index 44080ea..bbaed76 100644 --- a/frontend/src/types/CartTypes.ts +++ b/frontend/src/types/CartTypes.ts @@ -1,10 +1,15 @@ -import React, { createContext, useState, useContext, Dispatch, SetStateAction } from 'react'; -import { IProduct } from './types'; +import React, { + createContext, + useState, + useContext, + Dispatch, + SetStateAction, +} from "react"; +import { Product } from "./types"; // Define the type for the cart export interface ICartItem { - id: number; - product: IProduct; + product: Product; quantity: number; size: string; color: string; @@ -14,4 +19,23 @@ export interface ICartItem { export interface ICartContext { cart: ICartItem[]; setCart: Dispatch>; -} \ No newline at end of file +} + +export class CartItem implements ICartItem { + product: Product; + quantity: number; + size: string; + color: string; + + constructor( + product: Product, + quantity: number, + size: string, + color: string + ) { + this.product = product; + this.quantity = quantity; + this.size = size; + this.color = color; + } +} diff --git a/frontend/src/types/types.ts b/frontend/src/types/types.ts index 701ccfd..3ea6d53 100644 --- a/frontend/src/types/types.ts +++ b/frontend/src/types/types.ts @@ -3,39 +3,61 @@ export interface IProduct { product_type: string; product_name: string; price: number; - description: string; + sale_price: number; + blurb: string; stock_quantity: number; image_url: string; } -export interface IOrderItem { - id: number; - order_id: number; - product_id: number; - quantity: number; +export class Product implements IProduct { + product_id: string; + product_type: string; + product_name: string; + sale_price: number; + price: number; + blurb: string; + stock_quantity: number; + image_url: string; + + constructor(product: IProduct) { + this.product_id = product.product_id; + this.product_type = product.product_type; + this.product_name = product.product_name; + this.price = product.price; + this.sale_price = product.sale_price; + this.blurb = product.blurb; + this.stock_quantity = product.stock_quantity; + this.image_url = product.image_url; } - +} + +export interface IOrderItem { + id: number; + order_id: number; + product_id: number; + quantity: number; +} + export interface IOrder { - id: number; - user_id: number; - created_at: Date; - items: IOrderItem[]; - } + id: number; + user_id: number; + created_at: Date; + items: IOrderItem[]; +} export interface IReview { - id: number; - product_id: number; - author_id: number; - content: string; - rating: number; + id: number; + product_id: number; + author_id: number; + content: string; + rating: number; } export interface IUser { - id: number; - username: string; - email: string; - is_active?: boolean; // Optional + id: number; + username: string; + email: string; + is_active?: boolean; // Optional } - - -export default {} \ No newline at end of file + +export default {};