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

implement cookie-based caching for featured listings #561

Merged
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
34 changes: 13 additions & 21 deletions frontend/src/components/listing/FeaturedListings.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import React, { createContext, useContext, useEffect, useState } from "react";

import { useAuthentication } from "@/hooks/useAuth";

type FeaturedListing = {
id: string;
username: string;
slug: string | null;
name: string;
};
import {
FeaturedListing,
getFeaturedListingsFromStorage,
setFeaturedListingsStorage,
} from "@/lib/utils/featuredListingsStorage";

type FeaturedListingsContextType = {
featuredListings: FeaturedListing[];
Expand All @@ -23,7 +21,7 @@ export const FeaturedListingsProvider = ({
children: React.ReactNode;
}) => {
const [featuredListings, setFeaturedListings] = useState<FeaturedListing[]>(
[],
getFeaturedListingsFromStorage(),
);
const auth = useAuthentication();

Expand All @@ -34,6 +32,7 @@ export const FeaturedListingsProvider = ({

if (!featuredData?.listing_ids?.length) {
setFeaturedListings([]);
setFeaturedListingsStorage([]);
return;
}

Expand All @@ -58,26 +57,19 @@ export const FeaturedListingsProvider = ({
}));

setFeaturedListings(orderedListings);
setFeaturedListingsStorage(orderedListings);
}
} catch (error) {
console.error("Error refreshing featured listings:", error);
}
};

useEffect(() => {
refreshFeaturedListings();

const handleFeaturedChange = () => {
refreshFeaturedListings();
};

window.addEventListener("featuredListingsChanged", handleFeaturedChange);
return () => {
window.removeEventListener(
"featuredListingsChanged",
handleFeaturedChange,
);
};
Promise.resolve()
.then(refreshFeaturedListings)
.catch((error) => {
console.error("Background refresh failed:", error);
});
}, []);

return (
Expand Down
6 changes: 2 additions & 4 deletions frontend/src/components/listing/ListingFeatureButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions frontend/src/components/nav/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ const Sidebar = ({ show, onClose }: SidebarProps) => {
<ul className="space-y-1">
{featuredListings && featuredListings.length > 0 && (
<>
<div className="text-xl font-medium text-gray-1 px-3 py-2">
Featured Listings:
</div>
{featuredListings.map((listing) => (
<SidebarItem
key={listing.id}
Expand Down
26 changes: 26 additions & 0 deletions frontend/src/lib/utils/featuredListingsStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export interface FeaturedListing {
id: string;
username: string;
slug: string | null;
name: string;
}

export const FEATURED_LISTINGS_KEY = "featured_listings";

export const getFeaturedListingsFromStorage = (): FeaturedListing[] => {
try {
const stored = localStorage.getItem(FEATURED_LISTINGS_KEY);
return stored ? JSON.parse(stored) : [];
} catch (error) {
console.error("Error parsing featured listings from storage:", error);
return [];
}
};

export const setFeaturedListingsStorage = (listings: FeaturedListing[]) => {
try {
localStorage.setItem(FEATURED_LISTINGS_KEY, JSON.stringify(listings));
} catch (error) {
console.error("Error storing featured listings:", error);
}
};
Loading