Skip to content

Commit

Permalink
implement cookie-based caching for featured listings
Browse files Browse the repository at this point in the history
  • Loading branch information
ivntsng committed Nov 8, 2024
1 parent 68aa7e0 commit 45da2dc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
28 changes: 15 additions & 13 deletions frontend/src/components/listing/FeaturedListings.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,7 +27,7 @@ export const FeaturedListingsProvider = ({
children: React.ReactNode;
}) => {
const [featuredListings, setFeaturedListings] = useState<FeaturedListing[]>(
[],
getFeaturedListingsFromCookie(),
);
const auth = useAuthentication();

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

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

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

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

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 (
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
29 changes: 29 additions & 0 deletions frontend/src/lib/utils/FeaturedListingsCookies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export interface FeaturedListing {
id: string;
name: string;
slug: string;
description: string;
stripe_link?: string;
price?: number;
}

export const FEATURED_LISTINGS_COOKIES = "featured_listings";

export const getFeaturedListingsFromCookie = (): FeaturedListing[] => {
try {
const cookie = document.cookie
.split("; ")
.find((row) => row.startsWith(`${FEATURED_LISTINGS_COOKIES}=`));
if (cookie) {
return JSON.parse(decodeURIComponent(cookie.split("=")[1]));
}
} catch (error) {
console.error("Error parsing featured listings cookie:", error);
}
return [];
};

export const setFeaturedListingsCookie = (listings: FeaturedListing[]) => {
const value = encodeURIComponent(JSON.stringify(listings));
document.cookie = `${FEATURED_LISTINGS_COOKIES}=${value}; path=/; max-age=86400`;
};

0 comments on commit 45da2dc

Please sign in to comment.