Skip to content

Commit

Permalink
add BasteItems component, add solution to adding iem po basket and sh…
Browse files Browse the repository at this point in the history
…ow item at basket
  • Loading branch information
HalynaRiabokonenko committed Mar 3, 2024
1 parent d5848d6 commit 8aec068
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
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 my-app/src/Components/BasketModal/BasketItem/BasketItem.tsx
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>
);
}
14 changes: 13 additions & 1 deletion my-app/src/Components/BasketModal/BasketModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import styles from "./BasketModal.module.css";
import { Modal } from "../Modal/Modal.jsx";
import { useContext, useEffect } from "react";
import { ThemeContext } from "../../providers/theme.tsx";
import { BasketContext } from "../../providers/BasketContext";
import { BasketItem } from "../BasketModal/BasketItem/BasketItem.tsx";

export const BasketModal = ({ isOpen, onClose }) => {
const { theme } = useContext(ThemeContext);
const { basket } = useContext(BasketContext);
console.log("BASKET", basket);

useEffect(() => {
const handleKeyDown = (event) => {
Expand Down Expand Up @@ -42,7 +46,15 @@ export const BasketModal = ({ isOpen, onClose }) => {
x
</button>
<div>
<h1>Basket modal</h1>
{basket.length > 0 ? (
<div>
{basket.map((product, index) => (
<BasketItem key={index} product={product} />
))}
</div>
) : (
<div>Empty basket</div>
)}
</div>
</Modal>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import { useContext, useState, useEffect } from "react";
import classnames from "classnames";
import { db } from "../../../../firebase-config";
import { doc, getDoc } from "firebase/firestore";
import { BasketContext } from "../../../providers/BasketContext.tsx";

export const PortfolioImage = () => {
const { imageId } = useParams();
const { theme } = useContext(ThemeContext);
const [photo, setPhoto] = useState(null);
const { addToBasket } = useContext(BasketContext);

const handleClick = (product) => {
addToBasket(product);
};

useEffect(() => {
const fetchData = async () => {
Expand All @@ -25,10 +31,6 @@ export const PortfolioImage = () => {
fetchData();
}, [imageId]);

const addToBasket = () => {
// console.log("ADDED");
};

if (!photo) {
return <div>Loading...</div>;
}
Expand All @@ -47,7 +49,7 @@ export const PortfolioImage = () => {
</div>
{photo.amount ? (
<button
onClick={addToBasket}
onClick={() => handleClick(photo)}
className={classnames(styles["portfolio__input--buy"], styles[theme])}
>
Add to basket
Expand Down
2 changes: 1 addition & 1 deletion my-app/src/providers/basketContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ const useBasket = () => {
return context;
}

export { BasketProvider, useBasket };
export { BasketProvider, useBasket, BasketContext };

0 comments on commit 8aec068

Please sign in to comment.