Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SELC-4661] Feat: Optimize rendering of select institutions page #712

Merged
merged 4 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions src/components/partySelectionSearch/PartySelectionSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import { Grid, Typography, Box, useTheme } from '@mui/material';
import { styled } from '@mui/material/styles';
import { useTranslation } from 'react-i18next';
Expand Down Expand Up @@ -45,13 +45,44 @@ export default function PartySelectionSearch({
partyTitle,
}: Props) {
const { t } = useTranslation();
const [input, setInput] = useState('');
const theme = useTheme();
const [visibleParties, setVisibleParties] = useState<Array<BaseParty>>([]);
const [filteredParties, setFilteredParties] = useState<Array<BaseParty>>(parties);
const containerRef = useRef<HTMLDivElement>(null);
const [searchQuery, setSearchQuery] = useState('');

const theme = useTheme();
useEffect(() => {
setVisibleParties(filteredParties.slice(0, 50));
}, [filteredParties]);

useEffect(() => {
const handleScroll = () => {
if (
containerRef.current &&
containerRef.current.scrollHeight - containerRef.current.scrollTop <=
containerRef.current.clientHeight + 20 &&
filteredParties.length > visibleParties.length
) {
// User has scrolled to the bottom, load more parties
loadMoreParties();
}
};

containerRef.current?.addEventListener('scroll', handleScroll);

return () => {
containerRef.current?.removeEventListener('scroll', handleScroll);
};
}, [visibleParties, filteredParties, selectedParty]);

const loadMoreParties = () => {
const remainingParties = filteredParties.slice(visibleParties.length);
const nextBatch = remainingParties.slice(0, 50);
setVisibleParties((prevParties) => [...prevParties, ...nextBatch]);
};

const onFilterChange = (value: string) => {
setInput(value);
setSearchQuery(value);
if (!value) {
setFilteredParties(parties);
} else {
Expand Down Expand Up @@ -82,7 +113,7 @@ export default function PartySelectionSearch({
label={label}
iconMarginRight={iconMarginRight}
onChange={(e) => onFilterChange(e.target.value)}
input={input}
input={searchQuery}
clearField={() => onFilterChange('')}
iconColor={iconColor}
/>
Expand Down Expand Up @@ -127,9 +158,12 @@ export default function PartySelectionSearch({
{t('partySelection.notFoundResults')}
</Typography>
) : (
<CustomBox sx={{ pointerEvents: parties.length !== 1 ? 'auto' : 'none' }}>
{filteredParties &&
filteredParties.map((party) => {
<CustomBox
ref={containerRef}
sx={{ pointerEvents: parties.length !== 1 ? 'auto' : 'none' }}
>
{visibleParties &&
visibleParties.map((party) => {
const isDisabled =
party.status === 'PENDING' || party.status === 'TOBEVALIDATED';
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { fireEvent, getByText, render, screen } from '@testing-library/react';
import { Party } from '../../../model/Party';
import { fireEvent, getByText, render, screen, waitFor } from '@testing-library/react';
import { BaseParty, Party } from '../../../model/Party';
import PartyAccountItemSelection from '../PartyAccountItemSelection';
import PartySelectionSearch from '../PartySelectionSearch';
import './../../../locale';
import { mockedBaseParties } from '../../../services/__mocks__/partyService';
import React from 'react';
import { renderWithProviders } from '../../../utils/test-utils';

let selectedParty: Party | null = null;
let selectedParty: BaseParty | null = null;

const parties: Array<Party> = [
{
Expand Down Expand Up @@ -215,3 +218,39 @@ test('Test TOBEVALIDATED party', () => {
screen.getByText('In attesa');
}
});

test('Test disabled party', async () => {
const generateMockedParties = (N: number): Array<BaseParty> =>
Array.from({ length: N }, (_, index) => {
const partyId = `party-${index}`;
return {
partyId,
description: `Party ${index}`,
status: index % 2 === 0 ? 'ACTIVE' : 'PENDING',
userRole: index % 2 === 0 ? 'ADMIN' : 'LIMITED',
};
});

const mockedBaseParties = generateMockedParties(60);
renderWithProviders(
<PartySelectionSearch
parties={mockedBaseParties}
onPartySelectionChange={(p) => (selectedParty = p)}
selectedParty={selectedParty}
/>
);

const party40 = screen.getByText('Party 40');
expect(party40).toBeInTheDocument();

const party52 = screen.queryByText('Party 52');
expect(party52).not.toBeInTheDocument();

const input = document.getElementById('search') as HTMLInputElement;
fireEvent.change(input, { target: { value: 'Party 5' } });
expect(input.getAttribute('value')).toBe('Party 5');

expect(party40).not.toBeInTheDocument();

await waitFor(() => expect(screen.getByText('Party 52')).toBeInTheDocument());
});
Loading