Skip to content

Commit

Permalink
Merge pull request #1258 from CruGlobal/remove-limit-from-fetching-co…
Browse files Browse the repository at this point in the history
…aches

MPDX-8515 - Fetching all pages on the coaches page
  • Loading branch information
dr-bizz authored Jan 17, 2025
2 parents e7677b9 + d3ffe3d commit 28bd6d3
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 14 deletions.
72 changes: 72 additions & 0 deletions src/components/Coaching/CoachingList.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React from 'react';
import { ThemeProvider } from '@mui/material/styles';
import { render } from '@testing-library/react';
import { ErgonoMockShape } from 'graphql-ergonomock';
import TestRouter from '__tests__/util/TestRouter';
import TestWrapper from '__tests__/util/TestWrapper';
import { GqlMockedProvider } from '__tests__/util/graphqlMocking';
import theme from 'src/theme';
import { CoachingList } from './CoachingList';
import { LoadCoachingListQuery } from './LoadCoachingList.generated';

const accountListId = 'accountListId';
const mutationSpy = jest.fn();

const coachingAccountListsMock = [
{
id: '1',
name: 'Account 1',
},
{
id: '2',
name: 'Account 2',
},
];
const router = {
query: { accountListId: accountListId },
push: jest.fn(),
};

interface ComponentsProps {
mockNodes?: ErgonoMockShape[];
}
const Components = ({
mockNodes = coachingAccountListsMock,
}: ComponentsProps) => (
<ThemeProvider theme={theme}>
<TestRouter router={router}>
<TestWrapper>
<GqlMockedProvider<{
coachingAccountLists: LoadCoachingListQuery;
}>
mocks={{
LoadCoachingList: {
coachingAccountLists: {
nodes: mockNodes,
totalCount: 2,
},
},
}}
onCall={mutationSpy}
>
<CoachingList accountListId={accountListId} />
</GqlMockedProvider>
</TestWrapper>
</TestRouter>
</ThemeProvider>
);

describe('CoachingList', () => {
it('should render loading state', () => {
const { getAllByTestId } = render(<Components />);

expect(getAllByTestId('loading-coaches')[0]).toBeInTheDocument();
});

it('should render coaching accounts', async () => {
const { findByText, getByText } = render(<Components />);

expect(await findByText('Account 1')).toBeInTheDocument();
expect(getByText('Account 2')).toBeInTheDocument();
});
});
33 changes: 22 additions & 11 deletions src/components/Coaching/CoachingList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import { Spa } from '@mui/icons-material';
import { Box, Divider, Skeleton, Typography } from '@mui/material';
import { styled } from '@mui/material/styles';
import { useTranslation } from 'react-i18next';
import { CoachingRow } from './CoachingRow/CoachingRow';
import { useFetchAllPages } from 'src/hooks/useFetchAllPages';
import { CoachingRow, CoachingRowWrapper } from './CoachingRow/CoachingRow';
import { useLoadCoachingListQuery } from './LoadCoachingList.generated';

interface CoachingListProps {
accountListId: string;
}
const LoadingCoach: React.FC = () => (
<CoachingRowWrapper>
<CoachingLoading role="listitem" data-testid="loading-coaches" />
</CoachingRowWrapper>
);

const CoachingListWrapper = styled(Box)(({ theme }) => ({
width: '100%',
Expand All @@ -32,19 +35,27 @@ const CoachingListTitle = styled(Typography)(({ theme }) => ({
}));

const CoachingLoading = styled(Skeleton)(() => ({
width: '75%',
height: '50px',
height: '100px',
}));

interface CoachingListProps {
accountListId: string;
}
export const CoachingList: React.FC<CoachingListProps> = ({
accountListId,
}) => {
// This needs to become a infinite scroll query
const { data, loading } = useLoadCoachingListQuery();
const { data, fetchMore, error } = useLoadCoachingListQuery();
const { t } = useTranslation();

const coachingAccounts = data?.coachingAccountLists;

useFetchAllPages({
fetchMore,
error,
pageInfo: coachingAccounts?.pageInfo,
});

return (
<CoachingListWrapper>
<CoachingTitleWrapper>
Expand All @@ -55,11 +66,11 @@ export const CoachingList: React.FC<CoachingListProps> = ({
</CoachingTitleWrapper>
<Divider />
<Box>
{loading ? (
{!data && !error ? (
<>
<CoachingLoading role="listitem" />
<CoachingLoading role="listitem" />
<CoachingLoading role="listitem" />
<LoadingCoach />
<LoadingCoach />
<LoadingCoach />
</>
) : (
coachingAccounts?.nodes.map((coachingAccount, _index) => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Coaching/CoachingRow/CoachingRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface Props {
accountListId: string;
}

const CoachingRowWrapper = styled(Box)(({ theme }) => ({
export const CoachingRowWrapper = styled(Box)(({ theme }) => ({
width: '100%',
maxWidth: '950px',
margin: 'auto',
Expand Down
4 changes: 2 additions & 2 deletions src/components/Coaching/LoadCoachingList.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
query LoadCoachingList {
coachingAccountLists(first: 100) {
query LoadCoachingList($after: String) {
coachingAccountLists(first: 25, after: $after) {
nodes {
...CoachedPerson
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/apollo/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const createCache = () =>
accountListPledges: paginationFieldPolicy,
appeals: paginationFieldPolicy,
coachingAccountListPledges: paginationFieldPolicy,
coachingAccountLists: paginationFieldPolicy,
contacts: paginationFieldPolicy,
donations: paginationFieldPolicy,
financialAccounts: paginationFieldPolicy,
Expand Down

0 comments on commit 28bd6d3

Please sign in to comment.