History (add/ remove) favorites add remove done

This commit is contained in:
Mann Patel
2025-04-13 12:52:21 -06:00
parent 3bdb8877a6
commit 2ef05ac3af
9 changed files with 220 additions and 92 deletions

View File

@@ -7,6 +7,7 @@ const Favorites = () => {
const [showFilters, setShowFilters] = useState(false);
const [sortBy, setSortBy] = useState("dateAdded");
const [filterCategory, setFilterCategory] = useState("All");
const storedUser = JSON.parse(sessionStorage.getItem("user"));
const mapCategory = (id) => {
const categories = {
@@ -20,10 +21,41 @@ const Favorites = () => {
return categories[id] || "Other";
};
function reloadPage() {
var doctTimestamp = new Date(performance.timing.domLoading).getTime();
var now = Date.now();
if (now > doctTimestamp) {
location.reload();
}
}
const removeFromFavorites = async (itemID) => {
const response = await fetch(
"http://localhost:3030/api/product/delFavorite",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
userID: storedUser.ID,
productID: itemID,
}),
},
);
const data = await response.json();
if (data.success) {
reloadPage();
}
if (!response.ok) throw new Error("Failed to fetch products");
console.log(response);
console.log(`Add Product -> History: ${itemID}`);
};
useEffect(() => {
const fetchFavorites = async () => {
try {
const user = JSON.parse(sessionStorage.getItem("user"));
const response = await fetch(
"http://localhost:3030/api/product/getFavorites",
{
@@ -31,13 +63,11 @@ const Favorites = () => {
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ userID: user.ID }),
body: JSON.stringify({ userID: storedUser.ID }),
},
);
const data = await response.json();
console.log(user.ID);
console.log(data);
const favoritesData = data.favorites;
@@ -75,11 +105,6 @@ const Favorites = () => {
return `${diffInDays}d ago`;
};
const removeFromFavorites = (id) => {
setFavorites(favorites.filter((item) => item.id !== id));
// Optional: Send DELETE request to backend here
};
const sortedFavorites = [...favorites].sort((a, b) => {
if (sortBy === "dateAdded")
return new Date(b.dateAdded) - new Date(a.dateAdded);