From f1db052266f254dec65435a54702ca6e2a74a8cd Mon Sep 17 00:00:00 2001
From: Ariel Costas Guerrero <94913521+arielcostas@users.noreply.github.com>
Date: Fri, 30 Aug 2024 16:26:19 +0200
Subject: Fix client-side stop list cache

---
 src/pages/Home.tsx | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

(limited to 'src/pages/Home.tsx')

diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx
index 14d5d7a..f92847a 100644
--- a/src/pages/Home.tsx
+++ b/src/pages/Home.tsx
@@ -10,20 +10,19 @@ interface Stop {
 }
 
 interface CachedStopList {
-	timetsamp: number;
+	timestamp: number;
 	data: Stop[];
 }
 
 export function Home() {
 	const navigate = useNavigate()
 	const { data, error, isLoading } = useSWR<Stop[]>('home', async () => {
-		const cachedData = localStorage.getItem('cachedStopList')
-		if (cachedData) {
-			const parsedData: CachedStopList = JSON.parse(cachedData)
+		const rawCachedData = localStorage.getItem('cachedStopList');
+		if (rawCachedData) {
+			const parsedData: CachedStopList = JSON.parse(rawCachedData)
 
 			// Cache for 12 hours
-			if (Date.now() - parsedData.timetsamp < 1000 * 60 * 60 * 12) {
-				console.log("parsed data: ", parsedData.data)
+			if (Date.now() - parsedData.timestamp < 1000 * 60 * 60 * 12) {
 				return parsedData.data
 			} else {
 				localStorage.removeItem('cachedStopList')
@@ -33,10 +32,12 @@ export function Home() {
 		const response = await fetch('/api/ListStops')
 		const body = await response.json();
 
-		localStorage.setItem('cachedStopList', JSON.stringify({
+		const cachedData: CachedStopList = {
 			timestamp: Date.now(),
 			data: body
-		}));
+		}
+
+		localStorage.setItem('cachedStopList', JSON.stringify(cachedData));
 		
 		return body;
 	});
-- 
cgit v1.3

