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

Mutation forces refreshing even provided with options #3125

Closed
shawnchen1980 opened this issue Apr 13, 2019 · 2 comments
Closed

Mutation forces refreshing even provided with options #3125

shawnchen1980 opened this issue Apr 13, 2019 · 2 comments

Comments

@shawnchen1980
Copy link

What you were expecting:

The doc says Mutation accepts an options prop to customize the side effect processing by the CUSTOM_FETCH action. I found it's partially true. The onSuccess part is not read into the action I think.

``
What happened instead:

If I click the button, I will catch a CUSTOM_FETCH. It has a meta part which always contains onSuccess:{refresh:true} no matter how I config the options prop for the Mutation component. However, the onError part is correct read from the given prop. Weird!
Steps to reproduce:

I used the dataprovider from the tutorial. Just a fake user list.
Related code:

const options = {
  undoable: true,
  onSuccess: {
    notification: "this line is not read into CUSTOM_FETCH",
    refresh: false,
    redirect: "/users"
  },
  onError: {
    notification: { body: "Error: BUT THIS LINE IS READ into CUSTOM_FETCH", level: "warning" }
  }
};
class MyButton extends Component {
  
  render() {
    const { record } = this.props;
    const payload = record && {
      id: record.id,
      data: { username: "heha" }
    };

    return (
      <Mutation
        type="UPDATE"
        resource="users"
        payload={payload}
        options={options}
      >
        {approve => <Button onClick={approve}>hello</Button>}
      </Mutation>
    );
  }
}

const UserList = props => (
  <List {...props}>
    <Datagrid rowClick="edit">
      <TextField source="id" />
      <TextField source="name" />
      <MyButton />
    </Datagrid>
  </List>
);
storiesOf("test", module)
   .add(
    "with react-admin",

    () => (
      <Admin dataProvider={dataProvider}>
        <Resource name="users" list={UserList} />
      </Admin>
    )
  );

Other information:

Environment

  • React-admin version:
  • Last version that did not exhibit the issue (if applicable):
  • React version:
  • Browser:
  • Stack trace (in case of a JS error):
@kayneth
Copy link

kayneth commented Apr 15, 2019

Hi !
Maybe I can help a little for this.
For the part of the refresh, it comes from the Undoable side effect. Where it does the following:

  • // dispatch action in optimistic mode (no fetch), with success side effects
  • // if not cancelled, redispatch the action, this time immediate, and without success side effect
  • refresh: true

Then for the notification part, I can see a little error on the documentation page as the format for a notification side effect is the following:

export interface NotificationSideEffect {
    body: string;
    level: NotificationType;
    messageArgs?: object;
}

As seen here.

Maybe we could add a tiny hint to users so they could find where all the magic happens 😄

I wish i was not wrong and I was pleased to help you!

@fzaninotto
Copy link
Member

@kayneth is right twice:

  1. the refresh is normal and caused by the undoable side effect. Use non-undoable actions to control the refresh
  2. The documentation is wrong about the notification syntax, I'll fix that in another PR.

I'm closing this one as it's not a react-admin bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants