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] Declare fetch side effects as function #3425

Merged
merged 5 commits into from
Jul 22, 2019
Merged

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Jul 19, 2019

This is totally backwards compatible - delerative side effect objects are still interpreted by the Redux saga.

  • Add useNotify, useRedirect, useRefresh, and useUnselectAll hooks
  • Update useDataProvider to execute onSuccess and onFailure functions in the context of a hook
  • Update List, Show, Edit and Create controllers to use hook side effects instead of object side effects (allows for custom side effects)
  • Update DeleteButton to use hook side effects instead of object side effects
  • Update documentation

image

Closes #3413.

This change does not reduce the code in controllers, but makes it more understandable. There are less moving parts, it's more idiomatic to modern React, and it bypasses some of the redux-saga logic.

It allows to customize side effects quite easily, e.g. to display a custom notification message in case of failed HTTP request.

const MyEdit = props => {
   const editControllerProps = useEditController(props);
   const [update, { loading: isSaving }] = useUpdate(
        props.resource,
        props.id,
        {}, // set by the caller
        editControllerProps.record
    );

    const save = useCallback(
        (data: Partial<Record>, redirectTo = 'list') =>
            update(
                null,
                { data },
                {
                    onSuccess: () => {
                        notify(/** custom notification */);
                        redirect(/** custom redirection */);
                    },
                    onFailure: error => {
                        notify(/** custom notification */);
                        redirect(/** custom redirection */);
                    },
                    undoable,
                }
            ),
        [basePath, notify, redirect, undoable, update]
    );
    return <ListView {...props} {...controllerProps} save={save} />
}

This also opens the path for a pure hook-based fetch side effect handling (not using the fetch saga anymore).

@fzaninotto fzaninotto added this to the 3.0.0 milestone Jul 19, 2019
@fzaninotto fzaninotto changed the title [WIP] Declare fetch side effects as function [RFR] Declare fetch side effects as function Jul 19, 2019
@fzaninotto
Copy link
Member Author

Switching to RFR

Copy link
Contributor

@djhi djhi left a comment

Choose a reason for hiding this comment

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

Shouldn't we update the upgrade guide too ?

@@ -35,7 +35,7 @@ const UserEditToolbar = props => {
return (
<Toolbar {...props} classes={classes}>
<SaveButton />
<DeleteWithConfirmButton />
<DeleteWithConfirmButton {...props} />
Copy link
Contributor

Choose a reason for hiding this comment

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

We pass props to both the toolbar and the button ?

import { Identifier } from '../types';

/**
* Get a callback to call the dataProvider with a DELETE verb, the result and the loading state.
Copy link
Contributor

Choose a reason for hiding this comment

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

the result ?

docs/Actions.md Outdated
+ }
);
return <FlatButton label="Approve" onClick={approve} disabled={loading} />;
};
```

React-admin can handle the following side effects:
The `onSuccess` function is called with the response from the `dataProvider` as argument. The `onError` function is called wit hthe error returned by the `dataProvider`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
The `onSuccess` function is called with the response from the `dataProvider` as argument. The `onError` function is called wit hthe error returned by the `dataProvider`.
The `onSuccess` function is called with the response from the `dataProvider` as argument. The `onError` function is called with the error returned by the `dataProvider`.

@fzaninotto
Copy link
Member Author

I don't think we need to add anything to the upgrade guide, as this adds only backward compatible changes. Any application running before that PR will continue to run after that PR.

@djhi djhi merged commit c4548c0 into next Jul 22, 2019
@djhi djhi deleted the onSuccess-onFailure-functions branch July 22, 2019 21:18
@fzaninotto fzaninotto mentioned this pull request Sep 13, 2019
40 tasks
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

Successfully merging this pull request may close these issues.

None yet

2 participants