Skip to content

Commit

Permalink
fix: start observing events only on page rendered
Browse files Browse the repository at this point in the history
  • Loading branch information
MDeLuise committed Sep 11, 2023
1 parent 2fe88b5 commit 7aff37a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
28 changes: 21 additions & 7 deletions frontend/src/components/AllLogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ export default function AllLogs(props: {
entries: diaryEntry[],
plants: plant[],
openEditEvent: (arg: diaryEntry) => void,
printError: (err: any) => void;
printError: (err: any) => void,
active: boolean;
}) {
const pageSize = 5;
const [entities, setEntities] = useState<diaryEntry[]>([]);
Expand All @@ -169,6 +170,8 @@ export default function AllLogs(props: {
const [fetchNew, setFetchNew] = useState<boolean>(true);
const [filteredPlantId, setFilteredPlantId] = useState<number[]>([]);
const [filteredEventType, setFilteredEventType] = useState<string[]>([]);
const [wasAlreadyRendered, setWasAlreadyRendered] = useState<boolean>(false);


const observerCallback = (entries: IntersectionObserverEntry[], _observer: IntersectionObserver) => {
const entry = entries[0];
Expand All @@ -177,11 +180,13 @@ export default function AllLogs(props: {
}
};


const observer = new IntersectionObserver(observerCallback, {
rootMargin: '0px',
threshold: 1.0,
});


useEffect(() => {
if (pageNo === -1) {
return;
Expand All @@ -191,12 +196,26 @@ export default function AllLogs(props: {
}
}, [pageNo, entities, fetchNew]);


useEffect(() => {
setPageNo(0);
setEntities([]);
setFetchNew(true);
}, [filteredPlantId, filteredEventType, props.entries]);


useEffect(() => {
if (!wasAlreadyRendered && props.active) {
setTimeout(() => {
if (myRef.current !== undefined) {
observer.observe(myRef.current as Element);
}
}, 700);
setWasAlreadyRendered(true);
}
}, [props.active]);


const getPage = () => {
if (myRef.current !== undefined && myRef.current !== null) {
observer.unobserve(myRef.current);
Expand Down Expand Up @@ -239,11 +258,6 @@ export default function AllLogs(props: {

useEffect(() => {
setPageNo(0);
setTimeout(() => {
if (myRef.current !== undefined) {
observer.observe(myRef.current as Element);
}
}, 700);
}, []);

const myRef = useRef<Element>();
Expand Down Expand Up @@ -273,7 +287,7 @@ export default function AllLogs(props: {
return <LogEntry
entity={entity}
last={index == entities.length - 1}
key={entity.id}
key={index}
lastRef={myRef}
editEvent={() => props.openEditEvent(entity)}
/>;
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ export default function Home(props: { isLoggedIn: () => boolean, requestor: Axio
setEditEventVisible(true);
}}
printError={printError}
active={activeTab == 1}
/>
</Box>

Expand All @@ -350,7 +351,7 @@ export default function Home(props: { isLoggedIn: () => boolean, requestor: Axio
requestor={props.requestor}
visibility={activeTab === 3}
printError={printError}
/>
/>
</Box>
</Box>

Expand Down

0 comments on commit 7aff37a

Please sign in to comment.