Skip to content
Snippets Groups Projects
Unverified Commit bc6a0f8e authored by Joel Jeremy Marquez's avatar Joel Jeremy Marquez Committed by GitHub
Browse files

Fix mobile transactions load more (#2504)

* Fix mobile transaction load more

* Release notes
parent 5ee7d336
No related branches found
No related tags found
No related merge requests found
...@@ -5,13 +5,14 @@ import React, { ...@@ -5,13 +5,14 @@ import React, {
useState, useState,
useContext, useContext,
useEffect, useEffect,
useCallback,
} from 'react'; } from 'react';
import debounce from 'debounce'; import debounce from 'debounce';
type IScrollContext = { type IScrollContext = {
scrollY: number | undefined; scrollY: number | undefined;
isBottomReached: boolean; hasScrolledToBottom: (tolerance?: number) => boolean;
}; };
const ScrollContext = createContext<IScrollContext | undefined>(undefined); const ScrollContext = createContext<IScrollContext | undefined>(undefined);
...@@ -22,14 +23,20 @@ type ScrollProviderProps = { ...@@ -22,14 +23,20 @@ type ScrollProviderProps = {
export function ScrollProvider({ children }: ScrollProviderProps) { export function ScrollProvider({ children }: ScrollProviderProps) {
const [scrollY, setScrollY] = useState(undefined); const [scrollY, setScrollY] = useState(undefined);
const [isBottomReached, setIsBottomReached] = useState(false); const [scrollHeight, setScrollHeight] = useState(undefined);
const [clientHeight, setClientHeight] = useState(undefined);
const hasScrolledToBottom = useCallback(
(tolerance = 1) => scrollHeight - scrollY <= clientHeight + tolerance,
[clientHeight, scrollHeight, scrollY],
);
useEffect(() => { useEffect(() => {
const listenToScroll = debounce(e => { const listenToScroll = debounce(e => {
setScrollY(e.target?.scrollTop || 0); const target = e.target;
setIsBottomReached( setScrollY(target?.scrollTop || 0);
e.target?.scrollHeight - e.target?.scrollTop <= e.target?.clientHeight, setScrollHeight(target?.scrollHeight || 0);
); setClientHeight(target?.clientHeight || 0);
}, 10); }, 10);
window.addEventListener('scroll', listenToScroll, { window.addEventListener('scroll', listenToScroll, {
...@@ -43,7 +50,7 @@ export function ScrollProvider({ children }: ScrollProviderProps) { ...@@ -43,7 +50,7 @@ export function ScrollProvider({ children }: ScrollProviderProps) {
}, []); }, []);
return ( return (
<ScrollContext.Provider value={{ scrollY, isBottomReached }}> <ScrollContext.Provider value={{ scrollY, hasScrolledToBottom }}>
{children} {children}
</ScrollContext.Provider> </ScrollContext.Provider>
); );
......
import React, { useEffect, useRef } from 'react'; import React, { useRef } from 'react';
import { useListBox } from '@react-aria/listbox'; import { useListBox } from '@react-aria/listbox';
import { useListState } from '@react-stately/list'; import { useListState } from '@react-stately/list';
import { usePrevious } from '../../../hooks/usePrevious';
import { useScroll } from '../../ScrollProvider';
import { ListBoxSection } from './ListBoxSection'; import { ListBoxSection } from './ListBoxSection';
export function ListBox(props) { export function ListBox(props) {
...@@ -11,25 +14,13 @@ export function ListBox(props) { ...@@ -11,25 +14,13 @@ export function ListBox(props) {
const { listBoxProps, labelProps } = useListBox(props, state, listBoxRef); const { listBoxProps, labelProps } = useListBox(props, state, listBoxRef);
const { loadMore } = props; const { loadMore } = props;
useEffect(() => { const { hasScrolledToBottom } = useScroll();
function loadMoreTransactions() { const scrolledToBottom = hasScrolledToBottom();
if ( const prevScrolledToBottom = usePrevious(scrolledToBottom);
Math.abs(
listBoxRef.current.scrollHeight -
listBoxRef.current.clientHeight -
listBoxRef.current.scrollTop,
) < listBoxRef.current.clientHeight // load more when we're one screen height from the end
) {
loadMore?.();
}
}
const currentListBoxRef = listBoxRef.current;
currentListBoxRef?.addEventListener('scroll', loadMoreTransactions);
return () => { if (!prevScrolledToBottom && scrolledToBottom) {
currentListBoxRef?.removeEventListener('scroll', loadMoreTransactions); loadMore?.();
}; }
}, [loadMore, state.collection]);
return ( return (
<> <>
......
---
category: Bugfix
authors: [joel-jeremy]
---
Fix mobile account transactions page not loading more transactions when reaching end of page.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment