-
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.
completely replaced context with redux
- Loading branch information
Showing
27 changed files
with
391 additions
and
120 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
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 |
---|---|---|
@@ -1,19 +1,26 @@ | ||
import { ShoppingIcon, CartIconContainer, ItemCount } from './cart-icon.styles'; | ||
import { CartContext } from '../../contexts/cart.context'; | ||
import { useContext } from 'react'; | ||
|
||
import { ShoppingIcon, CartIconContainer, ItemCount } from "./cart-icon.styles"; | ||
// import { CartContext } from "../../contexts/cart.context"; | ||
// import { useContext } from "react"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import { | ||
selectCartCount, | ||
selectIsCartOpen, | ||
} from "../../store/cart/cart.selector"; | ||
import { setIsCartOpen } from "../../store/cart/cart.action"; | ||
const CartIcon = () => { | ||
const { isopen, setIsopen, count} = useContext(CartContext) | ||
const clilckHandler = () => { | ||
setIsopen(!isopen); | ||
} | ||
const dispatch = useDispatch(); | ||
//const { isopen, setIsopen, count } = useContext(); | ||
const cartCount = useSelector(selectCartCount); | ||
const isCartOpen = useSelector(selectIsCartOpen); | ||
|
||
const clilckHandler = () => dispatch(setIsCartOpen(!isCartOpen)); | ||
|
||
return ( | ||
<CartIconContainer onClick={clilckHandler}> | ||
<ShoppingIcon /> | ||
<ItemCount>{count}</ItemCount> | ||
</CartIconContainer> | ||
); | ||
return ( | ||
<CartIconContainer onClick={clilckHandler}> | ||
<ShoppingIcon /> | ||
<ItemCount>{cartCount}</ItemCount> | ||
</CartIconContainer> | ||
); | ||
}; | ||
|
||
export default CartIcon; | ||
export default CartIcon; |
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 |
---|---|---|
@@ -1,25 +1,48 @@ | ||
import Button from "../button/Button.component"; | ||
import './product-card.styles.scss'; | ||
import { useContext } from 'react' | ||
import { CartContext } from "../../contexts/cart.context"; | ||
|
||
import "./product-card.styles.scss"; | ||
// import { useContext } from "react"; | ||
// import { CartContext } from "../../contexts/cart.context"; | ||
import { useSelector } from "react-redux"; | ||
import { selectCartItems } from "../../store/cart/cart.selector"; | ||
import { useDispatch } from "react-redux"; | ||
//import { createAction } from "../../utils/reducer/reducer.util"; | ||
import { addItemsToCart } from "../../store/cart/cart.action"; | ||
const ProductCard = ({ product }) => { | ||
const { addItemsToCart } = useContext(CartContext); | ||
const { name, price, imageUrl} = product; | ||
return ( | ||
<div className="product-card-container"> | ||
<img | ||
src={imageUrl} | ||
alt={`${name}`} | ||
/> | ||
<div className="footer"> | ||
<span className="name">{name}</span> | ||
<span className="price">{price}</span> | ||
const items = useSelector(selectCartItems); | ||
const dispatch = useDispatch(); | ||
|
||
// const addCartItem = (items, productToAdd) => { | ||
// const existingCartItem = items.find((item) => item.id === productToAdd.id); | ||
// if (existingCartItem) { | ||
// return items.map((item) => | ||
// item.id === productToAdd.id | ||
// ? { ...item, quantity: item.quantity + 1 } | ||
// : item | ||
// ); | ||
// } | ||
// return [...items, { ...productToAdd, quantity: 1 }]; | ||
// }; | ||
const addProductsToCart = () => dispatch(addItemsToCart(items, product)); | ||
|
||
// const add = (product) => { | ||
// const newCartItems = addCartItem(items, product); | ||
// dispatch(createAction("SET_CART_ITEMS", { items: newCartItems })); | ||
// }; | ||
//const { addItemsToCart } = useContext(CartContext); | ||
|
||
</div> | ||
<Button buttonType='inverted' onClick = {()=>addItemsToCart(product)}>Add item</Button> | ||
</div> | ||
); | ||
const { name, price, imageUrl } = product; | ||
return ( | ||
<div className="product-card-container"> | ||
<img src={imageUrl} alt={`${name}`} /> | ||
<div className="footer"> | ||
<span className="name">{name}</span> | ||
<span className="price">{price}</span> | ||
</div> | ||
<Button buttonType="inverted" onClick={addProductsToCart}> | ||
Add item | ||
</Button> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ProductCard; | ||
export default ProductCard; |
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
10 changes: 6 additions & 4 deletions
10
src/routes/categories-preview/categories-preview.component.jsx
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 |
---|---|---|
@@ -1,28 +1,34 @@ | ||
import './category.styles.scss'; | ||
import { Fragment, useContext, useEffect, useState } from 'react'; | ||
import { useParams } from 'react-router-dom'; | ||
import { CategoriesContext } from '../../contexts/categories.context'; | ||
import ProductCard from '../../components/product-card/product-card.component'; | ||
import "./category.styles.scss"; | ||
import { Fragment, useEffect, useState } from "react"; | ||
import { useParams } from "react-router-dom"; | ||
//import { CategoriesContext } from '../../contexts/categories.context'; | ||
import ProductCard from "../../components/product-card/product-card.component"; | ||
import { useSelector } from "react-redux"; | ||
import { selectCategoriesMap } from "../../store/categories/category.selector"; | ||
const Category = () => { | ||
const { category } = useParams(); | ||
const { categoriesMap } = useContext(CategoriesContext); | ||
const [products, setProducts] = useState(categoriesMap[category]); | ||
console.log("rendering and rerendering category component"); | ||
|
||
useEffect(() => { | ||
const filteredProducts = categoriesMap[category]; | ||
//console.log(filteredProducts); | ||
setProducts(filteredProducts); | ||
}, [category, categoriesMap]); | ||
const { category } = useParams(); | ||
const categoriesMap = useSelector(selectCategoriesMap); | ||
const [products, setProducts] = useState(categoriesMap[category]); | ||
|
||
|
||
return ( | ||
<Fragment> | ||
<h2 className='category-title'>{category.toUpperCase()}</h2> | ||
<div className='category-container'> | ||
{products && products.map((product) => <ProductCard key={product.id} product={product} />)} | ||
</div> | ||
</Fragment> | ||
); | ||
} | ||
useEffect(() => { | ||
const filteredProducts = categoriesMap[category]; | ||
console.log("effect fired calling setproducts"); | ||
setProducts(filteredProducts); | ||
}, [category, categoriesMap]); | ||
|
||
export default Category; | ||
return ( | ||
<Fragment> | ||
<h2 className="category-title">{category.toUpperCase()}</h2> | ||
<div className="category-container"> | ||
{products && | ||
products.map((product) => ( | ||
<ProductCard key={product.id} product={product} /> | ||
))} | ||
</div> | ||
</Fragment> | ||
); | ||
}; | ||
|
||
export default Category; |
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 |
---|---|---|
@@ -1,16 +1,29 @@ | ||
import {Routes, Route} from 'react-router-dom' | ||
import './shop.styles.scss'; | ||
import CategoriesPreview from '../categories-preview/categories-preview.component'; | ||
import Category from '../category/category.component'; | ||
import { Routes, Route } from "react-router-dom"; | ||
import "./shop.styles.scss"; | ||
import CategoriesPreview from "../categories-preview/categories-preview.component"; | ||
import Category from "../category/category.component"; | ||
import { useDispatch } from "react-redux"; | ||
import { useEffect } from "react"; | ||
import { getCategoriesAndDocs } from "../../utils/firebase/firebse.utils"; | ||
import { setCategoriesMap } from "../../store/categories/category.action"; | ||
const Shop = () => { | ||
|
||
return ( | ||
<Routes> | ||
<Route index element = {<CategoriesPreview />}/> | ||
<Route path=':category' element={<Category/>}/> | ||
</Routes> | ||
); | ||
} | ||
const dispatch = useDispatch(); | ||
|
||
export default Shop; | ||
useEffect(() => { | ||
const getCategoriesMap = async () => { | ||
const categoriesArray = await getCategoriesAndDocs(); | ||
console.log(categoriesArray); | ||
dispatch(setCategoriesMap(categoriesArray)); | ||
}; | ||
getCategoriesMap(); | ||
}, [dispatch]); | ||
|
||
return ( | ||
<Routes> | ||
<Route index element={<CategoriesPreview />} /> | ||
<Route path=":category" element={<Category />} /> | ||
</Routes> | ||
); | ||
}; | ||
|
||
export default Shop; |
Oops, something went wrong.