forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Export as PDF - rasterized): Adding rasterized pdf functionality…
… to dashboard (apache#25696)
- Loading branch information
Showing
14 changed files
with
633 additions
and
36 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
superset-frontend/src/dashboard/components/menu/DownloadMenuItems/DownloadAsImage.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React, { SyntheticEvent } from 'react'; | ||
import { render, screen, waitFor } from 'spec/helpers/testing-library'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { Menu } from 'src/components/Menu'; | ||
import downloadAsImage from 'src/utils/downloadAsImage'; | ||
import DownloadAsImage from './DownloadAsImage'; | ||
|
||
jest.mock('src/utils/downloadAsImage', () => ({ | ||
__esModule: true, | ||
default: jest.fn(() => (_e: SyntheticEvent) => {}), | ||
})); | ||
|
||
const createProps = () => ({ | ||
addDangerToast: jest.fn(), | ||
text: 'Download as Image', | ||
dashboardTitle: 'Test Dashboard', | ||
logEvent: jest.fn(), | ||
}); | ||
|
||
const renderComponent = () => { | ||
render( | ||
<Menu> | ||
<DownloadAsImage {...createProps()} /> | ||
</Menu>, | ||
); | ||
}; | ||
|
||
test('Should call download image on click', async () => { | ||
const props = createProps(); | ||
renderComponent(); | ||
await waitFor(() => { | ||
expect(downloadAsImage).toBeCalledTimes(0); | ||
expect(props.addDangerToast).toBeCalledTimes(0); | ||
}); | ||
|
||
userEvent.click(screen.getByRole('button', { name: 'Download as Image' })); | ||
|
||
await waitFor(() => { | ||
expect(downloadAsImage).toBeCalledTimes(1); | ||
expect(props.addDangerToast).toBeCalledTimes(0); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
superset-frontend/src/dashboard/components/menu/DownloadMenuItems/DownloadAsImage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React, { SyntheticEvent } from 'react'; | ||
import { logging, t } from '@superset-ui/core'; | ||
import { Menu } from 'src/components/Menu'; | ||
import { LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_IMAGE } from 'src/logger/LogUtils'; | ||
import downloadAsImage from 'src/utils/downloadAsImage'; | ||
|
||
export default function DownloadAsImage({ | ||
text, | ||
logEvent, | ||
dashboardTitle, | ||
addDangerToast, | ||
...rest | ||
}: { | ||
text: string; | ||
addDangerToast: Function; | ||
dashboardTitle: string; | ||
logEvent?: Function; | ||
}) { | ||
const SCREENSHOT_NODE_SELECTOR = '.dashboard'; | ||
const onDownloadImage = async (e: SyntheticEvent) => { | ||
try { | ||
downloadAsImage(SCREENSHOT_NODE_SELECTOR, dashboardTitle, true)(e); | ||
} catch (error) { | ||
logging.error(error); | ||
addDangerToast(t('Sorry, something went wrong. Try again later.')); | ||
} | ||
logEvent?.(LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_IMAGE); | ||
}; | ||
|
||
return ( | ||
<Menu.Item key="download-image" {...rest}> | ||
<div onClick={onDownloadImage} role="button" tabIndex={0}> | ||
{text} | ||
</div> | ||
</Menu.Item> | ||
); | ||
} |
42 changes: 42 additions & 0 deletions
42
superset-frontend/src/dashboard/components/menu/DownloadMenuItems/DownloadAsPdf.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React, { SyntheticEvent } from 'react'; | ||
import { render, screen, waitFor } from 'spec/helpers/testing-library'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { Menu } from 'src/components/Menu'; | ||
import downloadAsPdf from 'src/utils/downloadAsPdf'; | ||
import DownloadAsPdf from './DownloadAsPdf'; | ||
|
||
jest.mock('src/utils/downloadAsPdf', () => ({ | ||
__esModule: true, | ||
default: jest.fn(() => (_e: SyntheticEvent) => {}), | ||
})); | ||
|
||
const createProps = () => ({ | ||
addDangerToast: jest.fn(), | ||
text: 'Export as PDF', | ||
dashboardTitle: 'Test Dashboard', | ||
logEvent: jest.fn(), | ||
}); | ||
|
||
const renderComponent = () => { | ||
render( | ||
<Menu> | ||
<DownloadAsPdf {...createProps()} /> | ||
</Menu>, | ||
); | ||
}; | ||
|
||
test('Should call download pdf on click', async () => { | ||
const props = createProps(); | ||
renderComponent(); | ||
await waitFor(() => { | ||
expect(downloadAsPdf).toBeCalledTimes(0); | ||
expect(props.addDangerToast).toBeCalledTimes(0); | ||
}); | ||
|
||
userEvent.click(screen.getByRole('button', { name: 'Export as PDF' })); | ||
|
||
await waitFor(() => { | ||
expect(downloadAsPdf).toBeCalledTimes(1); | ||
expect(props.addDangerToast).toBeCalledTimes(0); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
superset-frontend/src/dashboard/components/menu/DownloadMenuItems/DownloadAsPdf.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React, { SyntheticEvent } from 'react'; | ||
import { logging, t } from '@superset-ui/core'; | ||
import { Menu } from 'src/components/Menu'; | ||
import downloadAsPdf from 'src/utils/downloadAsPdf'; | ||
import { LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_PDF } from 'src/logger/LogUtils'; | ||
|
||
export default function DownloadAsPdf({ | ||
text, | ||
logEvent, | ||
dashboardTitle, | ||
addDangerToast, | ||
...rest | ||
}: { | ||
text: string; | ||
addDangerToast: Function; | ||
dashboardTitle: string; | ||
logEvent?: Function; | ||
}) { | ||
const SCREENSHOT_NODE_SELECTOR = '.dashboard'; | ||
const onDownloadPdf = async (e: SyntheticEvent) => { | ||
try { | ||
downloadAsPdf(SCREENSHOT_NODE_SELECTOR, dashboardTitle, true)(e); | ||
} catch (error) { | ||
logging.error(error); | ||
addDangerToast(t('Sorry, something went wrong. Try again later.')); | ||
} | ||
logEvent?.(LOG_ACTIONS_DASHBOARD_DOWNLOAD_AS_PDF); | ||
}; | ||
|
||
return ( | ||
<Menu.Item key="download-pdf" {...rest}> | ||
<div onClick={onDownloadPdf} role="button" tabIndex={0}> | ||
{text} | ||
</div> | ||
</Menu.Item> | ||
); | ||
} |
25 changes: 25 additions & 0 deletions
25
superset-frontend/src/dashboard/components/menu/DownloadMenuItems/DownloadMenuItems.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from 'react'; | ||
import { render, screen } from 'spec/helpers/testing-library'; | ||
import DownloadMenuItems from '.'; | ||
|
||
const createProps = () => ({ | ||
addDangerToast: jest.fn(), | ||
pdfMenuItemTitle: 'Export to PDF', | ||
imageMenuItemTitle: 'Download as Image', | ||
dashboardTitle: 'Test Dashboard', | ||
logEvent: jest.fn(), | ||
}); | ||
|
||
const renderComponent = () => { | ||
render(<DownloadMenuItems {...createProps()} />); | ||
}; | ||
|
||
test('Should render menu items', () => { | ||
renderComponent(); | ||
expect( | ||
screen.getByRole('menuitem', { name: 'Export to PDF' }), | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.getByRole('menuitem', { name: 'Download as Image' }), | ||
).toBeInTheDocument(); | ||
}); |
62 changes: 62 additions & 0 deletions
62
superset-frontend/src/dashboard/components/menu/DownloadMenuItems/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import React from 'react'; | ||
import { Menu } from 'src/components/Menu'; | ||
import DownloadAsImage from './DownloadAsImage'; | ||
import DownloadAsPdf from './DownloadAsPdf'; | ||
|
||
export interface DownloadMenuItemProps { | ||
pdfMenuItemTitle: string; | ||
imageMenuItemTitle: string; | ||
addDangerToast: Function; | ||
dashboardTitle: string; | ||
logEvent?: Function; | ||
} | ||
|
||
const DownloadMenuItems = (props: DownloadMenuItemProps) => { | ||
const { | ||
pdfMenuItemTitle, | ||
imageMenuItemTitle, | ||
addDangerToast, | ||
dashboardTitle, | ||
logEvent, | ||
...rest | ||
} = props; | ||
|
||
return ( | ||
<Menu selectable={false}> | ||
<DownloadAsPdf | ||
text={pdfMenuItemTitle} | ||
addDangerToast={addDangerToast} | ||
dashboardTitle={dashboardTitle} | ||
logEvent={logEvent} | ||
{...rest} | ||
/> | ||
<DownloadAsImage | ||
text={imageMenuItemTitle} | ||
addDangerToast={addDangerToast} | ||
dashboardTitle={dashboardTitle} | ||
logEvent={logEvent} | ||
{...rest} | ||
/> | ||
</Menu> | ||
); | ||
}; | ||
|
||
export default DownloadMenuItems; |
Oops, something went wrong.