Skip to content

Commit

Permalink
fixes: Seneca-CDOT#1229 Implemented an infinite scrolling without Loa…
Browse files Browse the repository at this point in the history
…dMoreButton
  • Loading branch information
klee214 committed Nov 11, 2020
1 parent 69c36ca commit 80b3317
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
57 changes: 57 additions & 0 deletions src/frontend/src/components/Posts/LoadAutoScroll.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React from 'react';
import PropTypes from 'prop-types';

import { Container, Button, Grid } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles((theme) => ({
content: {
'& > *': {
padding: theme.spacing(2),
bottom: theme.spacing(4),
},
},
}));

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

// This will make the automatic infinite scrolling feature
// Once the "button" is on the viewport(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) {
onScroll();
}
});
}, options);
observer.observe($buttonRef.current);

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

return (
<Container>
<Grid item xs={12} className={classes.content}>
<Button ref={$buttonRef}>Load More Posts</Button>
</Grid>
</Container>
);
}

LoadMoreButton.propTypes = {
onClick: PropTypes.func,
};

export default LoadMoreButton;
5 changes: 3 additions & 2 deletions src/frontend/src/components/Posts/Timeline.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Container, Grid } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
import Post from '../Post/Post.jsx';
import Spinner from '../Spinner/Spinner.jsx';
import LoadMoreButton from './LoadMoreButton.jsx';

import LoadAutoScroll from './LoadAutoScroll';
import useSiteMetaData from '../../hooks/use-site-metadata';

const useStyles = makeStyles((theme) => ({
Expand Down Expand Up @@ -40,7 +41,7 @@ const Timeline = ({ pages, nextPage }) => {
// key each time, based on page (i.e., size), so we remove the previous one
if (nextPage) {
postsTimeline.push(
<LoadMoreButton onClick={() => nextPage()} key={`load-more-button-${pages.length}`} />
<LoadAutoScroll onScroll={() => nextPage()} key={`load-more-button-${pages.length}`} />
);
}

Expand Down

0 comments on commit 80b3317

Please sign in to comment.