-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix product variant crash when many warehouses by load on scroll (#4932)
* Fetch warehouses async * Update create and update page * Improve loader and button * useInfityScroll hook * Center loader * Improve edge cases in infinity scroll * Improve useWarehouseSearch variables typing * Keep warehouses fetch in view component * Fix infinity loading * Add changesset * Fix typos * Remove not usedd hook useAllWarehouses * Use event listener insted of expose onScroll funtion * use scroll instead * Back to scroll again Yeah! * Fix loading more on init
- Loading branch information
Showing
22 changed files
with
350 additions
and
202 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,5 @@ | ||
--- | ||
"saleor-dashboard": patch | ||
--- | ||
|
||
You can now assign warehouses in product variant page without page crash |
2 changes: 1 addition & 1 deletion
2
src/channels/components/AssignmentList/AssignmentListFooter.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
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,77 @@ | ||
import { useCallback, useEffect, useRef } from "react"; | ||
|
||
import useDebounce from "./useDebounce"; | ||
|
||
const SCROLL_THRESHOLD = 100; | ||
const DEBOUNCE_TIME = 500; | ||
|
||
export const useInfinityScroll = <TElementRef extends HTMLElement>({ | ||
onLoadMore, | ||
threshold = SCROLL_THRESHOLD, | ||
debounceTime = DEBOUNCE_TIME, | ||
}: { | ||
onLoadMore: () => void; | ||
threshold?: number; | ||
debounceTime?: number; | ||
}) => { | ||
const scrollRef = useRef<TElementRef>(null); | ||
|
||
const shouldLoadMore = useCallback(() => { | ||
if (!scrollRef.current) { | ||
return false; | ||
} | ||
|
||
const totalScrollHeight = scrollRef.current.scrollHeight; | ||
const scrollTop = scrollRef.current.scrollTop; | ||
const clientHeight = scrollRef.current.clientHeight; | ||
|
||
if (scrollTop === 0 && totalScrollHeight === 0 && clientHeight === 0) { | ||
return false; | ||
} | ||
|
||
const scrolledHeight = scrollTop + clientHeight; | ||
const scrollBottom = totalScrollHeight - scrolledHeight; | ||
|
||
return scrollBottom < threshold; | ||
}, [threshold]); | ||
|
||
const handleInfiniteScroll = () => { | ||
if (!scrollRef.current) { | ||
return; | ||
} | ||
|
||
if (shouldLoadMore()) { | ||
onLoadMore(); | ||
} | ||
}; | ||
|
||
const debouncedHandleInfiniteScroll = useDebounce( | ||
handleInfiniteScroll, | ||
debounceTime, | ||
); | ||
|
||
useEffect(() => { | ||
if (!scrollRef.current) { | ||
return; | ||
} | ||
|
||
const callback = () => debouncedHandleInfiniteScroll(); | ||
|
||
scrollRef.current.addEventListener("scroll", callback); | ||
|
||
return () => { | ||
scrollRef.current?.removeEventListener("scroll", callback); | ||
}; | ||
}, [debouncedHandleInfiniteScroll]); | ||
|
||
useEffect(() => { | ||
// On init check thresholdd and load more if needed | ||
if (shouldLoadMore()) { | ||
onLoadMore(); | ||
} | ||
}, [onLoadMore, shouldLoadMore]); | ||
|
||
return { | ||
scrollRef, | ||
}; | ||
}; |
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
File renamed without changes.
Oops, something went wrong.