Skip to content

Commit

Permalink
test using custom save with submitError
Browse files Browse the repository at this point in the history
  • Loading branch information
bmihelac committed May 20, 2020
1 parent 21e88e6 commit 1a9f02b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
16 changes: 14 additions & 2 deletions examples/simple/src/comments/CommentEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Title,
minLength,
} from 'react-admin'; // eslint-disable-line import/no-unresolved
import { useDataProvider } from 'ra-core';

const LinkToRelatedPost = ({ record }) => (
<Link to={`/posts/${record.post_id}`}>
Expand Down Expand Up @@ -50,7 +51,18 @@ const CommentEdit = props => {
save,
basePath,
version,
} = useEditController(props);
} = useEditController({ ...props, undoable: false });

const dataProvider = useDataProvider();

async function mySave(values) {
const results = await dataProvider.update(resource, values);
if (values.body.startsWith('invalid')) {
return {
body: 'dont like it',
};
}
}
return (
<div className="edit-page">
<Title defaultTitle={`Comment #${record ? record.id : ''}`} />
Expand All @@ -70,7 +82,7 @@ const CommentEdit = props => {
redirect={redirect}
resource={resource}
record={record}
save={save}
save={mySave}
version={version}
>
<TextInput disabled source="id" fullWidth />
Expand Down
3 changes: 2 additions & 1 deletion packages/ra-core/src/form/FormWithRedirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ const FormWithRedirect: FC<FormWithRedirectOwnProps & FormProps> = ({
: redirect.current;
const finalValues = sanitizeEmptyValues(finalInitialValues, values);

onSave.current(finalValues, finalRedirect);
const result = onSave.current(finalValues, finalRedirect);
return result;
};

return (
Expand Down

1 comment on commit 1a9f02b

@bojangles-m
Copy link

@bojangles-m bojangles-m commented on 1a9f02b Dec 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HI, i tried your solution and you don't need to create whole new view unless you want to, it is better the whole design stays as it is created by react-admin.
It is also possible to pass customerSave function as prop and i is even better.
This is example for your CommentEdit.js

import {
  EditView,
  useEditController,
  EditContextProvider,
  useRedirect,
} from 'react-admin';
import { useDataProvider } from 'ra-core';

const customerSave = (resource, dataProvider, redirect) => {
  return async (values) => {
    try {
      await dataProvider.update(resource, {
        id: values.id,
        data: values,
      });
    } catch (error) {
      return {
        // name: { message: 'ra.validation.email' },
        name: 'dont like it',
        contactEmail: 'no ordinary mail',
      };
    }

    redirect('/customers');
  };
};

export const MyEdit = (props) => {
  const controllerProps = useEditController(props);
  const redirect = useRedirect();
  const { resource } = controllerProps;
  const dataProvider = useDataProvider();
  const save = useSave(resource, dataProvider, redirect);

  // create your own custom save function
  controllerProps.save = save;

  return (
    <EditContextProvider value={controllerProps}>
      <EditView {...props} {...controllerProps} />
    </EditContextProvider>
  );
};

Please sign in to comment.