Skip to content

Commit

Permalink
fixes: Seneca-CDOT#1229 Implemented an infinite scrolling on the Load…
Browse files Browse the repository at this point in the history
…MoreButton
  • Loading branch information
klee214 committed Nov 11, 2020
1 parent 70d28dd commit 69c36ca
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/frontend/src/components/Posts/LoadMoreButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,37 @@ const useStyles = makeStyles((theme) => ({

function LoadMoreButton({ onClick }) {
const classes = useStyles();
const $buttonRef = React.useRef(null);

// This will make the automatic infinite scrolling feature
// Once the "button" is on the viewpoint(shown on the window),
// The new posts are updated(call onClick() -- setSize(size + 1) in Posts.jsx --)
React.useEffect(() => {
let options = {
root: null,
rootMargin: '0px',
threshold: 1.0,
};

let observer = new IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
onClick();
}
});
}, options);
observer.observe($buttonRef.current);

return () => {
observer.unobserve($buttonRef.current);
};
}, []);

return (
<Container>
<Grid container spacing={0} direction="column" alignItems="center" justify="center">
<Grid item xs={12} className={classes.content}>
<Button color="primary" variant="outlined" onClick={() => onClick()}>
<Button ref={$buttonRef} color="primary" variant="outlined" onClick={() => onClick()}>
Load More Posts
</Button>
</Grid>
Expand Down

0 comments on commit 69c36ca

Please sign in to comment.