Skip to content

Commit

Permalink
prefetching queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanish2002 committed Nov 28, 2023
1 parent fe9cc0d commit 59ab9ba
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/app/(app)/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
import { QueryFunction, QueryKey, useQueryClient } from "@tanstack/react-query";
import { Redirect, Stack } from "expo-router";
import { useEffect } from "react";

import { areasKeys, fetchAreas } from "@/api/areas";
import { chatKeys, fetchMessages } from "@/api/chat";
import { fetchNotifications, sosKeys } from "@/api/sos";
import { fetchTickets, ticketKeys } from "@/api/ticket";
import { useUser } from "@/hooks/useUser";

const AppLayout = () => {
const { user } = useUser();
const queryClient = useQueryClient();
const preFetchAllData = async () => {
// Can't be bothered to add types for this.
const fetchFunctionswithKeys: [QueryKey, QueryFunction<any, any>][] = [
[chatKeys.community_chat, fetchMessages],
[areasKeys.heatmap, fetchAreas],
[
ticketKeys.tickets(user?.user_id!),
async () => await fetchTickets(user!.user_id!)
],
[sosKeys.sos(user?.user_id!), fetchNotifications]
];

// Fetching all data from store and retring if anyone failed
// const fetchAllData = async () => {
// const fetchFunctions = [];
//
// for (const fetchFunction of fetchFunctions) {
// await fetchDataWithRetry(fetchFunction);
// }
// };

// useEffect(() => {
// if (!user) return;
//
// fetchAllData();
// }, [user]);
for (const [key, fetchFunction] of fetchFunctionswithKeys) {
queryClient.prefetchQuery({ queryKey: key, queryFn: fetchFunction });
}
};

useEffect(() => {
if (!user) return;

preFetchAllData();
}, [user]);

if (!user) return <Redirect href="/Auth" />;

Expand Down

0 comments on commit 59ab9ba

Please sign in to comment.