diff --git a/frontend/src/components/listing/FeaturedListings.tsx b/frontend/src/components/listing/FeaturedListings.tsx index a91f6584..8066c871 100644 --- a/frontend/src/components/listing/FeaturedListings.tsx +++ b/frontend/src/components/listing/FeaturedListings.tsx @@ -1,6 +1,10 @@ import React, { createContext, useContext, useEffect, useState } from "react"; import { useAuthentication } from "@/hooks/useAuth"; +import { + getFeaturedListingsFromCookie, + setFeaturedListingsCookie, +} from "@/lib/utils/FeaturedListingsCookies"; type FeaturedListing = { id: string; @@ -23,7 +27,7 @@ export const FeaturedListingsProvider = ({ children: React.ReactNode; }) => { const [featuredListings, setFeaturedListings] = useState( - [], + getFeaturedListingsFromCookie(), ); const auth = useAuthentication(); @@ -34,6 +38,7 @@ export const FeaturedListingsProvider = ({ if (!featuredData?.listing_ids?.length) { setFeaturedListings([]); + setFeaturedListingsCookie([]); return; } @@ -58,6 +63,7 @@ export const FeaturedListingsProvider = ({ })); setFeaturedListings(orderedListings); + setFeaturedListingsCookie(orderedListings); } } catch (error) { console.error("Error refreshing featured listings:", error); @@ -65,19 +71,15 @@ export const FeaturedListingsProvider = ({ }; useEffect(() => { - refreshFeaturedListings(); - - const handleFeaturedChange = () => { + if (featuredListings.length === 0) { refreshFeaturedListings(); - }; - - window.addEventListener("featuredListingsChanged", handleFeaturedChange); - return () => { - window.removeEventListener( - "featuredListingsChanged", - handleFeaturedChange, - ); - }; + } else { + Promise.resolve() + .then(refreshFeaturedListings) + .catch((error) => { + console.error("Background refresh failed:", error); + }); + } }, []); return ( diff --git a/frontend/src/components/listing/ListingFeatureButton.tsx b/frontend/src/components/listing/ListingFeatureButton.tsx index 5c65c0c5..2b6502a0 100644 --- a/frontend/src/components/listing/ListingFeatureButton.tsx +++ b/frontend/src/components/listing/ListingFeatureButton.tsx @@ -54,13 +54,11 @@ const ListingFeatureButton = (props: Props) => { if (response.error) { addErrorAlert(response.error); } else { - const newFeaturedState = !isFeatured; - setIsFeatured(newFeaturedState); + setIsFeatured(!isFeatured); addAlert( - `Listing ${newFeaturedState ? "featured" : "unfeatured"} successfully`, + `Listing ${!isFeatured ? "featured" : "unfeatured"} successfully`, "success", ); - refreshFeaturedListings(); } } catch { diff --git a/frontend/src/components/nav/Sidebar.tsx b/frontend/src/components/nav/Sidebar.tsx index 3f70c195..65538b3e 100644 --- a/frontend/src/components/nav/Sidebar.tsx +++ b/frontend/src/components/nav/Sidebar.tsx @@ -110,9 +110,6 @@ const Sidebar = ({ show, onClose }: SidebarProps) => {