Skip to content

Commit

Permalink
Merge pull request #6912 from marmelab/fix-notifications
Browse files Browse the repository at this point in the history
Update notifications signature usage
  • Loading branch information
djhi committed Nov 29, 2021
2 parents 0d5fc19 + 0e49984 commit 10ad474
Show file tree
Hide file tree
Showing 36 changed files with 221 additions and 242 deletions.
4 changes: 2 additions & 2 deletions docs/Show.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ const PostShow = props => {
const redirect = useRedirect();

const onFailure = (error) => {
notify(`Could not load post: ${error.message}`)
notify(`Could not load post: ${error.message}`);
redirect('/posts');
refresh();
};
Expand All @@ -233,7 +233,7 @@ The default `onFailure` function is:

```jsx
(error) => {
notify('ra.notification.item_doesnt_exist', 'warning');
notify('ra.notification.item_doesnt_exist', { type: 'warning' });
redirect('list', basePath);
refresh();
}
Expand Down
2 changes: 1 addition & 1 deletion docs/Translation.md
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ export default {
By default, react-admin translates the notification messages. You can pass variables for polyglot interpolation with custom notifications. For example:
```js
notify('myroot.hello.world', 'info', { name: 'Planet Earth' });
notify('myroot.hello.world', { messageArgs: { name: 'Planet Earth' } });
```
Assuming you have the following in your custom messages:
Expand Down
2 changes: 1 addition & 1 deletion examples/crm/src/notes/Note.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const Note = ({
const [handleDelete] = useDelete(resource, note.id, note, {
mutationMode: 'undoable',
onSuccess: () => {
notify('Note deleted', undefined, undefined, true);
notify('Note deleted', { type: 'info', undoable: true });
update(
reference,
record.id,
Expand Down
16 changes: 9 additions & 7 deletions examples/demo/src/layout/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,16 @@ const Login = () => {
: typeof error === 'undefined' || !error.message
? 'ra.auth.sign_in_error'
: error.message,
'warning',
{
_:
typeof error === 'string'
? error
: error && error.message
? error.message
: undefined,
type: 'warning',
messageArgs: {
_:
typeof error === 'string'
? error
: error && error.message
? error.message
: undefined,
},
}
);
}
Expand Down
17 changes: 7 additions & 10 deletions examples/demo/src/reviews/AcceptButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@ const AcceptButton = ({ record }: { record: Review }) => {
{
undoable: true,
onSuccess: () => {
notify(
'resources.reviews.notification.approved_success',
'info',
{},
true
);
notify('resources.reviews.notification.approved_success', {
type: 'info',
undoable: true,
});
redirectTo('/reviews');
},
onFailure: () => {
notify(
'resources.reviews.notification.approved_error',
'warning'
);
notify('resources.reviews.notification.approved_error', {
type: 'warning',
});
},
}
);
Expand Down
17 changes: 7 additions & 10 deletions examples/demo/src/reviews/BulkAcceptButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,17 @@ const BulkAcceptButton = (props: BulkActionProps) => {
action: CRUD_UPDATE_MANY,
undoable: true,
onSuccess: () => {
notify(
'resources.reviews.notification.approved_success',
'info',
{},
true
);
notify('resources.reviews.notification.approved_success', {
type: 'info',
undoable: true,
});
refresh();
unselectAll();
},
onFailure: () => {
notify(
'resources.reviews.notification.approved_error',
'warning'
);
notify('resources.reviews.notification.approved_error', {
type: 'warning',
});
},
}
);
Expand Down
17 changes: 7 additions & 10 deletions examples/demo/src/reviews/BulkRejectButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,17 @@ const BulkRejectButton = (props: BulkActionProps) => {
action: CRUD_UPDATE_MANY,
undoable: true,
onSuccess: () => {
notify(
'resources.reviews.notification.approved_success',
'info',
{},
true
);
notify('resources.reviews.notification.approved_success', {
type: 'info',
undoable: true,
});
refresh();
unselectAll();
},
onFailure: () => {
notify(
'resources.reviews.notification.approved_error',
'warning'
);
notify('resources.reviews.notification.approved_error', {
type: 'warning',
});
},
}
);
Expand Down
17 changes: 7 additions & 10 deletions examples/demo/src/reviews/RejectButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@ const RejectButton = ({ record }: { record: Review }) => {
{
undoable: true,
onSuccess: () => {
notify(
'resources.reviews.notification.rejected_success',
'info',
{},
true
);
notify('resources.reviews.notification.rejected_success', {
type: 'info',
undoable: true,
});
redirectTo('/reviews');
},
onFailure: () => {
notify(
'resources.reviews.notification.rejected_error',
'warning'
);
notify('resources.reviews.notification.rejected_error', {
type: 'warning',
});
},
}
);
Expand Down
7 changes: 4 additions & 3 deletions examples/simple/src/posts/ResetViewsButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ const ResetViewsButton = ({ resource, selectedIds }) => {
{
action: CRUD_UPDATE_MANY,
onSuccess: () => {
notify('ra.notification.updated', 'info', {
smart_count: selectedIds.length,
notify('ra.notification.updated', {
type: 'info',
messageArgs: { smart_count: selectedIds.length },
});
unselectAll(resource);
},
Expand All @@ -29,7 +30,7 @@ const ResetViewsButton = ({ resource, selectedIds }) => {
typeof error === 'string'
? error
: error.message || 'ra.notification.http_error',
'warning'
{ type: 'warning' }
),
mutationMode: 'optimistic',
}
Expand Down
5 changes: 1 addition & 4 deletions packages/ra-core/src/auth/Authenticated.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ describe('<Authenticated>', () => {
expect(authProvider.logout.mock.calls[0][0]).toEqual({});
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch.mock.calls[0][0]).toEqual(
showNotification('ra.auth.auth_check_error', 'warning', {
messageArgs: {},
undoable: false,
})
showNotification('ra.auth.auth_check_error', 'warning')
);
expect(dispatch.mock.calls[1][0]).toEqual({
type: 'RA/CLEAR_STATE',
Expand Down
5 changes: 1 addition & 4 deletions packages/ra-core/src/auth/useAuthenticated.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ describe('useAuthenticated', () => {
expect(authProvider.logout.mock.calls[0][0]).toEqual({});
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch.mock.calls[0][0]).toEqual(
showNotification('ra.auth.auth_check_error', 'warning', {
messageArgs: {},
undoable: false,
})
showNotification('ra.auth.auth_check_error', 'warning')
);
expect(dispatch.mock.calls[1][0]).toEqual({
type: 'RA/CLEAR_STATE',
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/auth/useCheckAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const useCheckAuth = (): CheckAuth => {
!shouldSkipNotify &&
notify(
getErrorMessage(error, 'ra.auth.auth_check_error'),
'warning'
{ type: 'warning' }
);
}
throw error;
Expand Down
14 changes: 6 additions & 8 deletions packages/ra-core/src/auth/useLogoutIfAccessDenied.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,13 @@ const useLogoutIfAccessDenied = (): LogoutIfAccessDenied => {
.checkAuth({})
.then(() => {
if (logoutUser) {
notify(
'ra.notification.logged_out',
'warning'
);
notify('ra.notification.logged_out', {
type: 'warning',
});
} else {
notify(
'ra.notification.not_authorized',
'warning'
);
notify('ra.notification.not_authorized', {
type: 'warning',
});
}
})
.catch(() => {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ const useDeleteWithConfirmController = (
onSuccess: response => {
setOpen(false);
if (onSuccess === undefined) {
notify(
'ra.notification.deleted',
'info',
{ smart_count: 1 },
mutationMode === 'undoable'
);
notify('ra.notification.deleted', {
type: 'info',
messageArgs: { smart_count: 1 },
undoable: mutationMode === 'undoable',
});
redirect(redirectTo, basePath || `/${resource}`);
refresh();
} else {
Expand All @@ -108,14 +107,16 @@ const useDeleteWithConfirmController = (
typeof error === 'string'
? error
: error.message || 'ra.notification.http_error',
'warning',
{
_:
typeof error === 'string'
? error
: error && error.message
? error.message
: undefined,
type: 'warning',
messageArgs: {
_:
typeof error === 'string'
? error
: error && error.message
? error.message
: undefined,
},
}
);
refresh();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,11 @@ const useDeleteWithUndoController = (
onSuccess !== undefined
? onSuccess
: () => {
notify(
'ra.notification.deleted',
'info',
{ smart_count: 1 },
true
);
notify('ra.notification.deleted', {
type: 'info',
messageArgs: { smart_count: 1 },
undoable: true,
});
redirect(redirectTo, basePath || `/${resource}`);
refresh();
},
Expand All @@ -86,14 +85,16 @@ const useDeleteWithUndoController = (
typeof error === 'string'
? error
: error.message || 'ra.notification.http_error',
'warning',
{
_:
typeof error === 'string'
? error
: error && error.message
? error.message
: undefined,
type: 'warning',
messageArgs: {
_:
typeof error === 'string'
? error
: error && error.message
? error.message
: undefined,
},
}
);
refresh();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ describe('useCreateController', () => {
type: 'RA/SHOW_NOTIFICATION',
payload: {
messageArgs: { smart_count: 1 },
undoable: false,
autoHideDuration: undefined,
type: 'info',
message: 'ra.notification.created',
},
Expand Down Expand Up @@ -157,8 +155,6 @@ describe('useCreateController', () => {
type: 'RA/SHOW_NOTIFICATION',
payload: {
messageArgs: { _: 'not good' },
undoable: false,
autoHideDuration: undefined,
type: 'warning',
message: 'not good',
},
Expand Down
20 changes: 11 additions & 9 deletions packages/ra-core/src/controller/details/useCreateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ export const useCreateController = <
notify(
successMessage ||
'ra.notification.created',
'info',
{
smart_count: 1,
type: 'info',
messageArgs: { smart_count: 1 },
}
);
redirect(
Expand All @@ -187,14 +187,16 @@ export const useCreateController = <
? error
: error.message ||
'ra.notification.http_error',
'warning',
{
_:
typeof error === 'string'
? error
: error && error.message
? error.message
: undefined,
type: 'warning',
messageArgs: {
_:
typeof error === 'string'
? error
: error && error.message
? error.message
: undefined,
},
}
);
},
Expand Down
Loading

0 comments on commit 10ad474

Please sign in to comment.