Skip to content

Commit

Permalink
feat: add prefetch component
Browse files Browse the repository at this point in the history
Signed-off-by: Prince Muel <[email protected]>
  • Loading branch information
princemuel committed May 12, 2023
1 parent 20ad893 commit 3aaa96a
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/lib/middleware/prefetch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useQueryClient } from '@tanstack/react-query';
import { useCallback, useEffect } from 'react';
import { Outlet } from 'react-router-dom';
import { client } from '../client';
import { useAuthDispatch } from '../context';
import { useGetInvoicesQuery, useGetUserQuery } from '../hooks';

const Prefetch = () => {
const queryClient = useQueryClient();

const dispatch = useAuthDispatch();

const prefetchUser = useCallback(async () => {
await queryClient.prefetchQuery({
queryKey: useGetUserQuery.getKey(),
queryFn: useGetUserQuery.fetcher(client),
});
}, [queryClient]);
const prefetchInvoices = useCallback(async () => {
await queryClient.prefetchQuery({
queryKey: useGetInvoicesQuery.getKey(),
queryFn: useGetInvoicesQuery.fetcher(client),
});
}, [queryClient]);

useEffect(() => {
async function init() {
await prefetchUser();

await prefetchInvoices();

dispatch('auth/addToken');
dispatch('auth/addUser');
}

init();
}, [prefetchInvoices, prefetchUser, dispatch]);

return <Outlet />;
};
export default Prefetch;

0 comments on commit 3aaa96a

Please sign in to comment.