From e5e261f0c234cf63d0162afff061d1c12dfb94dc Mon Sep 17 00:00:00 2001 From: David Ratunuman Date: Thu, 11 Jul 2024 19:05:00 -0700 Subject: [PATCH] Added comments - maybe to much --- src/pages/dashboard/components/RouteList.tsx | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/pages/dashboard/components/RouteList.tsx b/src/pages/dashboard/components/RouteList.tsx index 52a9b270..f27b1a0b 100644 --- a/src/pages/dashboard/components/RouteList.tsx +++ b/src/pages/dashboard/components/RouteList.tsx @@ -13,30 +13,38 @@ interface RouteListProps { } const RouteList: Component = (props) => { + // Initialize state signals const [sortOption, setSortOption] = createSignal({ label: 'Date', key: 'date', order: 'asc' }) const [page, setPage] = createSignal(1) const [allRoutes, setAllRoutes] = createSignal([]) const [sortedRoutes, setSortedRoutes] = createSignal([]) const [hasMore, setHasMore] = createSignal(true) + // Create a resource for fetching routes + // ! This might refetch unnecessarily if any of the dependencies change const [routesData, { refetch }] = createResource( () => ({ dongleId: props.dongleId, page: page(), pageSize: PAGE_SIZE }), fetchRoutes, ) + // Effect to update allRoutes when new data is fetched createEffect(() => { const newRoutes = routesData() if (newRoutes) { setAllRoutes(prev => { + // Filter out duplicate routes const uniqueNewRoutes = newRoutes.filter(newRoute => !prev.some(existingRoute => existingRoute.start_time === newRoute.start_time), ) + // Update hasMore based on whether a full page was returned setHasMore(newRoutes.length === PAGE_SIZE) + // Append new unique routes to existing routes return [...prev, ...uniqueNewRoutes] }) } }) + // Effect to sort routes when allRoutes or sortOption changes createEffect(() => { const routes = allRoutes() const currentSortOption = sortOption() @@ -45,20 +53,24 @@ const RouteList: Component = (props) => { } }) + // Function to sort routes and update sortedRoutes signal const sortAndSetRoutes = async (routes: RouteSegments[], currentSortOption: SortOption) => { const sorted = await sortRoutes(routes, currentSortOption) setSortedRoutes(sorted) } + // Handler for sort option changes const handleSortChange = (key: SortKey, order: 'asc' | 'desc') => { const label = key.charAt(0).toUpperCase() + key.slice(1) setSortOption({ label, key, order }) + // Reset pagination and refetch routes setPage(1) setAllRoutes([]) setHasMore(true) void refetch() } + // Function to load more routes const loadMore = () => { console.log('loadMore called', { loading: routesData.loading, hasMore: hasMore() }) if (!routesData.loading && hasMore()) { @@ -67,6 +79,7 @@ const RouteList: Component = (props) => { } } + // Set up Intersection Observer for infinite scrolling let bottomRef: HTMLDivElement | undefined const observer = new IntersectionObserver( (entries) => { @@ -78,6 +91,7 @@ const RouteList: Component = (props) => { { rootMargin: '200px' }, ) + // Effect to observe/unobserve the bottom element createEffect(() => { if (bottomRef) { observer.observe(bottomRef) @@ -87,13 +101,17 @@ const RouteList: Component = (props) => { } }) + // Effect to refetch routes when page changes + // ! This might cause unnecessary refetches if other dependencies of routesData change createEffect(() => { page() void refetch() }) + // Cleanup function to disconnect the observer onCleanup(() => observer.disconnect()) + // Render the component return (
@@ -105,6 +123,7 @@ const RouteList: Component = (props) => { {!routesData.loading && !hasMore() && sortedRoutes().length === 0 &&
No routes found
} {!routesData.loading && !hasMore() && sortedRoutes().length > 0 &&
All routes loaded
}
+ {/* Invisible element for intersection observer */}
)