Skip to content
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

[RFR] Create/Edit allow changing success message #3573

Merged
merged 2 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/CreateEdit.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Here are all the props accepted by the `<Create>` and `<Edit>` components:
* [`title`](#page-title)
* [`actions`](#actions)
* [`aside`](#aside-component)
* [`successMessage`](#success-message)
* [`undoable`](#undoable) (`<Edit>` only)

Here is the minimal code necessary to display a form to create and edit comments:
Expand Down Expand Up @@ -177,6 +178,19 @@ const Aside = ({ record }) => (

**Tip**: Always test that the `record` is defined before using it, as react-admin starts rendering the UI before the API call is over.

### Success message

Once the `dataProvider` returns successfully after save, users see a generic notification ("Element created" / "Element updated"). You can customize this message by passing a `successMessage` prop:

```jsx
const PostEdit = props => (
<Edit successMessage="messages.post_saved" {...props}>
...
</Edit>
```

**Tip**: The message will be translated.

### Undoable

By default, the Save and Delete actions are undoable, i.e. react-admin only sends the related request to the data provider after a short delay, during which the user can cancel the action. This is part of the "optimistic rendering" strategy of react-admin ; it makes the user interactions more reactive.
Expand Down
14 changes: 10 additions & 4 deletions packages/ra-core/src/controller/useCreateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface CreateProps {
match: Match;
record?: Partial<Record>;
resource: string;
successMessage?: string;
}

/**
Expand Down Expand Up @@ -67,6 +68,7 @@ const useCreateController = (props: CreateProps): CreateControllerProps => {
record = {},
hasShow,
hasEdit,
successMessage,
} = props;

const translate = useTranslate();
Expand All @@ -90,9 +92,13 @@ const useCreateController = (props: CreateProps): CreateControllerProps => {
onSuccess: onSuccess
? onSuccess
: ({ data: newRecord }) => {
notify('ra.notification.created', 'info', {
smart_count: 1,
});
notify(
successMessage || 'ra.notification.created',
'info',
{
smart_count: 1,
}
);
redirect(
redirectTo,
basePath,
Expand All @@ -113,7 +119,7 @@ const useCreateController = (props: CreateProps): CreateControllerProps => {
},
}
),
[basePath, create, notify, redirect]
[create, notify, successMessage, redirect, basePath]
);

const resourceName = translate(`resources.${resource}.name`, {
Expand Down
7 changes: 4 additions & 3 deletions packages/ra-core/src/controller/useEditController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface EditControllerProps {
basePath: string;
record?: Record;
version: number;
successMessage?: string;
}

/**
Expand All @@ -55,7 +56,7 @@ export interface EditControllerProps {
*/
const useEditController = (props: EditProps): EditControllerProps => {
useCheckMinimumRequiredProps('Edit', ['basePath', 'resource'], props);
const { basePath, id, resource, undoable = true } = props;
const { basePath, id, resource, successMessage, undoable = true } = props;
const translate = useTranslate();
const notify = useNotify();
const redirect = useRedirect();
Expand Down Expand Up @@ -95,7 +96,7 @@ const useEditController = (props: EditProps): EditControllerProps => {
{
onSuccess: () => {
notify(
'ra.notification.updated',
successMessage || 'ra.notification.updated',
'info',
{
smart_count: 1,
Expand All @@ -114,7 +115,7 @@ const useEditController = (props: EditProps): EditControllerProps => {
undoable,
}
),
[basePath, notify, redirect, undoable, update]
[basePath, notify, redirect, undoable, update, successMessage]
);

return {
Expand Down
2 changes: 2 additions & 0 deletions packages/ra-ui-materialui/src/detail/Create.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Create.propTypes = {
title: PropTypes.node,
record: PropTypes.object,
hasList: PropTypes.bool,
successMessage: PropTypes.string,
};

const useStyles = makeStyles({
Expand Down Expand Up @@ -99,6 +100,7 @@ const sanitizeRestProps = ({
options,
locale,
permissions,
successMessage,
...rest
}) => rest;

Expand Down
2 changes: 2 additions & 0 deletions packages/ra-ui-materialui/src/detail/Edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Edit.propTypes = {
id: PropTypes.any.isRequired,
resource: PropTypes.string.isRequired,
title: PropTypes.node,
successMessage: PropTypes.string,
};

export const useStyles = makeStyles({
Expand Down Expand Up @@ -101,6 +102,7 @@ const sanitizeRestProps = ({
locale,
permissions,
undoable,
successMessage,
...rest
}) => rest;

Expand Down