-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
123 additions
and
17 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 |
---|---|---|
@@ -1,13 +1,59 @@ | ||
import { dehydrate, HydrationBoundary } from '@tanstack/react-query'; | ||
|
||
import { getQueryClient } from '@/shared/utils/get-query-client'; | ||
|
||
import { grantQueryKeys } from '@/grants/core/query-keys'; | ||
import { getGranteeDetails } from '@/grants/data/get-grantee-details'; | ||
import { getGranteeProject } from '@/grants/data/get-grantee-project'; | ||
import { getGranteesList } from '@/grants/data/get-grantees-list'; | ||
import { GranteeList } from '@/grants/components/grantee-list'; | ||
|
||
interface Props { | ||
params: { grantId: string }; | ||
} | ||
|
||
const ParallelGranteeList = ({}: Props) => { | ||
// TODO: React-Query SSR grantee list | ||
const ParallelGranteeList = async ({ params: { grantId } }: Props) => { | ||
const queryClient = getQueryClient(); | ||
|
||
const [granteeListResult] = await Promise.all([ | ||
// Prefetch list | ||
queryClient.fetchInfiniteQuery({ | ||
queryKey: grantQueryKeys.grantees(grantId, ''), | ||
queryFn: async ({ pageParam: page }) => | ||
getGranteesList({ page, grantId }), | ||
initialPageParam: 1, | ||
}), | ||
]); | ||
|
||
// Prefetch data for first item only (default for the page enough for seo) | ||
const grantee = granteeListResult.pages.flatMap((page) => page.data).at(0); | ||
if (grantee) { | ||
// Prefetch grantee details | ||
const promises = [ | ||
queryClient.prefetchQuery({ | ||
queryKey: grantQueryKeys.grantee(grantee.id), | ||
queryFn: () => getGranteeDetails(grantee.id), | ||
}), | ||
]; | ||
|
||
// Prefetch first project details | ||
if (grantee.projects.length > 0) { | ||
promises.push( | ||
queryClient.prefetchQuery({ | ||
queryKey: grantQueryKeys.project(grantee.projects[0]), | ||
queryFn: () => getGranteeProject(grantee.projects[0]), | ||
}), | ||
); | ||
} | ||
|
||
await Promise.all(promises); | ||
} | ||
|
||
return <GranteeList />; | ||
return ( | ||
<HydrationBoundary state={dehydrate(queryClient)}> | ||
<GranteeList /> | ||
</HydrationBoundary> | ||
); | ||
}; | ||
|
||
export default ParallelGranteeList; |
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
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 |
---|---|---|
@@ -1,11 +1,43 @@ | ||
import { dehydrate, HydrationBoundary } from '@tanstack/react-query'; | ||
|
||
import { getQueryClient } from '@/shared/utils/get-query-client'; | ||
|
||
import { grantQueryKeys } from '@/grants/core/query-keys'; | ||
import { getGrantDetails } from '@/grants/data/get-grant-details'; | ||
import { getGrantList } from '@/grants/data/get-grant-list'; | ||
import { GrantList } from '@/grants/components/grant-list/grant-list'; | ||
|
||
export const GrantListPage = () => { | ||
// TODO: React-Query SSR grant list | ||
// TODO: Check if need to use search params(filter related), if so need to force dynamic | ||
|
||
export const GrantListPage = async () => { | ||
const queryClient = getQueryClient(); | ||
|
||
const [grantListResult] = await Promise.all([ | ||
// Prefetch list | ||
queryClient.fetchInfiniteQuery({ | ||
queryKey: grantQueryKeys.list(''), | ||
queryFn: async ({ pageParam }) => getGrantList(pageParam), | ||
initialPageParam: 1, | ||
}), | ||
]); | ||
|
||
// Prefetch details for each grant item | ||
await Promise.all( | ||
grantListResult.pages | ||
.flatMap((page) => page.data) | ||
.map(({ id }) => | ||
queryClient.prefetchQuery({ | ||
queryKey: grantQueryKeys.details(id), | ||
queryFn: () => getGrantDetails(id), | ||
}), | ||
), | ||
); | ||
|
||
return ( | ||
<div className="p-8"> | ||
<GrantList /> | ||
</div> | ||
<HydrationBoundary state={dehydrate(queryClient)}> | ||
<div className="p-8"> | ||
<GrantList /> | ||
</div> | ||
</HydrationBoundary> | ||
); | ||
}; |
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