Skip to content

Commit

Permalink
completely replaced context with redux
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahesh-A committed Jan 13, 2023
1 parent 29c9607 commit 6eddddf
Show file tree
Hide file tree
Showing 27 changed files with 391 additions and 120 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"react-scripts": "5.0.1",
"redux": "^4.2.0",
"redux-logger": "^3.0.6",
"reselect": "^4.1.7",
"sass": "^1.57.0",
"serve": "^14.1.2",
"styled-components": "^5.3.6",
Expand Down
9 changes: 6 additions & 3 deletions src/components/cart-dropdown/cart-dropdown.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import {
} from "./cart-dropdown.styles";
import CartItem from "../cart-item/cart-item.component.jsx";
import Button from "../button/Button.component.jsx";
import { CartContext } from "../../contexts/cart.context";
import { useContext } from "react";
// import { CartContext } from "../../contexts/cart.context";
// import { useContext } from "react";
import { useNavigate } from "react-router-dom";
import { useSelector } from "react-redux";
import { selectCartItems } from "../../store/cart/cart.selector";
const CartDropdown = () => {
const navigate = useNavigate();
const goToCheckoutHandler = () => {
navigate("/check-out");
};
const { items } = useContext(CartContext);
const items = useSelector(selectCartItems)
//const { items } = useContext(CartContext);
return (
<CartDropdownContainer>
<CartItems>
Expand Down
37 changes: 22 additions & 15 deletions src/components/cart-icon/cart-icon.component.jsx
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;
21 changes: 12 additions & 9 deletions src/components/checkout-item/checkout-item.component.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { useContext } from 'react';

import { CartContext } from '../../contexts/cart.context';
// import { useContext } from 'react';

// import { CartContext } from '../../contexts/cart.context';
import { selectCartItems } from '../../store/cart/cart.selector';
import { useSelector, useDispatch } from 'react-redux';
import{addItemsToCart, removeItemsFromCart, decreaseItemsFromCart} from '../../store/cart/cart.action';
import './checkout-item.styles.scss';

const CheckoutItem = ({ cartItem }) => {
const { name, imageUrl, price, quantity } = cartItem;
const dispatch = useDispatch();
const items = useSelector(selectCartItems);
// const { removeItemsFromCart, addItemsToCart, decreaseItemsFromCart } =
// useContext(CartContext);

const { removeItemsFromCart, addItemsToCart, decreaseItemsFromCart } =
useContext(CartContext);

const clearItemHandler = () => removeItemsFromCart(cartItem);
const addItemHandler = () => addItemsToCart(cartItem);
const removeItemHandler = () => decreaseItemsFromCart(cartItem);
const clearItemHandler = () => dispatch(removeItemsFromCart(items, cartItem));
const addItemHandler = () => dispatch(addItemsToCart(items, cartItem));
const removeItemHandler = () => dispatch(decreaseItemsFromCart(items, cartItem));

return (
<div className='checkout-item-container'>
Expand Down
63 changes: 43 additions & 20 deletions src/components/product-card/product-card.component.jsx
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;
1 change: 1 addition & 0 deletions src/contexts/cart.context.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export const CartProvider = ({ children }) => {
const newCartItems = decreaseCartItem(items, productToDecrease);
updateCartItemsReducer(newCartItems);
};

const value = {
isopen,
setIsopen,
Expand Down
10 changes: 3 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@ import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { BrowserRouter } from "react-router-dom";

import { CategoriesProvider } from "./contexts/categories.context";
import { CartProvider } from "./contexts/cart.context";
//import { CategoriesProvider } from "./contexts/categories.context";
//import { CartProvider } from "./contexts/cart.context";

import { store } from "./store/store";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<Provider store={store}>
<BrowserRouter>
<CategoriesProvider>
<CartProvider>
<App />
</CartProvider>
</CategoriesProvider>
<App />
</BrowserRouter>
</Provider>
</React.StrictMode>
Expand Down
10 changes: 6 additions & 4 deletions src/routes/categories-preview/categories-preview.component.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useContext, Fragment } from "react";
import { CategoriesContext } from "../../contexts/categories.context";
import { Fragment } from "react";
// import { CategoriesContext } from "../../contexts/categories.context";
import CategoryPreview from "../../components/category-preview/category-preview.component";

import { useSelector } from "react-redux";
import { selectCategoriesMap } from "../../store/categories/category.selector";

const CategoriesPreview = () => {
const { categoriesMap } = useContext(CategoriesContext);
const categoriesMap = useSelector(selectCategoriesMap);
// const { categoriesMap } = useContext(CategoriesContext);
return (
<Fragment>
{Object.keys(categoriesMap).map((title) => {
Expand Down
54 changes: 30 additions & 24 deletions src/routes/category/category.component.jsx
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;
10 changes: 7 additions & 3 deletions src/routes/checkout/checkout.componet.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import './checkout.styles.scss';
import CheckoutItem from '../../components/checkout-item/checkout-item.component';
import { CartContext } from '../../contexts/cart.context';
import { useContext } from 'react';
import { useSelector } from 'react-redux';
import { selectCartItems, selectCartTotal } from '../../store/cart/cart.selector';
// import { CartContext } from '../../contexts/cart.context';
// import { useContext } from 'react';

const Checkout = () => {
const { items, cartTotal } = useContext(CartContext);
const items = useSelector(selectCartItems);
const cartTotal = useSelector(selectCartTotal);
//const { items, cartTotal } = useContext(CartContext);

return (
<div className='checkout-container'>
Expand Down
15 changes: 8 additions & 7 deletions src/routes/navigation/navigation.component.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Fragment, useContext } from "react";
import { Fragment } from "react";
import { Outlet } from "react-router-dom";
//import { UserContext } from "../../contexts/user.context";
import { useSelector } from "react-redux";
import { ReactComponent as CrwnLogo } from "../../assets/crown.svg";
import { auth, googleSignOut } from "../../utils/firebase/firebse.utils";
import CartIcon from "../../components/cart-icon/cart-icon.component";
import CartDropdown from "../../components/cart-dropdown/cart-dropdown.component";
import { CartContext } from "../../contexts/cart.context";
import { selectIsCartOpen } from "../../store/cart/cart.selector";
//import { CartContext } from "../../contexts/cart.context";
import {
NavLink,
NavigationContainer,
Expand All @@ -16,9 +17,8 @@ import {
import { selectCurrentUser } from "../../store/user/user.selector";
const NavigationBar = () => {
const currentUser = useSelector(selectCurrentUser);
//const { currentUser } = useContext(UserContext);

const { isopen } = useContext(CartContext);
const isCartOpen = useSelector(selectIsCartOpen);
// const { isopen } = useContext(CartContext);

return (
<Fragment>
Expand All @@ -30,7 +30,8 @@ const NavigationBar = () => {
<NavLinksContainer>
<NavLink to={"/shop"}>Shop</NavLink>
{currentUser ? (
<NavLink as='span'
<NavLink
as="span"
onClick={() => {
googleSignOut(auth);
}}
Expand All @@ -42,7 +43,7 @@ const NavigationBar = () => {
)}
<CartIcon />
</NavLinksContainer>
{isopen && <CartDropdown />}
{isCartOpen && <CartDropdown />}
</NavigationContainer>
<Outlet />
</Fragment>
Expand Down
39 changes: 26 additions & 13 deletions src/routes/shop/shop.component.jsx
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;
Loading

0 comments on commit 6eddddf

Please sign in to comment.