Skip to content

Commit

Permalink
refac: directory and slight code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
PS1242 committed Jun 26, 2023
1 parent 537b639 commit fc9db6f
Show file tree
Hide file tree
Showing 12 changed files with 14,659 additions and 3,249 deletions.
17,689 changes: 14,551 additions & 3,138 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createContext, useEffect, useState } from "react";
import ProductsPage from "./ProductsPage/ProductsPage";
import Cart from "./Cart/Cart";
import ProductsPage from "./products/ProductsPage";
import Cart from "./cart/Cart";
import { getProducts } from "./api/api";
import "./App.css";

Expand Down
38 changes: 35 additions & 3 deletions src/Cart/Cart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,34 @@ import CartItem from "./CartItem";
import Total from "./Total";

function Cart() {
const { cartItems } = useContext(CartContext);
const { cartItems, setCartItems, setProducts, products } =
useContext(CartContext);

const increaseQuantity = (id) => {
const updatedData = cartItems.map((item) => ({
...item,
quantity: item.id === id ? item.quantity + 1 : item.quantity,
}));
setCartItems(updatedData);
};

const decreaseQuantity = (id, quantity) => {
// If quantity is only 1, remove this item from cart
if (quantity === 1) {
const updatedData = products.map((item) => ({
...item,
addedToCart: item.id === id ? false : item.addedToCart,
}));
setProducts(updatedData);
setCartItems((items) => items.filter((item) => item.id !== id));
} else {
const updatedData = cartItems.map((item) => ({
...item,
quantity: item.id === id ? item.quantity - 1 : item.quantity,
}));
setCartItems(updatedData);
}
};

return (
<>
Expand All @@ -14,11 +41,16 @@ function Cart() {
</div>
<div className={styles.cartItems}>
{cartItems.map((item) => (
<CartItem key={item.id} data={item} />
<CartItem
key={item.id}
data={item}
increaseQuantity={increaseQuantity}
decreaseQuantity={decreaseQuantity}
/>
))}
</div>
<div className={styles.total}>
<Total />
<Total cartItems={cartItems} />
</div>
</>
);
Expand Down
40 changes: 6 additions & 34 deletions src/Cart/CartItem.jsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,21 @@
import { useContext } from "react";
import styles from "./CartItem.module.css";
import { CartContext } from "../App";

function CartItem({ data }) {
const { cartItems, setCartItems, setProducts, products } =
useContext(CartContext);

const increaseQuantity = () => {
const updatedData = cartItems.map((item) => ({
...item,
quantity: item.id === data.id ? item.quantity + 1 : item.quantity,
}));
setCartItems(updatedData);
};

const decreaseQuantity = () => {
// If quantity is only 1, remove this item from cart
if (data.quantity === 1) {
const updatedData = products.map((item) => ({
...item,
addedToCart: item.id === data.id ? false : item.addedToCart,
}));
setProducts(updatedData);
setCartItems((items) => items.filter((item) => item.id !== data.id));
} else {
const updatedData = cartItems.map((item) => ({
...item,
quantity: item.id === data.id ? item.quantity - 1 : item.quantity,
}));
setCartItems(updatedData);
}
};

function CartItem({ data, increaseQuantity, decreaseQuantity }) {
return (
<div className={styles.cartItemContainer}>
<div className={styles.item}>
<div className={styles.imageContainer}>
<img className={styles.image} src={data.thumbnail} alt="item-image" />
</div>
<div className={styles.controls}>
<button type="button" onClick={decreaseQuantity}>
<button
type="button"
onClick={() => decreaseQuantity(data.id, data.quantity)}
>
-
</button>
<p>{data.quantity}</p>
<button type="button" onClick={increaseQuantity}>
<button type="button" onClick={() => increaseQuantity(data.id)}>
+
</button>
</div>
Expand Down
9 changes: 3 additions & 6 deletions src/Cart/Total.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { useContext } from "react";
import { CartContext } from "../App";
import styles from "./Total.module.css";

function Total() {
const { cartItems } = useContext(CartContext);
const total = cartItems.reduce(
function Total({ cartItems: data }) {
const total = data.reduce(
(curr, next) => curr + next.quantity * next.price,
0
);

return (
<>
{cartItems.length > 0 ? (
{data.length > 0 ? (
<div className={styles.checkoutSection}>
<p className={styles.totalText}>Total: {total}</p>
<button className={styles.checkoutButton}>Checkout</button>
Expand Down
48 changes: 0 additions & 48 deletions src/ProductsPage/Products/Card.jsx

This file was deleted.

17 changes: 0 additions & 17 deletions src/ProductsPage/Products/Products.jsx

This file was deleted.

24 changes: 24 additions & 0 deletions src/products/Products/Card.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from "react";
import styles from "./Card.module.css";

function Card({ data, addToCart }) {
return (
<div className={styles.dataCard}>
<div className={styles.imageContainer}>
<img src={data.thumbnail} alt="" className={styles.productImage} />
</div>
<p className={styles.productTitle}>{data.title}</p>
<p>${data.price}</p>
<button
type="button"
className={styles.addToCartButton}
style={{ backgroundColor: data.addedToCart ? "#A3BE8C" : "#5E81AC" }}
onClick={() => addToCart(data)}
>
{data.addedToCart ? "Added" : "Add to cart"}
</button>
</div>
);
}

export default Card;
File renamed without changes.
38 changes: 38 additions & 0 deletions src/products/Products/Products.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useContext } from "react";
import Card from "./Card";
import { CartContext } from "../../App";

function Products() {
const { products, setCartItems, setProducts } = useContext(CartContext);

const addToCart = (data) => {
if (data.addedToCart) return;

const updateData = products.map((item) => ({
...item,
addedToCart: item.id === data.id ? true : item.addedToCart,
}));
setProducts(updateData);

setCartItems((prev) => [
...prev,
{
id: data.id,
title: data.title,
price: data.price,
quantity: 1,
thumbnail: data.thumbnail,
},
]);
};

return (
<>
{products.map((product) => (
<Card key={product.id} data={product} addToCart={addToCart} />
))}
</>
);
}

export default Products;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import styles from "./ProductsPage.module.css";
import Products from "./Products/Products";

function ProductsPage() {

return (
<>
<div className={styles.heading}>
Expand Down
File renamed without changes.

0 comments on commit fc9db6f

Please sign in to comment.