diff --git a/src/components/Coaching/CoachingDetail/CoachingDetail.tsx b/src/components/Coaching/CoachingDetail/CoachingDetail.tsx index a17162267..da74beeeb 100644 --- a/src/components/Coaching/CoachingDetail/CoachingDetail.tsx +++ b/src/components/Coaching/CoachingDetail/CoachingDetail.tsx @@ -10,13 +10,15 @@ import { MonthlyCommitment } from './MonthlyCommitment/MonthlyCommitment'; import { useGetAccountListCoachUsersQuery, useGetAccountListUsersQuery, + useGetCoachingDonationGraphQuery, useLoadAccountListCoachingDetailQuery, useLoadCoachingDetailQuery, } from './LoadCoachingDetail.generated'; import theme from 'src/theme'; -import { MonthlyActivitySection } from 'src/components/Reports/DonationsReport/MonthlyActivity/MonthlyActivitySection'; import { currencyFormat } from 'src/lib/intlFormat'; import { useLocale } from 'src/hooks/useLocale'; +import DonationHistories from 'src/components/Dashboard/DonationHistories'; +import { useGetDonationGraphQuery } from 'src/components/Reports/DonationsReport/GetDonationGraph.generated'; interface CoachingDetailProps { coachingId: string; @@ -86,11 +88,10 @@ export const CoachingDetail: React.FC = ({ }) => { const { t } = useTranslation(); const locale = useLocale(); - const { data: accountListData, loading } = - useLoadAccountListCoachingDetailQuery({ - variables: { coachingId }, - skip: !isAccountListId, - }); + const { data: ownData, loading } = useLoadAccountListCoachingDetailQuery({ + variables: { coachingId }, + skip: !isAccountListId, + }); const { data: coachingData, loading: coachingLoading } = useLoadCoachingDetailQuery({ @@ -108,10 +109,28 @@ export const CoachingDetail: React.FC = ({ variables: { accountListId: coachingId }, }); - const data = isAccountListId - ? accountListData?.accountList + const { data: ownDonationGraphData } = useGetDonationGraphQuery({ + variables: { + accountListId: coachingId, + }, + skip: !isAccountListId, + }); + + const { data: coachingDonationGraphData } = useGetCoachingDonationGraphQuery({ + variables: { + coachingAccountListId: coachingId, + }, + skip: isAccountListId, + }); + + const accountListData = isAccountListId + ? ownData?.accountList : coachingData?.coachingAccountList; + const donationGraphData = isAccountListId + ? ownDonationGraphData + : coachingDonationGraphData; + const [isMonthly, setIsMonthly] = useState(true); return ( @@ -171,8 +190,8 @@ export const CoachingDetail: React.FC = ({ {t('Commitment Goal:') + ' ' + currencyFormat( - data?.monthlyGoal ? data?.monthlyGoal : 0, - data?.currency, + accountListData?.monthlyGoal ? accountListData?.monthlyGoal : 0, + accountListData?.currency, locale, )} @@ -248,33 +267,35 @@ export const CoachingDetail: React.FC = ({ margin: theme.spacing(1), }} > - {data?.name} + {accountListData?.name} - {/* - TODO: MonthlyActivitySection doesn't work if coaching is not one of the - Accountlists. reportDonationsHistories is required for this View and it doesn't - work with coaching. - */} - + diff --git a/src/components/Coaching/CoachingDetail/LoadCoachingDetail.graphql b/src/components/Coaching/CoachingDetail/LoadCoachingDetail.graphql index d5d2d2880..841407701 100644 --- a/src/components/Coaching/CoachingDetail/LoadCoachingDetail.graphql +++ b/src/components/Coaching/CoachingDetail/LoadCoachingDetail.graphql @@ -71,3 +71,9 @@ query GetAccountListUsers($accountListId: ID!) { } } } + +query GetCoachingDonationGraph($coachingAccountListId: ID!) { + reportsDonationHistories(accountListId: $coachingAccountListId) { + ...DonationGraphHistories + } +} diff --git a/src/components/Reports/DonationsReport/DonationsReport.test.tsx b/src/components/Reports/DonationsReport/DonationsReport.test.tsx index 4a0701846..b9cb0b27f 100644 --- a/src/components/Reports/DonationsReport/DonationsReport.test.tsx +++ b/src/components/Reports/DonationsReport/DonationsReport.test.tsx @@ -174,4 +174,65 @@ describe('DonationsReport', () => { ).not.toBeInTheDocument(); expect(getByTestId('donationRow')).toBeInTheDocument(); }); + + it('filters report by designation account', async () => { + const mutationSpy = jest.fn(); + render( + + + mocks={mocks} onCall={mutationSpy}> + + + + , + ); + + await waitFor(() => + expect(mutationSpy.mock.calls[2][0]).toMatchObject({ + operation: { + operationName: 'GetDonationGraph', + variables: { + designationAccountIds: ['account-1'], + }, + }, + }), + ); + }); + + it('does not filter report by designation account', async () => { + const mutationSpy = jest.fn(); + render( + + + mocks={mocks} onCall={mutationSpy}> + + + + , + ); + + await waitFor(() => + expect(mutationSpy.mock.calls[2][0]).toMatchObject({ + operation: { + operationName: 'GetDonationGraph', + variables: { + designationAccountIds: null, + }, + }, + }), + ); + }); }); diff --git a/src/components/Reports/DonationsReport/DonationsReport.tsx b/src/components/Reports/DonationsReport/DonationsReport.tsx index 57abca624..f6c6f4440 100644 --- a/src/components/Reports/DonationsReport/DonationsReport.tsx +++ b/src/components/Reports/DonationsReport/DonationsReport.tsx @@ -2,10 +2,10 @@ import React, { useEffect, useState } from 'react'; import { Container, Box } from '@mui/material'; import { DateTime } from 'luxon'; import { useRouter } from 'next/router'; -import { ISODateString } from 'next-auth'; import { AccountsListHeader as Header } from '../AccountsListLayout/Header/Header'; -import { MonthlyActivitySection } from './MonthlyActivity/MonthlyActivitySection'; import { DonationsReportTable } from './Table/DonationsReportTable'; +import DonationHistories from 'src/components/Dashboard/DonationHistories'; +import { useGetDonationGraphQuery } from './GetDonationGraph.generated'; interface DonationReportsProps { accountListId: string; @@ -26,9 +26,19 @@ export const DonationsReport: React.FC = ({ }) => { const [time, setTime] = useState(DateTime.now().startOf('month')); const { query, replace } = useRouter(); + + const { data } = useGetDonationGraphQuery({ + variables: { + accountListId, + designationAccountIds: designationAccounts?.length + ? designationAccounts + : null, + }, + }); + useEffect(() => { - if (query.month) { - setTime(DateTime.fromISO(query.month as ISODateString)); + if (typeof query.month === 'string') { + setTime(DateTime.fromISO(query.month)); } replace( { @@ -47,9 +57,11 @@ export const DonationsReport: React.FC = ({ title={title} /> - { - return ( - - mocks={{ - GetDonationGraph: { - accountList: { - currency: 'CAD', - monthlyGoal: 100, - totalPledges: 10, - }, - reportsDonationHistories: { - averageIgnoreCurrent: 10, - periods: [ - { - startDate: DateTime.now().minus({ months: 12 }).toISO(), - convertedTotal: 10, - totals: [ - { - currency: 'CAD', - convertedAmount: 70, - }, - ], - }, - { - startDate: DateTime.now().minus({ months: 11 }).toISO(), - convertedTotal: 10, - totals: [ - { - currency: 'USD', - convertedAmount: 50, - }, - ], - }, - ], - }, - }, - }} - > - {}} /> - - ); -}; - -export const Empty = (): ReactElement => { - return ( - - mocks={{ - GetDonationGraph: { - accountList: { - currency: 'CAD', - monthlyGoal: 0, - totalPledges: 0, - }, - reportsDonationHistories: { - averageIgnoreCurrent: 0, - periods: [ - { - startDate: DateTime.now().minus({ months: 12 }).toISO(), - convertedTotal: 0, - totals: [ - { - currency: 'CAD', - convertedAmount: 0, - }, - ], - }, - { - startDate: DateTime.now().minus({ months: 11 }).toISO(), - convertedTotal: 0, - totals: [ - { - currency: 'CAD', - convertedAmount: 0, - }, - ], - }, - ], - }, - }, - }} - > - {}} /> - - ); -}; diff --git a/src/components/Reports/DonationsReport/MonthlyActivity/MonthlyActivitySection.test.tsx b/src/components/Reports/DonationsReport/MonthlyActivity/MonthlyActivitySection.test.tsx deleted file mode 100644 index 9e07e991c..000000000 --- a/src/components/Reports/DonationsReport/MonthlyActivity/MonthlyActivitySection.test.tsx +++ /dev/null @@ -1,206 +0,0 @@ -import React from 'react'; -import { render, waitFor } from '@testing-library/react'; -import { ThemeProvider } from '@mui/material/styles'; -import { DateTime } from 'luxon'; -import theme from '../../../../theme'; -import { GetDonationGraphQuery } from '../GetDonationGraph.generated'; -import { GqlMockedProvider } from '../../../../../__tests__/util/graphqlMocking'; -import { MonthlyActivitySection } from './MonthlyActivitySection'; -import TestRouter from '__tests__/util/TestRouter'; -import { - beforeTestResizeObserver, - afterTestResizeObserver, -} from 'src/utils/tests/windowResizeObserver'; - -const setTime = jest.fn(); - -const push = jest.fn(); - -const router = { - query: { accountListId: '123' }, - isReady: true, - push, -}; - -const mocks = { - GetDonationGraph: { - accountList: { - currency: 'CAD', - monthlyGoal: 100, - totalPledges: 10, - }, - reportsDonationHistories: { - averageIgnoreCurrent: 10, - periods: [ - { - startDate: DateTime.now().minus({ months: 12 }).toISO(), - convertedTotal: 10, - totals: [ - { - currency: 'CAD', - convertedAmount: 10, - }, - ], - }, - { - startDate: DateTime.now().minus({ months: 11 }).toISO(), - convertedTotal: 10, - totals: [ - { - currency: 'CAD', - convertedAmount: 10, - }, - ], - }, - ], - }, - }, -}; - -describe('Render Monthly Activity Section', () => { - beforeEach(() => { - beforeTestResizeObserver(); - }); - - afterEach(() => { - afterTestResizeObserver(); - }); - - it('renders with data', async () => { - const { getByTestId, queryByRole, queryByTestId } = render( - - - - mocks={mocks} - > - - - - , - ); - - await waitFor(() => { - expect(queryByRole('progressbar')).not.toBeInTheDocument(); - expect( - getByTestId('DonationHistoriesTypographyAverage'), - ).toBeInTheDocument(); - }); - expect( - queryByTestId('DonationHistoriesGridLoading'), - ).not.toBeInTheDocument(); - }); - - it('renders empty', async () => { - const mocks = { - GetDonationGraph: { - accountList: { - currency: 'CAD', - monthlyGoal: 0, - totalPledges: 0, - }, - reportsDonationHistories: { - averageIgnoreCurrent: 0, - periods: [ - { - startDate: DateTime.now().minus({ months: 12 }).toISO(), - convertedTotal: 0, - totals: [ - { - currency: 'CAD', - convertedAmount: 0, - }, - ], - }, - { - startDate: DateTime.now().minus({ months: 11 }).toISO(), - convertedTotal: 0, - totals: [ - { - currency: 'CAD', - convertedAmount: 0, - }, - ], - }, - ], - }, - }, - }; - - const { queryByText, queryByRole, getByTestId } = render( - - - - mocks={mocks} - > - - - - , - ); - - await waitFor(() => - expect(queryByRole('progressbar')).not.toBeInTheDocument(), - ); - expect(getByTestId('DonationHistoriesBoxEmpty')).toBeInTheDocument(); - expect(queryByText('Average')).not.toBeInTheDocument(); - }); - - it('filters report by designation account', async () => { - const mutationSpy = jest.fn(); - render( - - - - - - - , - ); - - await waitFor(() => - expect(mutationSpy.mock.calls[0][0]).toMatchObject({ - operation: { - operationName: 'GetDonationGraph', - variables: { - designationAccountIds: ['account-1'], - }, - }, - }), - ); - }); - - it('does not filter report by designation account', async () => { - const mutationSpy = jest.fn(); - render( - - - - mocks={mocks} - onCall={mutationSpy} - > - - - - , - ); - - await waitFor(() => - expect(mutationSpy.mock.calls[0][0]).toMatchObject({ - operation: { - operationName: 'GetDonationGraph', - variables: { - designationAccountIds: null, - }, - }, - }), - ); - }); -}); diff --git a/src/components/Reports/DonationsReport/MonthlyActivity/MonthlyActivitySection.tsx b/src/components/Reports/DonationsReport/MonthlyActivity/MonthlyActivitySection.tsx deleted file mode 100644 index a876ac56b..000000000 --- a/src/components/Reports/DonationsReport/MonthlyActivity/MonthlyActivitySection.tsx +++ /dev/null @@ -1,35 +0,0 @@ -import { DateTime } from 'luxon'; -import React from 'react'; -import DonationHistories from '../../../Dashboard/DonationHistories'; -import { useGetDonationGraphQuery } from '../GetDonationGraph.generated'; - -interface Props { - accountListId: string; - designationAccounts?: string[]; - setTime?: (time: DateTime) => void; -} - -export const MonthlyActivitySection: React.FC = ({ - accountListId, - designationAccounts, - setTime, -}) => { - const { data } = useGetDonationGraphQuery({ - variables: { - accountListId, - designationAccountIds: designationAccounts?.length - ? designationAccounts - : null, - }, - }); - - return ( - - ); -};