Skip to content

Commit

Permalink
Updated to use localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
ivntsng committed Nov 8, 2024
1 parent 2fd70cd commit 6cde634
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 42 deletions.
26 changes: 11 additions & 15 deletions frontend/src/components/listing/FeaturedListings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import React, { createContext, useContext, useEffect, useState } from "react";
import { useAuthentication } from "@/hooks/useAuth";
import {
FeaturedListing,
getFeaturedListingsFromCookie,
setFeaturedListingsCookie,
} from "@/lib/utils/FeaturedListingsCookies";
getFeaturedListingsFromStorage,
setFeaturedListingsStorage,
} from "@/lib/utils/featuredListingsStorage";

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

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

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

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

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

useEffect(() => {
if (featuredListings.length === 0) {
refreshFeaturedListings();
} else {
Promise.resolve()
.then(refreshFeaturedListings)
.catch((error) => {
console.error("Background refresh failed:", error);
});
}
Promise.resolve()
.then(refreshFeaturedListings)
.catch((error) => {
console.error("Background refresh failed:", error);
});
}, []);

return (
Expand Down
27 changes: 0 additions & 27 deletions frontend/src/lib/utils/FeaturedListingsCookies.ts

This file was deleted.

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 6cde634

Please sign in to comment.