-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Prince Muel <[email protected]>
- Loading branch information
1 parent
20ad893
commit 3aaa96a
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |