-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Use bulk export #10908
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
Merged
Merged
Use bulk export #10908
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,54 @@ | ||
| import * as React from 'react'; | ||
| import { fireEvent, render, screen, waitFor } from '@testing-library/react'; | ||
| import { Basic, HookLevelExporter } from './useBulkExport.stories'; | ||
|
|
||
| describe('useBulkExport', () => { | ||
| it('should export selected records using the exporter from the list context', async () => { | ||
| let exportedData: any[]; | ||
| let exportedResource: string; | ||
| const exporter = jest.fn( | ||
| (data, fetchRelatedRecords, dataProvider, resource) => { | ||
| exportedData = data; | ||
| exportedResource = resource; | ||
| } | ||
| ); | ||
| render(<Basic exporter={exporter} />); | ||
| fireEvent.click(await screen.findByText('War and Peace')); | ||
| fireEvent.click(await screen.findByText('The Lord of the Rings')); | ||
| fireEvent.click(await screen.findByText('Export')); | ||
| await waitFor(() => expect(exporter).toHaveBeenCalled()); | ||
| expect(exportedData!).toEqual([ | ||
| { id: 1, title: 'War and Peace' }, | ||
| { id: 5, title: 'The Lord of the Rings' }, | ||
| ]); | ||
| expect(exportedResource!).toEqual('books'); | ||
| }); | ||
| it('should export selected records using the exporter from the hook options', async () => { | ||
| const exporter = jest.fn(); | ||
| let exportedData: any[]; | ||
| let exportedResource: string; | ||
| const hookExporter = jest.fn( | ||
| (data, fetchRelatedRecords, dataProvider, resource) => { | ||
| exportedData = data; | ||
| exportedResource = resource; | ||
| } | ||
| ); | ||
|
|
||
| render( | ||
| <HookLevelExporter | ||
| exporter={exporter} | ||
| hookExporter={hookExporter} | ||
| /> | ||
| ); | ||
| fireEvent.click(await screen.findByText('War and Peace')); | ||
| fireEvent.click(await screen.findByText('The Lord of the Rings')); | ||
| fireEvent.click(await screen.findByText('Export')); | ||
| await waitFor(() => expect(hookExporter).toHaveBeenCalled()); | ||
| expect(exportedData!).toEqual([ | ||
| { id: 1, title: 'War and Peace' }, | ||
| { id: 5, title: 'The Lord of the Rings' }, | ||
| ]); | ||
| expect(exportedResource!).toEqual('books'); | ||
| expect(exporter).toHaveBeenCalledTimes(0); | ||
| }); | ||
| }); |
This file contains hidden or 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,127 @@ | ||
| import * as React from 'react'; | ||
| import fakeRestProvider from 'ra-data-fakerest'; | ||
| import { | ||
| CoreAdminContext, | ||
| Exporter, | ||
| ListBase, | ||
| useBulkExport, | ||
| UseBulkExportOptions, | ||
| useListContext, | ||
| } from '..'; | ||
|
|
||
| export default { | ||
| title: 'ra-core/export/useBulkExport', | ||
| }; | ||
|
|
||
| const data = { | ||
| books: [ | ||
| { id: 1, title: 'War and Peace' }, | ||
| { id: 2, title: 'The Little Prince' }, | ||
| { id: 3, title: "Swann's Way" }, | ||
| { id: 4, title: 'A Tale of Two Cities' }, | ||
| { id: 5, title: 'The Lord of the Rings' }, | ||
| { id: 6, title: 'And Then There Were None' }, | ||
| { id: 7, title: 'Dream of the Red Chamber' }, | ||
| { id: 8, title: 'The Hobbit' }, | ||
| { id: 9, title: 'She: A History of Adventure' }, | ||
| { id: 10, title: 'The Lion, the Witch and the Wardrobe' }, | ||
| { id: 11, title: 'The Chronicles of Narnia' }, | ||
| { id: 12, title: 'Pride and Prejudice' }, | ||
| { id: 13, title: 'Ulysses' }, | ||
| { id: 14, title: 'The Catcher in the Rye' }, | ||
| { id: 15, title: 'The Little Mermaid' }, | ||
| { id: 16, title: 'The Secret Garden' }, | ||
| { id: 17, title: 'The Wind in the Willows' }, | ||
| { id: 18, title: 'The Wizard of Oz' }, | ||
| { id: 19, title: 'Madam Bovary' }, | ||
| { id: 20, title: 'The Little House' }, | ||
| { id: 21, title: 'The Phantom of the Opera' }, | ||
| { id: 22, title: 'The Adventures of Tom Sawyer' }, | ||
| { id: 23, title: 'The Adventures of Huckleberry Finn' }, | ||
| { id: 24, title: 'The Time Machine' }, | ||
| { id: 25, title: 'The War of the Worlds' }, | ||
| ], | ||
| }; | ||
|
|
||
| const dataProvider = fakeRestProvider( | ||
| data, | ||
| process.env.NODE_ENV !== 'test', | ||
| 300 | ||
| ); | ||
|
|
||
| export const Basic = ({ | ||
| exporter = (data, fetchRelatedRecords, dataProvider, resource) => { | ||
| alert( | ||
| `Exporting ${data.length} items (${data.map(record => record.id).join(', ')}) from ${resource}` | ||
| ); | ||
| }, | ||
| }: { | ||
| exporter?: Exporter; | ||
| }) => ( | ||
| <CoreAdminContext dataProvider={dataProvider}> | ||
| <ListBase resource="books" perPage={5} exporter={exporter}> | ||
| <ListView /> | ||
| <BulkExportButton /> | ||
| </ListBase> | ||
| </CoreAdminContext> | ||
| ); | ||
|
|
||
| const ListView = () => { | ||
| const { data, error, isPending, selectedIds, onToggleItem } = | ||
| useListContext(); | ||
|
|
||
| if (isPending) { | ||
| return <div>Loading...</div>; | ||
| } | ||
| if (error) { | ||
| return <div>Error...</div>; | ||
| } | ||
|
|
||
| return ( | ||
| <div> | ||
| <ul> | ||
| {data.map((record: any) => ( | ||
| <li key={record.id}> | ||
| <label> | ||
| <input | ||
| type="checkbox" | ||
| style={{ marginRight: '8px' }} | ||
| checked={selectedIds.includes(record.id)} | ||
| onChange={() => onToggleItem(record.id)} | ||
| /> | ||
| {record.title} | ||
| </label> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export const HookLevelExporter = ({ | ||
| exporter = (data, fetchRelatedRecords, dataProvider, resource) => { | ||
| alert( | ||
| `Exporting ${data.length} items (${data.map(record => record.id).join(', ')}) from ${resource} at the list level` | ||
| ); | ||
| }, | ||
| hookExporter = (data, fetchRelatedRecords, dataProvider, resource) => { | ||
| alert( | ||
| `Exporting ${data.length} items (${data.map(record => record.id).join(', ')}) from ${resource} at the hook level` | ||
| ); | ||
| }, | ||
| }: { | ||
| exporter?: Exporter; | ||
| hookExporter?: Exporter; | ||
| }) => ( | ||
| <CoreAdminContext dataProvider={dataProvider}> | ||
| <ListBase resource="books" perPage={5} exporter={exporter}> | ||
| <ListView /> | ||
| <BulkExportButton exporter={hookExporter} /> | ||
| </ListBase> | ||
| </CoreAdminContext> | ||
| ); | ||
|
|
||
| const BulkExportButton = ({ exporter }: UseBulkExportOptions) => { | ||
| const bulkExport = useBulkExport({ exporter }); | ||
| return <button onClick={bulkExport}>Export</button>; | ||
| }; |
This file contains hidden or 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,52 @@ | ||
| import { useCallback } from 'react'; | ||
| import { Exporter, RaRecord } from '../types'; | ||
| import { useResourceContext } from '../core/useResourceContext'; | ||
| import { useListContext } from '../controller/list/useListContext'; | ||
| import { useDataProvider } from '../dataProvider/useDataProvider'; | ||
| import { useNotify } from '../notification/useNotify'; | ||
| import { fetchRelatedRecords } from './fetchRelatedRecords'; | ||
|
|
||
| /** | ||
| * A hook that provides a callback to export the selected records from the nearest ListContext and call the exporter function for them. | ||
| */ | ||
| export function useBulkExport<RecordType extends RaRecord = any>( | ||
| options: UseBulkExportOptions<RecordType> = {} | ||
| ): UseBulkExportResult { | ||
| const { exporter: customExporter, meta } = options; | ||
|
|
||
| const resource = useResourceContext(options); | ||
| const { exporter: exporterFromContext, selectedIds } = | ||
| useListContext<RecordType>(); | ||
| const exporter = customExporter || exporterFromContext; | ||
| const dataProvider = useDataProvider(); | ||
| const notify = useNotify(); | ||
|
|
||
| return useCallback(() => { | ||
| if (exporter && resource) { | ||
| dataProvider | ||
| .getMany(resource, { ids: selectedIds, meta }) | ||
| .then(({ data }) => | ||
| exporter( | ||
| data, | ||
| fetchRelatedRecords(dataProvider), | ||
| dataProvider, | ||
| resource | ||
| ) | ||
| ) | ||
| .catch(error => { | ||
| console.error(error); | ||
| notify('ra.notification.http_error', { | ||
| type: 'error', | ||
| }); | ||
| }); | ||
| } | ||
| }, [dataProvider, exporter, notify, resource, selectedIds, meta]); | ||
| } | ||
|
|
||
| export type UseBulkExportResult = () => void; | ||
|
|
||
| export interface UseBulkExportOptions<RecordType extends RaRecord = any> { | ||
| exporter?: Exporter<RecordType>; | ||
| meta?: any; | ||
| resource?: string; | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.