-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui-editor): export form definition (#13390)
- Loading branch information
Showing
28 changed files
with
860 additions
and
5 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
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
15 changes: 15 additions & 0 deletions
15
.../studio-components/src/components/StudioBlobDownloader/StudioBlobDownloader.mdx
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,15 @@ | ||
import { Canvas, Meta } from '@storybook/blocks'; | ||
import { Heading, Paragraph } from '@digdir/designsystemet-react'; | ||
import * as StudioBlobDownloaderStories from './StudioBlobDownloader.stories'; | ||
|
||
<Meta of={StudioBlobDownloaderStories} /> | ||
|
||
<Heading level={1} size='small'> | ||
StudioBlobDownloader | ||
</Heading> | ||
<Paragraph> | ||
StudioBlowDownloader is a link that triggers the download of the specified data in the specified | ||
file format. | ||
</Paragraph> | ||
|
||
<Canvas of={StudioBlobDownloaderStories.Preview} /> |
19 changes: 19 additions & 0 deletions
19
...bs/studio-components/src/components/StudioBlobDownloader/StudioBlobDownloader.stories.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,19 @@ | ||
import React from 'react'; | ||
import type { Meta, StoryFn } from '@storybook/react'; | ||
import { StudioBlobDownloader } from './StudioBlobDownloader'; | ||
|
||
type Story = StoryFn<typeof StudioBlobDownloader>; | ||
|
||
const meta: Meta = { | ||
title: 'Studio/StudioBlobDownloader', | ||
component: StudioBlobDownloader, | ||
}; | ||
export default meta; | ||
|
||
export const Preview: Story = (args): React.ReactElement => <StudioBlobDownloader {...args} />; | ||
Preview.args = { | ||
data: JSON.stringify({ test: 'test' }), | ||
fileName: 'testtest.json', | ||
fileType: 'application/json', | ||
linkText: 'Download JSON', | ||
}; |
53 changes: 53 additions & 0 deletions
53
.../libs/studio-components/src/components/StudioBlobDownloader/StudioBlobDownloader.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,53 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { StudioBlobDownloader } from './StudioBlobDownloader'; | ||
import type { StudioBlobDownloaderProps } from './StudioBlobDownloader'; | ||
import { BlobDownloader } from '@studio/pure-functions'; | ||
|
||
describe('StudioBlobDownloader', () => { | ||
type ExampleData = { | ||
testField1: string; | ||
testField2: number; | ||
}; | ||
|
||
const mockData: ExampleData = { | ||
testField1: 'test', | ||
testField2: 1, | ||
}; | ||
|
||
const handleDownloadClickMock = jest.fn(); | ||
|
||
beforeAll(() => { | ||
jest | ||
.spyOn(BlobDownloader.prototype, 'handleDownloadClick') | ||
.mockImplementation(handleDownloadClickMock); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should render', () => { | ||
renderStudioBlobDownloader({ data: JSON.stringify(mockData) }); | ||
expect(screen.getByRole('button', { name: 'Download' })).toBeInTheDocument(); | ||
}); | ||
|
||
it('should create a link to Blob with the correct data', async () => { | ||
jest.spyOn(console, 'error').mockImplementationOnce(() => {}); // Suppress expected jsdom error message from attempted download/navigate | ||
renderStudioBlobDownloader({ data: JSON.stringify(mockData) }); | ||
const user = userEvent.setup(); | ||
const downloadButton = screen.getByRole('button', { name: 'Download' }); | ||
await user.click(downloadButton); | ||
expect(handleDownloadClickMock).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
const renderStudioBlobDownloader = (props: Partial<StudioBlobDownloaderProps>) => { | ||
const defaultProps: StudioBlobDownloaderProps = { | ||
data: '{}', | ||
fileName: 'test.json', | ||
linkText: 'Download', | ||
}; | ||
render(<StudioBlobDownloader {...defaultProps} {...props} />); | ||
}; |
33 changes: 33 additions & 0 deletions
33
frontend/libs/studio-components/src/components/StudioBlobDownloader/StudioBlobDownloader.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,33 @@ | ||
import React, { useMemo } from 'react'; | ||
import { BlobDownloader } from '@studio/pure-functions'; | ||
import { StudioButton } from '../StudioButton'; | ||
import type { StudioButtonProps } from '../StudioButton'; | ||
|
||
export type StudioBlobDownloaderProps = { | ||
data: string; | ||
fileName: string; | ||
fileType?: string; | ||
linkText: string; | ||
} & StudioButtonProps; | ||
|
||
export const StudioBlobDownloader = ({ | ||
data, | ||
fileName, | ||
fileType = 'application/json', | ||
linkText, | ||
...rest | ||
}: StudioBlobDownloaderProps) => { | ||
const blobDownloader = useMemo( | ||
() => new BlobDownloader(data, fileType, fileName), | ||
[data, fileType, fileName], | ||
); | ||
const handleExportClick = () => { | ||
blobDownloader.handleDownloadClick(); | ||
}; | ||
|
||
return ( | ||
<StudioButton {...rest} onClick={handleExportClick} variant='tertiary'> | ||
{linkText} | ||
</StudioButton> | ||
); | ||
}; |
2 changes: 2 additions & 0 deletions
2
frontend/libs/studio-components/src/components/StudioBlobDownloader/index.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,2 @@ | ||
export { StudioBlobDownloader } from './StudioBlobDownloader'; | ||
export type { StudioBlobDownloaderProps } from './StudioBlobDownloader'; |
2 changes: 1 addition & 1 deletion
2
...ssion/SubExpressionValueSelector/SubExpressionValueContentInput/GatewayActionSelector.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
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
47 changes: 47 additions & 0 deletions
47
frontend/libs/studio-pure-functions/src/BlobDownloader/BlobDownloader.test.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,47 @@ | ||
import { BlobDownloader } from './BlobDownloader'; | ||
|
||
describe('BlobDownloader', () => { | ||
const data = { test: 'test' }; | ||
|
||
beforeEach(() => { | ||
global.URL.createObjectURL = jest.fn(); | ||
global.URL.revokeObjectURL = jest.fn(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should generate a download url', () => { | ||
const blobDownloader = new BlobDownloader(JSON.stringify(data)); | ||
blobDownloader.getDownloadURL(); | ||
const testBlob = new Blob([JSON.stringify(data)], { type: 'application/json' }); | ||
expect(global.URL.createObjectURL).toHaveBeenCalledWith(testBlob); | ||
}); | ||
|
||
it('should revoke a download url', () => { | ||
const blobDownloader = new BlobDownloader(JSON.stringify(data)); | ||
const downloadUrl = blobDownloader.getDownloadURL(); | ||
blobDownloader.revokeDownloadURL(downloadUrl); | ||
expect(global.URL.revokeObjectURL).toHaveBeenCalledWith(downloadUrl); | ||
}); | ||
|
||
it('should generate a download url with a custom file type', () => { | ||
const fileType = 'application/pdf'; | ||
const blobDownloaderWithFileType = new BlobDownloader(JSON.stringify(data), fileType); | ||
blobDownloaderWithFileType.getDownloadURL(); | ||
const testBlob = new Blob([JSON.stringify(data)], { type: fileType }); | ||
expect(global.URL.createObjectURL).toHaveBeenCalledWith(testBlob); | ||
}); | ||
|
||
it('should handle clicking the download link', () => { | ||
const blobDownloader = new BlobDownloader(JSON.stringify(data)); | ||
const mockGetDownloadURL = jest.fn(); | ||
const mockGetRevokeDownloadURL = jest.fn(); | ||
jest.spyOn(blobDownloader, 'getDownloadURL').mockImplementation(mockGetDownloadURL); | ||
jest.spyOn(blobDownloader, 'revokeDownloadURL').mockImplementation(mockGetRevokeDownloadURL); | ||
blobDownloader.handleDownloadClick(); | ||
expect(mockGetDownloadURL).toHaveBeenCalled(); | ||
expect(mockGetRevokeDownloadURL).toHaveBeenCalled(); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
frontend/libs/studio-pure-functions/src/BlobDownloader/BlobDownloader.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,36 @@ | ||
export class BlobDownloader { | ||
private readonly data: string; | ||
private readonly fileType: string; | ||
private readonly fileName: string; | ||
|
||
constructor(data: string, fileType = 'application/json', fileName = 'data.json') { | ||
this.data = data; | ||
this.fileType = fileType; | ||
this.fileName = fileName; | ||
} | ||
|
||
public getDownloadURL(): string { | ||
const blob = this.generateBlobToDownlaod(); | ||
return this.generateDownloadUrl(blob); | ||
} | ||
|
||
public revokeDownloadURL(url: string): void { | ||
return URL.revokeObjectURL(url); | ||
} | ||
|
||
public handleDownloadClick(): void { | ||
const link = document.createElement('a'); | ||
link.href = this.getDownloadURL(); | ||
link.download = this.fileName; | ||
link.click(); | ||
this.revokeDownloadURL(link.href); | ||
} | ||
|
||
private generateBlobToDownlaod(): Blob { | ||
return new Blob([this.data], { type: this.fileType }); | ||
} | ||
|
||
private generateDownloadUrl(blob: Blob): string { | ||
return URL.createObjectURL(blob); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
frontend/libs/studio-pure-functions/src/BlobDownloader/index.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 @@ | ||
export { BlobDownloader } from './BlobDownloader'; |
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
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
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
Oops, something went wrong.