-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ADDON-65107 implement download button for openapi (#932)
feat: ADDON-65107 implement download button for openapi
- Loading branch information
1 parent
b385759
commit f71d7ec
Showing
8 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
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
19 changes: 19 additions & 0 deletions
19
ui/src/components/DownloadButton/DownloadButton.stories.ts
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,19 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import DownloadButton from './DownloadButton'; | ||
|
||
const meta = { | ||
component: DownloadButton, | ||
title: 'Components/DownloadButton', | ||
} satisfies Meta<typeof DownloadButton>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Base: Story = { | ||
args: { | ||
// using index.json as it needs to be from the same domain | ||
fileUrl: '/index.json', | ||
children: 'Download', | ||
fileNameAfterDownload: 'index.json', | ||
}, | ||
}; |
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 '@testing-library/react'; | ||
import DownloadButton from './DownloadButton'; | ||
|
||
describe('DownloadButton', () => { | ||
it('Check if download button displays content correctly', async () => { | ||
const btnText = 'some btn text'; | ||
const exampleContent = { | ||
fileUrl: 'http://localhost:6006/index.json', | ||
children: btnText, | ||
fileNameAfterDownload: 'fileName', | ||
}; | ||
|
||
render(<DownloadButton {...exampleContent} />); | ||
|
||
const downloadBtn: HTMLAnchorElement = screen.getByTestId('downloadButton'); | ||
expect(downloadBtn).toBeInTheDocument(); | ||
|
||
expect(downloadBtn).toHaveTextContent(btnText); | ||
|
||
expect(downloadBtn.href).toEqual(exampleContent.fileUrl); | ||
|
||
expect(downloadBtn.download).toEqual(exampleContent.fileNameAfterDownload); | ||
}); | ||
}); |
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,24 @@ | ||
import React, { ReactElement } from 'react'; | ||
import Button from '@splunk/react-ui/Button'; | ||
|
||
interface DownloadButtonProps { | ||
// needs to be same domain if not it will just open link | ||
fileUrl: string; | ||
fileNameAfterDownload: string; | ||
children: ReactElement | ReactElement[] | string; | ||
} | ||
|
||
function DownloadButton(props: DownloadButtonProps) { | ||
return ( | ||
<Button | ||
target="_blank" | ||
to={props.fileUrl} | ||
download={props.fileNameAfterDownload} | ||
data-test="downloadButton" | ||
> | ||
{props.children} | ||
</Button> | ||
); | ||
} | ||
|
||
export default DownloadButton; |
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,24 @@ | ||
import React from 'react'; | ||
import { createRESTURL } from '@splunk/splunk-utils/url'; | ||
import { app } from '@splunk/splunk-utils/config'; | ||
import Icon from '@splunk/react-icons/ArrowBroadUnderbarDown'; | ||
import DownloadButton from './DownloadButton'; | ||
|
||
function OpenApiDownloadButton() { | ||
return ( | ||
<div> | ||
<DownloadButton | ||
fileUrl={createRESTURL('static/openapi.json', { | ||
app, | ||
owner: 'nobody', | ||
})} | ||
fileNameAfterDownload="openapi.json" | ||
> | ||
<Icon /> | ||
<span>OpenAPI.json</span> | ||
</DownloadButton> | ||
</div> | ||
); | ||
} | ||
|
||
export default OpenApiDownloadButton; |
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