🐛(frontend) fix infinity scroll on invitation list

The infinity scroll had some difficulties to
load the next page of invitations because the ref
could be not init.
This commit fixes the issue.
This commit is contained in:
Anthony LC
2024-10-04 10:19:22 +02:00
committed by Anthony LC
parent 0b15ebba71
commit 98b60ebe93
2 changed files with 68 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
import { Loader } from '@openfun/cunningham-react';
import React, { useMemo, useRef } from 'react';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { APIError } from '@/api';
@@ -73,6 +73,7 @@ interface InvitationListProps {
export const InvitationList = ({ doc }: InvitationListProps) => {
const { t } = useTranslation();
const containerRef = useRef<HTMLDivElement>(null);
const [, setRefInitialized] = useState(false);
const {
data,
isLoading,
@@ -90,6 +91,17 @@ export const InvitationList = ({ doc }: InvitationListProps) => {
}, [] as Invitation[]);
}, [data?.pages]);
/**
* The "return null;" statement below blocks a necessary rerender
* for the InfiniteScroll component to work properly.
* This useEffect is a workaround to force the rerender.
*/
useEffect(() => {
if (containerRef.current) {
setRefInitialized(true);
}
}, [invitations?.length]);
if (!invitations?.length) {
return null;
}