From 105faa5b52b3240456125bde487b59d655f69589 Mon Sep 17 00:00:00 2001 From: aymanhki Date: Sat, 30 Mar 2024 03:28:29 -0500 Subject: [PATCH] Bug fix for feed scrolling --- epoch_frontend/src/modules/Feed.js | 18 +++++++--- .../src/modules/NotificationsPopup.js | 3 +- epoch_frontend/src/modules/Post.js | 33 +++++++++---------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/epoch_frontend/src/modules/Feed.js b/epoch_frontend/src/modules/Feed.js index af51ce1..e739699 100644 --- a/epoch_frontend/src/modules/Feed.js +++ b/epoch_frontend/src/modules/Feed.js @@ -42,7 +42,7 @@ export default function Feed({ const [offset, setOffset] = useState(0); const limit = 20; // must be bigger than toRemove const maxPosts = 25; - const toRemove = 15; // Must be smaller than maxPosts + const toRemove = 15; // Must be smaller than limit const [previousPosts, setPreviousPosts] = useState([]); const bottomElementRef = useRef(null); const [ref, inView] = useInView({threshold: 0.1, }); @@ -104,11 +104,21 @@ export default function Feed({ { let finalToSetFeedPosts = finalFeedPosts; - setPreviousPosts(previousPosts.concat(finalFeedPosts.slice(0, toRemove))); - finalToSetFeedPosts = finalToSetFeedPosts.slice(toRemove); - finalToSetFeedPosts = finalToSetFeedPosts.concat(data); + if (finalFeedPosts.length - toRemove > maxPosts) + { + setPreviousPosts(previousPosts.concat(finalFeedPosts.slice(0, toRemove + (maxPosts - toRemove)))); + finalToSetFeedPosts = finalToSetFeedPosts.slice(0, toRemove + (maxPosts - toRemove)); + } + else + { + setPreviousPosts(previousPosts.concat(finalFeedPosts.slice(0, toRemove))); + finalToSetFeedPosts = finalToSetFeedPosts.slice(toRemove); + } + + finalToSetFeedPosts = finalToSetFeedPosts.concat(data); + if(finalOffset > 0) { setAllowScrollToTop(true); setNoMoreTopPosts(false); diff --git a/epoch_frontend/src/modules/NotificationsPopup.js b/epoch_frontend/src/modules/NotificationsPopup.js index 0e18f3c..9165641 100644 --- a/epoch_frontend/src/modules/NotificationsPopup.js +++ b/epoch_frontend/src/modules/NotificationsPopup.js @@ -19,6 +19,7 @@ function NotificationsPopup ({showNotifications, setShowNotifications, newUnread const [countUnreadNotifications, setCountUnreadNotifications] = useState(0); const [loadMorePrompt, setLoadMorePrompt] = useState('Load more'); const [markAllAsReadPrompt, setMarkAllAsReadPrompt] = useState('Mark all as read'); + const getNotificationsEvery = 10000; // 10 seconds const {transform: inTransform, opacity: inOpacity} = useSpring({ opacity: showNotifications ? 1 : 0, @@ -103,7 +104,7 @@ function NotificationsPopup ({showNotifications, setShowNotifications, newUnread if (!showNotifications && !isLoading ) { const interval = setInterval(() => { getNotifications(); - }, 90000000); + }, getNotificationsEvery); return () => clearInterval(interval); } diff --git a/epoch_frontend/src/modules/Post.js b/epoch_frontend/src/modules/Post.js index c87e3d6..020ee49 100644 --- a/epoch_frontend/src/modules/Post.js +++ b/epoch_frontend/src/modules/Post.js @@ -6,7 +6,6 @@ import FavoriteBorderOutlinedIcon from '@mui/icons-material/FavoriteBorderOutlin import '../styles/Post.css'; import {useNavigate} from 'react-router-dom'; import {useLocation} from 'react-router-dom'; -import PostPopup from "./PostPopup"; import ArrowCircleUpSharpIcon from '@mui/icons-material/ArrowCircleUpSharp'; import ArrowCircleDownSharpIcon from '@mui/icons-material/ArrowCircleDownSharp'; import {favoritePost, removeFavoritePost, votePost, removeVotePost} from "../services/post"; @@ -22,10 +21,9 @@ export default function Post({post, postViewer, isInFavorites, setShowDeletePost const navigate = useNavigate(); const [showOverlay, setShowOverlay] = useState(false); const [overlayImageUrl, setOverlayImageUrl] = useState(''); - const [postAdmin, setPostAdmin] = useState(postViewer && postViewer.username === post.username); + const postAdmin= postViewer && postViewer.username === post.username; const [error, setError] = useState(false); const [errorMessage, setErrorMessage] = useState(''); - const [deleted, setDeleted] = useState(false); const [updating, setUpdating] = useState(false); const [updatingMessage, setUpdatingMessage] = useState(''); const [favorited, setFavorited] = useState(false); @@ -197,29 +195,28 @@ export default function Post({post, postViewer, isInFavorites, setShowDeletePost let visible = false; if (postIsInThePast() || postAdmin) { - if (!deleted) { - if ( (isInFavorites && favorited) || !isInFavorites) { - if (usernames.length > 0) { - for (let i = 0; i < usernames.length && !visible; i++) { - if (postViewer && postViewer.username === usernames[i]) { - visible = true; - } - } - - if (!visible) - { - visible = postAdmin; + if ( (isInFavorites && favorited) || !isInFavorites) { + if (usernames.length > 0) { + for (let i = 0; i < usernames.length && !visible; i++) { + if (postViewer && postViewer.username === usernames[i]) { + visible = true; } } - else + + if (!visible) { - visible = true; + visible = postAdmin; } } + else + { + visible = true; + } } } + return visible; } @@ -549,7 +546,7 @@ export default function Post({post, postViewer, isInFavorites, setShowDeletePost setLocalFinalFavoritedByUsernameList(updatedFavoritedByUsernameList); } } else if (!favorited && postViewer) { - let updatedFavoritedByUsernameList = favoritedByUsernameList.filter((user) => user.user_id !== postViewer.id); + let updatedFavoritedByUsernameList = localFinalFavoritedByUsernameList.filter((user) => user.user_id !== postViewer.id); if (JSON.stringify(updatedFavoritedByUsernameList) !== JSON.stringify(localFinalFavoritedByUsernameList)) { setLocalFinalFavoritedByUsernameList(updatedFavoritedByUsernameList);