-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Fix<DeleteButton mutationOptions>
ignores meta
parameter
#8023
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
364785a
Delete contexts don't send mutation meta
antoinefricker ae7aa5e
Fix access error
antoinefricker 94a5678
Add meta support for bulk delete buttons
antoinefricker 603ccdc
Update doc
antoinefricker e55f8de
Comment from PR
antoinefricker 5bd24ce
Add unit tests
antoinefricker 35a81b9
Unit test corrections
antoinefricker 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 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 |
---|---|---|
|
@@ -244,11 +244,11 @@ export const PostList = () => ( | |
|
||
![Bulk Delete button](./img/bulk-delete-button.png) | ||
|
||
| Prop | Required | Type | Default | Description | | ||
| ------------ | -------- | --------------- | ------------------ | ----------------------------------- | | ||
| `label` | Optional | `string` | 'ra.action.delete' | label or translation message to use | | ||
| `icon` | Optional | `ReactElement` | `<DeleteIcon>` | iconElement, e.g. `<CommentIcon />` | | ||
| `exporter` | Optional | `Function` | - | Override the List exporter function | | ||
| Prop | Required | Type | Default | Description | | ||
| --------------------| -------- | --------------- | ------------------ | ---------------------------------------------------| | ||
| `label` | Optional | `string` | 'ra.action.delete' | label or translation message to use | | ||
| `icon` | Optional | `ReactElement` | `<DeleteIcon>` | iconElement, e.g. `<CommentIcon />` | | ||
| `mutationOptions` | Optional | `object` | null | options for react-query `useMutation` hook | | ||
|
||
### `<FilterButton>` | ||
|
||
|
@@ -277,6 +277,7 @@ Delete the current record after a confirm dialog has been accepted. To be used i | |
| `confirmContent` | Optional | `ReactNode` | 'ra.message.delete_content' | Message or React component to be used as the body of the confirm dialog | | ||
| `redirect` | Optional | `string | false | Function` | 'list' | Custom redirection after success side effect | | ||
| `translateOptions` | Optional | `{ id?: string, name?: string }` | {} | Custom id and name to be used in the confirm dialog's title | | ||
| `mutationOptions` | Optional | | null | options for react-query `useMutation` hook | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
|
||
{% raw %} | ||
```jsx | ||
|
48 changes: 48 additions & 0 deletions
48
packages/ra-core/src/controller/button/useDeleteWithConfirmController.spec.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,48 @@ | ||
import React from 'react'; | ||
import expect from 'expect'; | ||
import { MemoryRouter, Route, Routes } from 'react-router'; | ||
import { fireEvent, screen, render, waitFor } from '@testing-library/react'; | ||
|
||
import { testDataProvider } from '../../dataProvider'; | ||
import { CoreAdminContext } from '../../core'; | ||
import useDeleteWithConfirmController, { | ||
UseDeleteWithConfirmControllerParams, | ||
} from './useDeleteWithConfirmController'; | ||
|
||
describe('useDeleteWithConfirmController', () => { | ||
it('should call the dataProvider.delete() function with the meta param', async () => { | ||
let receivedMeta = null; | ||
const dataProvider = testDataProvider({ | ||
delete: jest.fn((ressource, params) => { | ||
receivedMeta = params?.meta?.key; | ||
return Promise.resolve({ data: params?.meta?.key }); | ||
}), | ||
}); | ||
|
||
const MockComponent = () => { | ||
const { handleDelete } = useDeleteWithConfirmController({ | ||
record: { id: 1 }, | ||
resource: 'posts', | ||
mutationMode: 'pessimistic', | ||
mutationOptions: { meta: { key: 'metadata' } }, | ||
} as UseDeleteWithConfirmControllerParams); | ||
return <button onClick={handleDelete}>Delete</button>; | ||
}; | ||
|
||
render( | ||
<MemoryRouter> | ||
<CoreAdminContext dataProvider={dataProvider}> | ||
<Routes> | ||
<Route path="/" element={<MockComponent />} /> | ||
</Routes> | ||
</CoreAdminContext> | ||
</MemoryRouter> | ||
); | ||
|
||
const button = await screen.findByText('Delete'); | ||
fireEvent.click(button); | ||
waitFor(() => expect(receivedMeta).toEqual('metadata'), { | ||
fzaninotto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
timeout: 1000, | ||
}); | ||
}); | ||
}); |
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
48 changes: 48 additions & 0 deletions
48
packages/ra-core/src/controller/button/useDeleteWithUndoController.spec.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,48 @@ | ||
import React from 'react'; | ||
import expect from 'expect'; | ||
import { MemoryRouter, Route, Routes } from 'react-router'; | ||
import { fireEvent, screen, render, waitFor } from '@testing-library/react'; | ||
|
||
import { testDataProvider } from '../../dataProvider'; | ||
import { CoreAdminContext } from '../../core'; | ||
import useDeleteWithUndoController, { | ||
UseDeleteWithConfirmControllerParams, | ||
} from './useDeleteWithConfirmController'; | ||
|
||
describe('useDeleteWithUndoController', () => { | ||
it('should call the dataProvider.delete() function with the meta param', async () => { | ||
let receivedMeta = null; | ||
const dataProvider = testDataProvider({ | ||
delete: jest.fn((ressource, params) => { | ||
receivedMeta = params?.meta?.key; | ||
return Promise.resolve({ data: params?.meta?.key }); | ||
}), | ||
}); | ||
|
||
const MockComponent = () => { | ||
const { handleDelete } = useDeleteWithUndoController({ | ||
record: { id: 1 }, | ||
resource: 'posts', | ||
mutationMode: 'undoable', | ||
mutationOptions: { meta: { key: 'metadata' } }, | ||
} as UseDeleteWithConfirmControllerParams); | ||
return <button onClick={handleDelete}>Delete</button>; | ||
}; | ||
|
||
render( | ||
<MemoryRouter> | ||
<CoreAdminContext dataProvider={dataProvider}> | ||
<Routes> | ||
<Route path="/" element={<MockComponent />} /> | ||
</Routes> | ||
</CoreAdminContext> | ||
</MemoryRouter> | ||
); | ||
|
||
const button = await screen.findByText('Delete'); | ||
fireEvent.click(button); | ||
waitFor(() => expect(receivedMeta).toEqual('metadata'), { | ||
timeout: 1000, | ||
}); | ||
}); | ||
}); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description isn't entirely accurate, as react-query's
options
cannot contain ameta
key. In fact, you should probably add an example explaining how to set themeta
parameter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a
meta
option inuseMutation
. It is of typeRecord<string, unknown>
when ours isany
though.