-
Notifications
You must be signed in to change notification settings - Fork 69
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 #121 from TanayMaurya84/wishlist
Added feature of Wishlist(Saving items for future reference)! #23
- Loading branch information
Showing
8 changed files
with
243 additions
and
2 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
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,109 @@ | ||
|
||
import React from "react" | ||
import {useContext} from "react" | ||
import {SavedContext} from "../Context/SavedContext" | ||
import { ShopContext } from "../Context/ShopContext"; | ||
|
||
|
||
const SavedItems=()=>{ | ||
const {all_products,listItem,AddToList,RemoveFromList,getListQuantity}=useContext(SavedContext) | ||
const {AddToCart}=useContext(ShopContext) | ||
|
||
|
||
return( | ||
<> | ||
|
||
<div className="flex my-50"><p className="font-normal ">Total products Wishlisted: </p><p className="font-bold">{getListQuantity()}</p></div> | ||
|
||
<div className="grid grid-cols-3 "> | ||
{ | ||
all_products.map((item) => { | ||
if (listItem[item.id] > 0) { | ||
return( | ||
<div className="h-220 w-94 bg-white space-x-7" key={item.id}> | ||
<img src={item.image} alt={item.name} className="h-150 w-92"/> | ||
<div className="h-150 w-92"> | ||
<p className="font-semibold font-serif"> | ||
{(item.name.length > 40) ? ( | ||
// Truncate the name to the maximum length | ||
item.name.substring(0, 40) + "..." | ||
): (item.name) | ||
} | ||
</p> | ||
<p className="font-mono font-bold">Category: {item.category}</p> | ||
<div className="grid grid-cols-2"> | ||
<div><p className="font-bold text-green-900">${item.new_price}</p></div> | ||
<div><p className="line-through text-red-600">${item.old_price}</p></div> | ||
|
||
</div> | ||
<div className="flex space-x-20"> | ||
|
||
<div><button onClick={()=>{ | ||
RemoveFromList(item.id) | ||
}} className="bg-blue-950 text-white p-4 font-bold">Remove</button></div> | ||
<div><button className="bg-red-700 text-white p-4 font-extrabold" onClick={()=>{ | ||
AddToCart(item.id) | ||
}}>Add to Cart</button></div> | ||
|
||
</div> | ||
</div> | ||
|
||
|
||
</div> | ||
) | ||
} | ||
}) | ||
} | ||
|
||
|
||
|
||
</div> | ||
|
||
</> | ||
) | ||
} | ||
|
||
export default SavedItems; | ||
|
||
/*function CardComponent(item){ | ||
return( | ||
<div className="h-220 w-94 bg-white"> | ||
<img src={item.image} alt={item.name} className="h-150 w-92"/> | ||
<p className="font-semibold">{item.name}</p> | ||
<p className="font-normal">#Category:{item.category}</p> | ||
<div className="flex justify-between"> | ||
<p className="font-bold text-green-900">${item.new_price}</p> | ||
<p className="line-through text-red-600">${item.old_price}</p> | ||
</div> | ||
</div> | ||
) | ||
}*/ | ||
|
||
|
||
|
||
/*<div className="flex justify-between bg-pink-300"> | ||
{ | ||
all_products.map((item) => { | ||
if (listItem[item.id] > 0) { | ||
return( | ||
<CardComponent item={item}/> | ||
) | ||
} | ||
}) | ||
} | ||
</div>*/ | ||
|
||
|
||
/*<div className="flex justify-between"> | ||
<button onClick={()=>{ | ||
RemoveFromList(item.id) | ||
}} className="bg-blue-950 text-white">Remove</button> | ||
<button className="bg-red-700 text-white">Add to Cart</button> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import react, { createContext, useState } from "react"; | ||
import all_products from "../assets/all_product"; | ||
export const SavedContext = createContext(null); //ContextAPI for providing state variables to components of Wishlist | ||
|
||
const defaultWishlist = () => { | ||
let list = {}; | ||
for (let index = 0; index < all_products.length; index++) { | ||
list[index] = false; | ||
} | ||
return list; | ||
}; | ||
|
||
const SavedContextProvider = (props) => { | ||
const [listItem, setListItem] = useState(defaultWishlist()); | ||
const AddToList = (id) => { | ||
setListItem((prev) => { | ||
|
||
const updatedState = { ...prev, [id]: true }; | ||
console.log(updatedState); | ||
return updatedState; | ||
|
||
|
||
}); | ||
}; | ||
|
||
const RemoveFromList = (id) => { | ||
if (listItem[id]) { | ||
setListItem((prev) => { | ||
|
||
const updatedState = { ...prev, [id]: false }; | ||
|
||
console.log(updatedState); | ||
return updatedState; | ||
}); | ||
} | ||
}; | ||
|
||
/*const getCartTotalAmount = () => { | ||
let totalAmount = 0; | ||
for (const item in cartItem) { | ||
if (cartItem[item] > 0) { | ||
let itemInfo = all_products.find( | ||
(product) => product.id === Number(item) | ||
); | ||
totalAmount += cartItem[item] * itemInfo.new_price; | ||
} | ||
} | ||
return totalAmount; | ||
};*/ | ||
|
||
const getListQuantity = () => { | ||
let totalQuantity = 0; | ||
for (const item in listItem) { | ||
if (listItem[item]) { | ||
totalQuantity ++; | ||
} | ||
} | ||
return totalQuantity; | ||
}; | ||
|
||
const contextValue = { | ||
all_products, | ||
listItem, | ||
AddToList, | ||
RemoveFromList, | ||
getListQuantity, | ||
}; | ||
return ( | ||
<SavedContext.Provider value={contextValue}> | ||
{props.children} | ||
</SavedContext.Provider> | ||
); | ||
}; | ||
|
||
export default SavedContextProvider; |
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,13 @@ | ||
import React from "react" | ||
import SavedItems from "../Components/SavedItems" | ||
|
||
|
||
const Saved=()=>{ | ||
return( | ||
<div> | ||
<SavedItems /> | ||
</div> | ||
) | ||
} | ||
export default Saved; | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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