Skip to content

Commit f55e8bb

Browse files
authoredJul 7, 2023
Merge pull request #1779 from mkholjuraev/pdf-export
fix(usePDFExport): add missing dispatch call
2 parents 4228edd + 5d60122 commit f55e8bb

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed
 

‎packages/utils/src/useExportPDF/useExportPDF.test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ jest.mock('@redhat-cloud-services/frontend-components-notifications/redux', () =
88

99
window.URL.createObjectURL = jest.fn();
1010
global.fetch = jest.fn();
11+
const dispatch = jest.fn();
1112

1213
describe('useExportPDF', () => {
1314
it('Should download PDF', async () => {
1415
global.fetch.mockReturnValueOnce(Promise.resolve({ blob: jest.fn() }));
15-
const exportPDF = useExportPDF('vulnerability');
16+
const exportPDF = useExportPDF('vulnerability', dispatch);
1617
await exportPDF('executiveReport', 'vulnerability-test-export', { someRequestPayload: 'some value' });
1718
expect(fetch).toHaveBeenCalledWith(pdfGeneratorURL, {
1819
body: '{"service":"vulnerability","template":"executiveReport","params":{"someRequestPayload":"some value"}}',
@@ -32,7 +33,7 @@ describe('useExportPDF', () => {
3233
it('Should fail to download PDF with notification', async () => {
3334
global.fetch.mockReturnValueOnce(Promise.reject('error'));
3435

35-
const exportPDF = useExportPDF('vulnerability');
36+
const exportPDF = useExportPDF('vulnerability', dispatch);
3637
await exportPDF('executiveReport', 'vulnerability-test-export', { someRequestPayload: 'some value' });
3738
expect(addNotification).toHaveBeenCalledWith({
3839
description: 'Reinitiate this export to try again.',

‎packages/utils/src/useExportPDF/useExportPDF.ts

+21-15
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,32 @@ const renderPDF = (pdfBlob: Blob, fileName = 'new-report') => {
3030

3131
// Hook to provide a function that request pdf-generator service to generate report blob
3232
// and convert returned blob into pdf
33-
const usePDFExport = (service: string) => {
33+
const usePDFExport = (service: string, dispatch: any) => {
3434
const exportPDF = async (template: string, filename: string, exportSettings: Record<string, unknown>) => {
35-
addNotification({
36-
variant: 'info',
37-
title: 'Preparing export',
38-
description: 'Once complete, your download will start automatically.',
39-
});
35+
dispatch(
36+
addNotification({
37+
variant: 'info',
38+
title: 'Preparing export',
39+
description: 'Once complete, your download will start automatically.',
40+
})
41+
);
4042
try {
4143
const pdfBlob = await fetchPDF(service, template, exportSettings);
4244
renderPDF(pdfBlob, filename);
43-
addNotification({
44-
variant: 'success',
45-
title: 'Downloading export',
46-
});
45+
dispatch(
46+
addNotification({
47+
variant: 'success',
48+
title: 'Downloading export',
49+
})
50+
);
4751
} catch (e) {
48-
addNotification({
49-
variant: 'danger',
50-
title: 'Couldn’t download export',
51-
description: 'Reinitiate this export to try again.',
52-
});
52+
dispatch(
53+
addNotification({
54+
variant: 'danger',
55+
title: 'Couldn’t download export',
56+
description: 'Reinitiate this export to try again.',
57+
})
58+
);
5359
}
5460
};
5561

0 commit comments

Comments
 (0)
Please sign in to comment.