Skip to content

Commit

Permalink
Merge pull request #33 from CalcagnoLoic/develop
Browse files Browse the repository at this point in the history
Adding HOC for basket
  • Loading branch information
CalcagnoLoic authored Jan 25, 2024
2 parents 053deda + 41db353 commit a883d96
Show file tree
Hide file tree
Showing 18 changed files with 123 additions and 153 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# e-commerce
# Sneakers Company E-Commerce

![](public/assets/img/readme.jpg)
![](public/assets/img/readme.png)
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="description" content="Building ecommerce" />
<link rel="icon" type="image/svg+xml" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ecommerce | sneakers</title>
<title>Sneakers Company</title>
</head>
<body>
<div id="root"></div>
Expand Down
Binary file removed public/assets/img/readme.jpg
Binary file not shown.
Binary file added public/assets/img/readme.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 3 additions & 7 deletions src/Pages/ProductPage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { description } from "../../data";

import Button from "../../components/Button";
import Counter from "../../components/Counter";
import AddToCart from "../../container/AddToCart";
import Footer from "../../components/Footer";
import Header from "../../components/Header";
import MainContentLayout from "../../layouts/MainContentLayout";
Expand All @@ -12,7 +11,7 @@ import Heading from "../../typographies/Heading";
import Paragraph from "../../typographies/Paragraph";
import ProductLayout from "../../layouts/ProductLayout";

function Page() {
const Page = () => {
return (
<div className="md:mx-32 lg:mx-24 xl:mx-40">
<Header />
Expand Down Expand Up @@ -48,10 +47,7 @@ function Page() {
<ProductPrice isOnSale={true} price={"$250.00"} />
</div>

<div className="mt-8 flex flex-col lg:flex-row">
<Counter />
<Button />
</div>
<AddToCart />
</div>
</ProductLayout>
</MainContentLayout>
Expand Down
6 changes: 4 additions & 2 deletions src/components/Button/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import * as ButtonStories from './index.stories';

# Button

A button is a component that can be clicked to add one or more product into basket
A button is a component that can be clicked to make or not an action.

<Canvas of={ButtonStories.Default} />
<Canvas of={ButtonStories.AddToCart} />

<Canvas of={ButtonStories.Checkout} />
13 changes: 0 additions & 13 deletions src/components/Button/index.stories.ts

This file was deleted.

28 changes: 28 additions & 0 deletions src/components/Button/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Meta, StoryObj } from "@storybook/react";

import BasketIcons from "../../Icons/BasketIcons";
import Button from "./index";

const meta = {
title: "Components/Button",
component: Button,
} satisfies Meta<typeof Button>;

export default meta;
type Story = StoryObj<typeof meta>;

export const AddToCart: Story = {
args: {
onClick: () => {},
content: "Add To Cart",
icon: <BasketIcons color="light" css="icon-to-cart mr-5" />,
css: "button-wrapper",
},
};

export const Checkout: Story = {
args: {
content: "Checkout",
css: "button-basket",
},
};
27 changes: 19 additions & 8 deletions src/components/Button/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import { useBasketContext } from "../../hooks/useBasketContext";

import BasketIcons from "../../Icons/BasketIcons";
import Paragraph from "../../typographies/Paragraph";

const Component = () => {
const { copyQuantity } = useBasketContext();
interface ButtonType {
onClick?: () => void;
content: string;
icon?: React.ReactNode;
css: string;
}

const Component = ({ onClick, content, icon, css }: ButtonType) => {
if (onClick) {
return (
<div className={css} onClick={() => onClick()}>
{icon}
<Paragraph kind="span" content={content} css="button-text" />
</div>
);
}

return (
<div className="button-wrapper" onClick={() => copyQuantity()}>
<BasketIcons color="light" css="icon-to-cart mr-5" />
<Paragraph kind="span" content="Add to Cart" css="button-text" />
<div className={css}>
{icon}
<Paragraph kind="span" content={content} css="button-text" />
</div>
);
};
Expand Down
7 changes: 6 additions & 1 deletion src/components/Counter/index.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ const meta = {
export default meta;
type Story = StoryObj<typeof meta>;

export const Default: Story = {};
export const Default: Story = {
args: {
onClick: () => {},
quantity: 0,
},
};
15 changes: 8 additions & 7 deletions src/components/Counter/index.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { useBasketContext } from "../../hooks/useBasketContext";

import Paragraph from "../../typographies/Paragraph";
import QuantityIcons from "../../Icons/QuantityIcons";

const Component = () => {
const { counter, addItemToBasket, removeItemToBasket } = useBasketContext();
interface CounterType {
onClick: (newValue: string) => void;
quantity: number;
}

const Component = ({ onClick, quantity }: CounterType) => {
return (
<div className="counter-wrapper">
<button
onClick={() => {
removeItemToBasket();
onClick("sub");
}}
>
<QuantityIcons kind="minus" />
</button>

<Paragraph kind="span" content={counter} css="px-3" />
<Paragraph kind="span" content={quantity} css="px-3" />

<button
onClick={() => {
addItemToBasket();
onClick("add");
}}
>
<QuantityIcons kind="plus" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import DeleteIcon from "../../../../Icons/DeleteIcon";
import Heading from "../../../../typographies/Heading";
import Paragraph from "../../../../typographies/Paragraph";
import PRODUCT_THUMBNAIL from "/assets/img/product-1-thumbnail.jpg";
import Button from "../../../Button";

const Component = () => {
const { quantity, removeToBasket } = useBasketContext();
Expand Down Expand Up @@ -37,7 +38,7 @@ const Component = () => {
<DeleteIcon handleClick={() => removeToBasket()} />
</div>

<button className="button-basket">Checkout</button>
<Button content="Checkout" css="button-basket" />
</>
);
};
Expand Down
4 changes: 2 additions & 2 deletions src/components/Dropdown/BasketDropdown/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BasketProvider } from "../../../context/Basket/BasketContext";
import { render, screen, waitFor } from "@testing-library/react";
import { render } from "@testing-library/react";
import { vi } from "vitest";

import BasketDropdown from "./index";
import "@testing-library/jest-dom";
import Counter from "../../../components/Counter";
//import Counter from "../../../components/Counter";
import userEvent from "@testing-library/user-event";

describe("Basket Dropdown Suite Test", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Component = () => (
target="_blank"
className="ml-1"
>
@CalcagnoLoic
@CalcagnoLoic 🎉
</a>
</>
}
Expand Down
39 changes: 39 additions & 0 deletions src/container/AddToCart/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useBasketContext } from "../../hooks/useBasketContext";
import { useState } from "react";

import BasketIcons from "../../Icons/BasketIcons";
import Button from "../../components/Button";
import Counter from "../../components/Counter";

const Component = () => {
const [quantity, setQuantity] = useState<number>(0);
const { addToCart } = useBasketContext();

const handleCounter = (kind: string) => {
if (kind === "add") {
setQuantity(quantity + 1);
} else {
setQuantity(quantity - 1);
}
};

const handleSubmit = () => {
if (quantity >= 0) {
addToCart(quantity);
}
};

return (
<div className="mt-8 flex flex-col lg:flex-row">
<Counter onClick={handleCounter} quantity={quantity} />
<Button
onClick={handleSubmit}
content="Add To Cart"
icon={<BasketIcons color="light" css="icon-to-cart mr-5" />}
css="button-wrapper"
/>
</div>
);
};

export default Component;
33 changes: 7 additions & 26 deletions src/context/Basket/BasketContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ import { createContext, Context, useState } from "react";

interface BasketContext {
quantity: number;
counter: number;
addItemToBasket: () => void;
removeItemToBasket: () => void;
copyQuantity: () => void;
addToCart: (quantity: number) => void;
removeToBasket: () => void;
}

export const BasketContext: Context<BasketContext> = createContext({
quantity: 0,
counter: 0,
addItemToBasket: () => {},
removeItemToBasket: () => {},
copyQuantity: () => {},
addToCart: (quantity) => {
console.log(quantity);
},
removeToBasket: () => {},
});

Expand All @@ -24,21 +20,9 @@ export const BasketProvider = ({
children: JSX.Element | JSX.Element[];
}) => {
const [quantity, setQuantity] = useState<number>(0);
const [counter, setCounter] = useState<number>(0);

const addItemToBasket = () => {
setCounter((prevCounter) => prevCounter + 1);
};

const removeItemToBasket = () => {
if (counter > 0) {
setCounter((prevCounter) => prevCounter - 1);
}
};

const copyQuantity = () => {
setQuantity(counter);
setCounter(0);
const addToCart = (quantity: number) => {
setQuantity(quantity);
};

const removeToBasket = () => {
Expand All @@ -47,10 +31,7 @@ export const BasketProvider = ({

const ContextValue: BasketContext = {
quantity,
counter,
addItemToBasket,
removeItemToBasket,
copyQuantity,
addToCart,
removeToBasket,
};

Expand Down
Loading

0 comments on commit a883d96

Please sign in to comment.