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

[MPDX-8230] Fix 14 month report export button doing nothing #1078

Merged
merged 3 commits into from
Sep 19, 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
2 changes: 2 additions & 0 deletions __tests__/util/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Object.defineProperty(window, 'location', {

window.HTMLElement.prototype.scrollIntoView = jest.fn();

window.URL.revokeObjectURL = jest.fn();

beforeEach(() => {
Settings.now = () => new Date(2020, 0, 1).valueOf();
matchMediaMock({ width: window.innerWidth });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ describe('FourteenMonthReportActions', () => {
);

expect(
getByRole('group', { hidden: true, name: 'Report header button group' }),
getByRole('group', { name: 'Report header button group' }),
).toBeInTheDocument();
userEvent.click(getByRole('button', { hidden: true, name: 'Expand' }));
userEvent.click(getByRole('button', { hidden: true, name: 'Print' }));
userEvent.click(getByRole('button', { hidden: true, name: 'Export' }));
userEvent.click(getByRole('button', { name: 'Expand' }));
userEvent.click(getByRole('button', { name: 'Print' }));
expect(getByRole('link', { name: 'Export' })).toHaveAttribute(
'href',
'data:text/csv;charset=utf-8,',
);
});

it('expand toggle event', async () => {
Expand All @@ -46,7 +49,7 @@ describe('FourteenMonthReportActions', () => {
</ThemeProvider>,
);

userEvent.click(getByRole('button', { hidden: true, name: 'Hide' }));
userEvent.click(getByRole('button', { name: 'Hide' }));
expect(onExpandToggle).toHaveBeenCalled();
});

Expand All @@ -64,7 +67,7 @@ describe('FourteenMonthReportActions', () => {
</ThemeProvider>,
);

userEvent.click(getByRole('button', { hidden: true, name: 'Print' }));
userEvent.click(getByRole('button', { name: 'Print' }));
expect(onPrint).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import FullscreenIcon from '@mui/icons-material/Fullscreen';
import FullscreenExitIcon from '@mui/icons-material/FullscreenExit';
import GetAppIcon from '@mui/icons-material/GetApp';
import PrintIcon from '@mui/icons-material/Print';
import { Button, ButtonGroup, SvgIcon } from '@mui/material';
import { styled } from '@mui/material/styles';
import { DateTime } from 'luxon';
import { CSVLink } from 'react-csv';
import { buildURI } from 'react-csv/lib/core';
import { useTranslation } from 'react-i18next';
import { FourteenMonthReportCurrencyType } from 'src/graphql/types.generated';

Expand All @@ -19,11 +18,6 @@ interface FourteenMonthReportActionsProps {
onPrint: (event: React.MouseEvent<unknown>) => void;
}

const DownloadCsvLink = styled(CSVLink)(({}) => ({
color: 'inherit',
textDecoration: 'none',
}));

export const FourteenMonthReportActions: React.FC<
FourteenMonthReportActionsProps
> = ({
Expand All @@ -35,6 +29,16 @@ export const FourteenMonthReportActions: React.FC<
onPrint,
}) => {
const { t } = useTranslation();
const [csvBlob, setCsvBlob] = useState('');

// This has to be a useEffect instead of a useMemo to prevent hydration errors because the
// server isn't able to calculate a blob URL.
useEffect(() => {
const csvBlob = buildURI(csvData);
setCsvBlob(csvBlob);

return () => URL.revokeObjectURL(csvBlob);
}, [csvData]);

return (
<ButtonGroup aria-label={t('Report header button group')}>
Expand All @@ -59,13 +63,10 @@ export const FourteenMonthReportActions: React.FC<
<GetAppIcon titleAccess={t('Download CSV Icon')} />
</SvgIcon>
}
href={csvBlob}
download={`mpdx-${currencyType}-contributions-export-${DateTime.now().toISODate()}.csv`}
>
<DownloadCsvLink
data={csvData}
filename={`mpdx-${currencyType}-contributions-export-${DateTime.now().toISODate()}.csv`}
>
{t('Export')}
</DownloadCsvLink>
{t('Export')}
</Button>
<Button
startIcon={
Expand Down
Loading