forked from infoshareacademy/jfdzr12-project-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from infoshareacademy/hriabokonenko/basket
Hriabokonenko/basket
- Loading branch information
Showing
8 changed files
with
122 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
my-app/src/Components/BasketModal/BasketItem/BasketItem.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.basket-item__product-image { | ||
height: 100px; | ||
} |
29 changes: 29 additions & 0 deletions
29
my-app/src/Components/BasketModal/BasketItem/BasketItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, { useContext } from 'react'; | ||
import styles from "./BasketItem.module.css"; | ||
import classNames from 'classnames'; | ||
import { ThemeContext } from '../../../providers/theme'; | ||
|
||
interface Product { | ||
author: string; | ||
height: number; | ||
id: number; | ||
price: number; | ||
url: string; | ||
width: number; | ||
} | ||
|
||
interface BasketItemProps { | ||
product: Product; | ||
} | ||
|
||
export const BasketItem: React.FC<BasketItemProps> = ({ product }) => { | ||
const { theme } = useContext(ThemeContext); | ||
|
||
return ( | ||
<div> | ||
<h3>{product.author}</h3> | ||
<img className={classNames(styles["basket-item__product-image"], styles[theme])} src={product.url}></img> | ||
<p>{product.price}$</p> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React, { PropsWithChildren, createContext, useContext, useState } from 'react'; | ||
import { Product } from "./basketContext.types" | ||
|
||
interface BasketContextType { | ||
basket: Product[]; | ||
addToBasket: (product: Product) => void; | ||
removeFromBasket: (productId: number) => void; | ||
} | ||
|
||
const BasketContext = createContext<BasketContextType | undefined>(undefined); | ||
|
||
const BasketProvider: React.FC = ({ children }: PropsWithChildren) => { | ||
const [basket, setBasket] = useState<Product[]>([]); | ||
|
||
const addToBasket = (product: Product) => { | ||
setBasket([...basket, product]); | ||
} | ||
|
||
const removeFromBasket = (productId: number) => { | ||
setBasket(basket.filter(item => item.id !== productId)); | ||
} | ||
|
||
return ( | ||
<BasketContext.Provider value={{ basket, addToBasket, removeFromBasket }}> | ||
{children} | ||
</BasketContext.Provider> | ||
); | ||
} | ||
|
||
const useBasket = () => { | ||
const context = useContext(BasketContext); | ||
if (!context) { | ||
throw new Error('useBasket must be used within a BasketProvider'); | ||
} | ||
return context; | ||
} | ||
|
||
export { BasketProvider, useBasket, BasketContext }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface Product { | ||
author: string; | ||
height: number; | ||
id: number; | ||
price: number; | ||
url: string; | ||
width: number; | ||
} |
This file was deleted.
Oops, something went wrong.