-
Notifications
You must be signed in to change notification settings - Fork 477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resolve infinite scroll issue in Resource page #9267
Open
NikhilA8606
wants to merge
9
commits into
ohcnetwork:develop
Choose a base branch
from
NikhilA8606:issues/3149/resource-scroll-infinity
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+184
−109
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
83e9154
Resolve infinite scroll issue in Resource page
NikhilA8606 97119a0
useDebounce hook implemented
NikhilA8606 4b3a557
removed react-beautiful-dnd
NikhilA8606 c8452e5
removed node
NikhilA8606 669bdc9
Removed KanbanBoardType<T>
NikhilA8606 d3b8102
useInfiniteQuery hook implemented
NikhilA8606 f9249dc
Initialize totalCount with Infinity
NikhilA8606 a780d43
Merge pull request #1 from ohcnetwork/develop
NikhilA8606 b9c0bc3
useDebounce removed
NikhilA8606 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
useInfiniteQuery hook implemented
commit d3b81025c9f01259fab545e1e1903092ebff8f08
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,61 @@ | ||
import { useCallback, useState } from "react"; | ||
|
||
import { RESULTS_PER_PAGE_LIMIT } from "@/common/constants"; | ||
|
||
import { PaginatedResponse, QueryRoute } from "@/Utils/request/types"; | ||
import useQuery, { QueryOptions } from "@/Utils/request/useQuery"; | ||
|
||
export interface InfiniteQueryOptions<TItem> | ||
extends QueryOptions<PaginatedResponse<TItem>> { | ||
deduplicateBy: (item: TItem) => string | number; | ||
} | ||
|
||
export function useInfiniteQuery<TItem>( | ||
route: QueryRoute<PaginatedResponse<TItem>>, | ||
options?: InfiniteQueryOptions<TItem>, | ||
) { | ||
const [items, setItems] = useState<TItem[]>([]); | ||
const [totalCount, setTotalCount] = useState<number>(); | ||
const [offset, setOffset] = useState(0); | ||
|
||
const { refetch, loading, ...queryResponse } = useQuery(route, { | ||
...options, | ||
query: { | ||
...(options?.query ?? {}), | ||
offset, | ||
}, | ||
onResponse: ({ data }) => { | ||
if (!data) return; | ||
const allItems = items.concat(data.results); | ||
|
||
const deduplicatedItems = options?.deduplicateBy | ||
? Array.from( | ||
allItems | ||
.reduce((map, item) => { | ||
const key = options.deduplicateBy(item); | ||
return map.set(key, item); | ||
}, new Map<string | number, TItem>()) | ||
.values(), | ||
) | ||
: allItems; | ||
|
||
setItems(deduplicatedItems); | ||
setTotalCount(data.count); | ||
}, | ||
}); | ||
|
||
const fetchNextPage = useCallback(() => { | ||
if (loading) return; | ||
setOffset((prevOffset) => prevOffset + RESULTS_PER_PAGE_LIMIT); | ||
}, [loading]); | ||
NikhilA8606 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return { | ||
items, | ||
loading, | ||
fetchNextPage, | ||
refetch, | ||
totalCount, | ||
hasMore: items.length < (totalCount ?? 0), | ||
NikhilA8606 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
...queryResponse, | ||
}; | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential Overwrite of
onResponse
andquery
OptionsBy spreading
...options
and then overridingquery
andonResponse
, anyquery
oronResponse
properties provided in theoptions
object will be overwritten. This could lead to unintended behavior if the caller supplies their ownonResponse
orquery
parameters.Consider merging the
query
parameters and chaining theonResponse
callbacks to preserve the functionality provided by the caller:This ensures that any
onResponse
logic provided inoptions
is still executed.