Skip to content

Commit

Permalink
implement cookie-based caching for featured listings (#561)
Browse files Browse the repository at this point in the history
* implement cookie-based caching for featured listings

* Fix type errors

* Updated to use localStorage
  • Loading branch information
ivntsng authored Nov 8, 2024
1 parent 9c523ce commit 4b1ff68
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
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);
}
};

0 comments on commit 4b1ff68

Please sign in to comment.