Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add favorite button #3

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions components/product/favorite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { SingleProductComponent } from "./single";

export const FavoriteProductComponent = ({
products = [],
handleClearCart,
handleRemoveFromCart,
}) => {


return (
<>
<div className="flex justify-between py-4 px-3">
<h3 className="text-lg font-bold">Your Favorite Cart</h3>
<button className="bg-red-400 p-2" onClick={handleClearCart}>
Empty cart
</button>
</div>
<div className="flex">
{products.map((product) => (
<SingleProductComponent
key={product.name}
product={product}
onRemoveFromCartClick={handleRemoveFromCart}
/>
))}
</div>
</>
);
};
5 changes: 4 additions & 1 deletion components/product/list.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { SingleProductComponent } from "./single";

export const ListProductComponent = ({ products = [], handleAddToCartClick }) => {
export const ListProductComponent = ({ products = [], handleAddToCartClick, handleAddFavoriteCart}) => {
return (
<div className="flex">
{products.map((product) => (
<SingleProductComponent
key={product.name}
product={product}
onAddToCartClick={handleAddToCartClick}
onAddToFavorite={handleAddFavoriteCart}


/>
))}
</div>
Expand Down
32 changes: 31 additions & 1 deletion components/product/single.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import Image from "next/image";
import React, {useState} from "react";



export const SingleProductComponent = ({
product = {},
onAddToCartClick,
onRemoveFromCartClick,
onAddToFavorite,
onRemoveFromFavorite

}) => {

const [click, setClick] = useState(false)
const [showFavorite, setShowFavorite] = useState(true)


return (
<div className="p-4 shadow rounded-sm mx-3">
<Image
Expand All @@ -23,12 +35,30 @@ export const SingleProductComponent = ({
className="px-3 py-2 bg-indigo-200 text-gray-800"
onClick={(e) => {
e.preventDefault();
onAddToCartClick(product);
onAddToCartClick(product);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit weird 1 space extra indentation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry i didn't notice those error..

}}
>
Add to cart
</button>
)}


{showFavorite && !!onAddToFavorite && (

<button className="px-3 py-2 mx-3 bg-indigo-200 text-gray-800"
onClick={(e) => {
e.preventDefault();
onAddToFavorite(product);
setClick(!click);
}}
>
{!!click ? "Remove Favorite❌" : "Add favorite 👍🏼"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

</button>

)}



{!!onRemoveFromCartClick && (
<button
className="px-3 py-2 bg-indigo-200 text-gray-800"
Expand Down
4 changes: 4 additions & 0 deletions data/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ db.version(1).stores({
cartItems: `name, price, description, image, count`,
});

db.version(2).stores({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why create a different db for this, you could just add favoriteItems store in the previous definition along with cartItems, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought i need another db for this favorite item list . So i used this one.

favoriteItems: `name, price, description, image, count`,
});

export default db;
5 changes: 2 additions & 3 deletions hooks/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ export const useCart = () => {
}

setCartItems(newCartItems);
};

const handleRemoveFromCart = (product) => {
};
const handleRemoveFromCart = (product) => {
const existingItemFromCart = cartItems.find(
(item) => item.name === product.name
);
Expand Down
60 changes: 60 additions & 0 deletions hooks/favorites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { useState, useEffect } from "react";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not bad but this is pretty much a copy of everything I wrote which is why you have a bunch of code that doesn't quite apply to favorite list. can you identify those lines?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually i didn't understand how to implement those things so copied your cart item theme and tried to work on this . And i removed lots of cart item codes but as you said there are still available i'll check those codes and try to solve ..
Thanks

import db from "../data/database";

const getItemsFromDb = () => db.favoriteItems.toArray();
const addItemToDb = (item) => db.favoriteItems.put(item);
const updateItemInDb = (item, count) => {
db.favoriteItems.where("name").equalsIgnoreCase(item.name).modify({ count });
};
const removeItemFromDb = (item) => {
db.favoriteItems.where("name").equalsIgnoreCase(item.name).delete();
};
const clearItemsFromDb = () => db.favoriteItems.clear();

export const useFavoriteCart = () => {
const [favoriteItems, setfavoriteItems] = useState([]);

useEffect(() => {
getItemsFromDb().then((itemsFromDb) => setfavoriteItems(itemsFromDb));
}, []);

const handleAddFavoriteCart = (product) => {
const existingItemFromCart = favoriteItems.find(
(item) => item.name === product.name
);
let newfavoriteItems = [];

if (existingItemFromCart) {
removeItemFromDb(product)
}else {
newfavoriteItems = [...favoriteItems, { ...product, count: 1 }];
addItemToDb({ ...product, count: 1 });
}

setfavoriteItems(newfavoriteItems);
};
const handleRemoveFromFavorite = (product) => {
const existingItemFromCart = favoriteItems.find(
(item) => item.name === product.name
);
let newfavoriteItems = [];

if (existingItemFromCart) {
newfavoriteItems = favoriteItems.filter((item) => item.name !== product.name);
removeItemFromDb(product);
}
setfavoriteItems(newfavoriteItems);
};

const handleClearCart = () => {
setfavoriteItems([]);
clearItemsFromDb();
};

return {
favoriteItems,
handleClearCart,
handleAddFavoriteCart,
handleRemoveFromFavorite,
};
};
31 changes: 31 additions & 0 deletions pages/favorites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Link from 'next/link';
import React from 'react'
import { FavoriteProductComponent } from '../components/product/favorite';

import { useFavoriteCart } from '../hooks/favorites';

function Favorites() {

const {
favoriteItems,
handleClearCart,
handleRemoveFromFavorite,
} = useFavoriteCart();



return (
<div>
<FavoriteProductComponent
products={favoriteItems}
handleClearCart={handleClearCart}
handleRemoveFromCart={handleRemoveFromFavorite}
/>
<Link href="/">
<a className="px-3 py-2 my-3 bg-indigo-200 text-gray-800">go to list</a>
</Link>
</div>
)
}

export default Favorites;
13 changes: 13 additions & 0 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ListProductComponent } from "../components/product/list";
import { products } from "../data/products";
import { CartProductComponent } from "../components/product/cart";
import { useCart } from "../hooks/cart";
import Link from "next/link";
import { useFavoriteCart } from "../hooks/favorites";

export default function Home() {
const {
Expand All @@ -12,6 +14,10 @@ export default function Home() {
handleRemoveFromCart,
} = useCart();

const {
handleAddFavoriteCart
} = useFavoriteCart();

return (
<div>
<Head>
Expand All @@ -25,6 +31,7 @@ export default function Home() {
<ListProductComponent
products={products}
handleAddToCartClick={handleAddToCartClick}
handleAddFavoriteCart={handleAddFavoriteCart}
/>

{cartItems.length > 0 && (
Expand All @@ -37,6 +44,12 @@ export default function Home() {
</div>
)}
</div>
<Link href="/order">
<a className="px-3 py-2 bg-indigo-200 text-gray-800">Your order</a>
</Link>
<Link href="/favorites">
<a className="px-3 py-2 mx-3 bg-indigo-200 text-gray-800">go to Favorites</a>
</Link>
</div>
);
}
16 changes: 16 additions & 0 deletions pages/order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Fragment } from "react";
import { products } from "../data/products";
export const Order = () => {
return (
<div>
{products.map((product) => (
<Fragment key={product.id}>
<h1>{product.name}</h1>
<p>{product.price}</p>
<p>{product.description}</p>
</Fragment>
))}
</div>
);
};
export default Order;