-
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.
refactor: setup grantee list component
- Loading branch information
Showing
21 changed files
with
345 additions
and
108 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,16 +1,13 @@ | ||
import GranteeList from '@/grants/components/grantee-list'; | ||
|
||
import { fakeGrantee } from '@/grants/testutils/fake-grantee'; | ||
import { GranteeList } from '@/grants/components/grantee-list'; | ||
|
||
interface Props { | ||
params: { grantId: string }; | ||
} | ||
|
||
const ParallelGranteeList = ({}: Props) => { | ||
// TODO: fetch grantees using grantId | ||
const grantees = Array.from({ length: 12 }).map(() => fakeGrantee); | ||
// TODO: React-Query SSR grantee list | ||
|
||
return <GranteeList grantees={grantees} />; | ||
return <GranteeList />; | ||
}; | ||
|
||
export default ParallelGranteeList; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
src/grants/components/grantee-list/grantee-list.stories.tsx
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,67 @@ | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { MockInfiniteQueryResult } from '@/shared/testutils/misc'; | ||
|
||
import { mockGranteeListQuery } from '@/grants/testutils/mock-grantee-list-query'; | ||
|
||
import { GranteeList } from './grantee-list'; | ||
|
||
const meta: Meta<typeof GranteeList> = { | ||
title: 'grants/components/grantee-list', | ||
component: GranteeList, | ||
args: {}, | ||
parameters: { | ||
msw: { | ||
handlers: [mockGranteeListQuery(MockInfiniteQueryResult.SUCCESS)], | ||
}, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof GranteeList>; | ||
|
||
export const Success: Story = {}; | ||
|
||
export const Loading: Story = { | ||
parameters: { | ||
msw: { | ||
handlers: [ | ||
mockGranteeListQuery(MockInfiniteQueryResult.SUCCESS, { | ||
networkDelay: 'infinite', | ||
}), | ||
], | ||
}, | ||
}, | ||
}; | ||
|
||
export const Empty: Story = { | ||
parameters: { | ||
msw: { | ||
handlers: [mockGranteeListQuery(MockInfiniteQueryResult.EMPTY)], | ||
}, | ||
}, | ||
}; | ||
|
||
export const EndOfResults: Story = { | ||
parameters: { | ||
msw: { | ||
handlers: [mockGranteeListQuery(MockInfiniteQueryResult.END_OF_RESULTS)], | ||
}, | ||
}, | ||
}; | ||
|
||
export const NetworkError: Story = { | ||
parameters: { | ||
msw: { | ||
handlers: [mockGranteeListQuery(MockInfiniteQueryResult.NETWORK_ERROR)], | ||
}, | ||
}, | ||
}; | ||
|
||
export const FetchError: Story = { | ||
parameters: { | ||
msw: { | ||
handlers: [mockGranteeListQuery(MockInfiniteQueryResult.FETCH_ERROR)], | ||
}, | ||
}, | ||
}; |
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,47 @@ | ||
'use client'; | ||
|
||
import { useParams } from 'next/navigation'; | ||
import { useMemo } from 'react'; | ||
|
||
import { VirtualWrapper } from '@/shared/components/virtual-wrapper'; | ||
|
||
import { GranteeListItem } from './item'; | ||
import { useGranteeList } from './use-grantee-list'; | ||
|
||
export const GranteeList = () => { | ||
// TODO: JOB-681 | ||
|
||
const params = useParams(); | ||
|
||
const { grantees, error, inViewRef, hasNextPage, isPending } = useGranteeList( | ||
params.grantId as string, | ||
); | ||
|
||
const lastItem = useMemo(() => { | ||
if (error) return <p>Error: {error.message}</p>; | ||
|
||
if (!hasNextPage) return <p>No more grantees available.</p>; | ||
|
||
return <div ref={inViewRef}>Loading more...</div>; | ||
}, [error, hasNextPage, inViewRef]); | ||
|
||
if (isPending) return <p>Loading Grants ...</p>; | ||
|
||
if (!grantees.length) { | ||
return error ? <p>Error: {error.message}</p> : <p>No grantees found.</p>; | ||
} | ||
|
||
return ( | ||
<div className="flex flex-col gap-4"> | ||
<VirtualWrapper count={grantees.length}> | ||
{(index) => ( | ||
<div className="pt-8"> | ||
<GranteeListItem grantee={grantees[index]} /> | ||
</div> | ||
)} | ||
</VirtualWrapper> | ||
|
||
{lastItem} | ||
</div> | ||
); | ||
}; |
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 @@ | ||
export * from './grantee-list'; |
File renamed without changes.
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 @@ | ||
export * from './item'; |
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
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,24 @@ | ||
import { useInView } from 'react-intersection-observer'; | ||
|
||
import { useGranteeListQuery } from '@/grants/hooks/use-grantee-list-query'; | ||
|
||
export const useGranteeList = (grantId: string) => { | ||
const { data, error, fetchNextPage, hasNextPage, isPending, isFetching } = | ||
useGranteeListQuery(grantId); | ||
|
||
// Next page fetch on scroll | ||
const { ref: inViewRef } = useInView({ | ||
threshold: 1, | ||
onChange: (inView) => { | ||
if (inView && !error && !isFetching) fetchNextPage(); | ||
}, | ||
}); | ||
|
||
return { | ||
grantees: data?.pages.flatMap((d) => d.data) ?? [], | ||
error, | ||
inViewRef, | ||
hasNextPage, | ||
isPending, | ||
}; | ||
}; |
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,5 +1,12 @@ | ||
import { MW_URL } from '@/shared/core/envs'; | ||
|
||
export const GRANT_TEST_IDS = { | ||
GRANT_ITEM: 'grant-item', | ||
GRANT_CARD: 'grant-card', | ||
GRANTEE_ITEM: 'grantee-item', | ||
} as const; | ||
|
||
export const GRANT_QUERY_URLS = { | ||
GRANT_LIST: `${MW_URL}/grants/list`, | ||
GRANTEE_LIST: `${MW_URL}/grantees/list`, | ||
} as const; |
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
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { PAGE_SIZE } from '@/shared/core/envs'; | ||
import { createUrlWithSearchParams } from '@/shared/utils/create-url-with-search-params'; | ||
import { mwGET } from '@/shared/utils/mw-get'; | ||
|
||
import { GRANT_QUERY_URLS } from '@/grants/core/constants'; | ||
import { granteeListQueryPageSchema } from '@/grants/core/schemas'; | ||
|
||
interface Props { | ||
page: number; | ||
searchParams?: string; | ||
grantId: string; | ||
} | ||
|
||
export const getGranteesList = async ({ | ||
page, | ||
grantId, | ||
searchParams = '', | ||
}: Props) => { | ||
const url = createUrlWithSearchParams( | ||
`${GRANT_QUERY_URLS.GRANTEE_LIST}?page=${page}&limit=${PAGE_SIZE}&grantId=${grantId}`, | ||
searchParams, | ||
); | ||
|
||
return mwGET({ | ||
url, | ||
label: 'getGrantList', | ||
responseSchema: granteeListQueryPageSchema, | ||
options: { next: { revalidate: 60 * 60 } }, | ||
}); | ||
}; |
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,30 @@ | ||
import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query'; | ||
|
||
import { QUERY_STALETIME } from '@/shared/core/constants'; | ||
|
||
import { GrantQueryKeys, grantQueryKeys } from '@/grants/core/query-keys'; | ||
import { GranteeListQueryPage } from '@/grants/core/schemas'; | ||
import { getGranteesList } from '@/grants/data/get-grantees-list'; | ||
|
||
export const useGranteeListQuery = (grantId: string) => { | ||
// TODO: filter search params string | ||
const searchParams = ''; | ||
|
||
return useInfiniteQuery< | ||
GranteeListQueryPage, | ||
Error, | ||
InfiniteData<GranteeListQueryPage, number>, | ||
ReturnType<GrantQueryKeys['grantees']>, | ||
number | ||
>({ | ||
queryKey: grantQueryKeys.grantees(grantId, searchParams), | ||
queryFn: async ({ pageParam }) => | ||
getGranteesList({ page: pageParam, grantId, searchParams }), | ||
initialPageParam: 1, | ||
getNextPageParam: ({ page, data }) => | ||
typeof page === 'number' && page > 0 && data.length > 0 | ||
? page + 1 | ||
: undefined, | ||
staleTime: QUERY_STALETIME.DEFAULT, | ||
}); | ||
}; |
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
Oops, something went wrong.