Skip to content

Commit

Permalink
lazyLoading for profile page.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrswastik-robot committed Mar 27, 2024
1 parent f1c6763 commit a47cbef
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
42 changes: 38 additions & 4 deletions app/profilePage/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import ProfileCard from "@/components/profilePage/ProfileCard";
import Image from "next/image";
import React, { useEffect, useState } from "react";
import React, { useEffect, useState , useRef } from "react";
import { useRouter } from "next/navigation";


Expand Down Expand Up @@ -64,6 +64,9 @@ const ProfilePage = (props: Props) => {
const router = useRouter();
const [user, loading] = useAuthState(auth);

//for automating loadmore lazy load button ...
const loadMoreButtonRef = useRef<HTMLDivElement>(null);

//followF
const [followers, setFollowers] = useState<any>([]);
const [following, setFollowing] = useState<any>([]);
Expand Down Expand Up @@ -410,6 +413,31 @@ useEffect(() => {
setLoadMore((prev)=>!prev)
};


//useEffect for automting lazyload functionality
useEffect(() => {
if(morePosts){
const observer = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting) {
handleLoadMore();
}
},
{ threshold: 1 } // 1.0 means that when 100% of the target is visible within the element specified by the root option, the callback is invoked.
);

if (loadMoreButtonRef.current) {
observer.observe(loadMoreButtonRef.current);
}

return () => {
if (loadMoreButtonRef.current) {
observer.unobserve(loadMoreButtonRef.current);
}
};
}
}, [loadMoreButtonRef, handleLoadMore ]);

const handleSortChange = ()=>{
setStart(true);
setAnonymousStart(true);
Expand Down Expand Up @@ -660,20 +688,26 @@ useEffect(() => {
<div className="mt-2 mb-5">
{isAnonymous ? (
anonymousMorePosts ? (
<Button onClick={handleLoadMore}>LoadMore...</Button>
<div ref={loadMoreButtonRef}>
<button onClick={handleLoadMore}></button>
</div>
) : (
<div>No More Posts...</div>
)
):(
isAnswers?(
answerMorePosts ? (
<Button onClick={handleLoadMore}>LoadMore...</Button>
<div ref={loadMoreButtonRef}>
<button onClick={handleLoadMore}></button>
</div>
) : (
<div>No More Posts...</div>
)
):
morePosts ? (
<Button onClick={handleLoadMore}>LoadMore</Button>
<div ref={loadMoreButtonRef}>
<button onClick={handleLoadMore}></button>
</div>
) : (
<div>No More Posts...</div>
)
Expand Down
6 changes: 5 additions & 1 deletion components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ const Navbar = ({}: Props) => {
const [notifications , setNotifications] = useState<any[]>([]);
const [limitCount, setLimitCount] = useState(6); // Number of notifications to fetch at a time
const [lastVisible, setLastVisible] = useState<any>(null);
const [showLoadMoreButton , setShowLoadMoreButton] = useState(true);

//for algolia search
const dispatch = useDispatch();
Expand Down Expand Up @@ -365,6 +366,9 @@ const Navbar = ({}: Props) => {
}, [user, limitCount]);

const loadMoreNotifications = () => {

setShowLoadMoreButton(false);

if (lastVisible) {
const notificationsRef = collection(db, "notifications");
const nextQ1 = query(
Expand Down Expand Up @@ -526,7 +530,7 @@ const clearNotifications = async () => {
))}
</DropdownMenuGroup>
<DropdownMenuSeparator />
{notifications.length>0&&
{notifications.length>0&& showLoadMoreButton &&
<div className="cursor-pointer w-full flex justify-center text-sm" onClick={loadMoreNotifications}>Load More</div>
}
</DropdownMenuContent>
Expand Down

0 comments on commit a47cbef

Please sign in to comment.