diff --git a/src/components/ScrollThreshold.jsx b/src/components/ScrollThreshold.jsx new file mode 100644 index 00000000..be8272b5 --- /dev/null +++ b/src/components/ScrollThreshold.jsx @@ -0,0 +1,41 @@ +import React, { + useEffect, + useRef, +} from 'react'; +import PropTypes from 'prop-types'; + +function ScrollThreshold({ onScroll }) { + const elementRef = useRef(null); + + useEffect(() => { + if (!elementRef.current) { + return undefined; + } + + // create the observer + const observer = new IntersectionObserver( + ([entry]) => { + if (entry.isIntersecting) { + onScroll(); + } + }, + ); + + observer.observe(elementRef.current); + + // cleanup callback + return () => { + observer.disconnect(); + }; + }, [elementRef]); + + return ( +
+ ); +} + +ScrollThreshold.propTypes = { + onScroll: PropTypes.func.isRequired, +}; + +export default ScrollThreshold; diff --git a/src/discussions/posts/PostsView.jsx b/src/discussions/posts/PostsView.jsx index 54858e84..0948b21c 100644 --- a/src/discussions/posts/PostsView.jsx +++ b/src/discussions/posts/PostsView.jsx @@ -4,10 +4,10 @@ import PropTypes from 'prop-types'; import { useDispatch, useSelector } from 'react-redux'; import { useParams } from 'react-router'; -import { injectIntl, intlShape } from '@edx/frontend-platform/i18n'; import { AppContext } from '@edx/frontend-platform/react'; -import { Button, Spinner } from '@edx/paragon'; +import { Spinner } from '@edx/paragon'; +import ScrollThreshold from '../../components/ScrollThreshold'; import { RequestStatus } from '../../data/constants'; import { selectAllThreads, @@ -20,10 +20,9 @@ import { } from './data/selectors'; import { fetchThreads } from './data/thunks'; import PostFilterBar from './post-filter-bar/PostFilterBar'; -import messages from './messages'; import { PostLink } from './post'; -function PostsView({ showOwnPosts, intl }) { +function PostsView({ showOwnPosts }) { const { courseId, topicId, @@ -76,9 +75,7 @@ function PostsView({ showOwnPosts, intl }) { ) : ( nextPage && ( - +