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] Add withDataProvider HOC and Query/Mutation components to ease custom queries #2899

Merged
merged 20 commits into from
Feb 28, 2019

Conversation

ThieryMichel
Copy link
Contributor

@ThieryMichel ThieryMichel commented Feb 18, 2019

Problem

Calling the API directly is a bit cumbersome, especially when handling side effects or chaining calls. We need a better way to do that.

Solution

  • Add withDataProvider() HOC
  • Document withDataProvider() HOC
  • Add <Query> component
  • Document <Query> component
  • Add <Mutation> component
  • Document <Mutation> component
  • Use those in the demo example
  • Test all that

Note: Of course we'll use a hook for that once we make the move to React 16.8.

Sample Syntax

withDataProvider

import { withDataProvider, showNotification } from 'react-admin';

class PostList extends Component {
    state = {
        posts: [],
    }

    componentDidMount() {
        const { dataProvider, dispatch } = this.props;
        dataProvider('GET_LIST', 'posts', { filter: { status: 'pending' }})
           .then(({ data: posts }) => this.setState({ posts }))
           .catch(error => dispatch(showNotification(error.message, 'error')))
    }

    render() {
        const { posts } = this.state;
        return (
           <Fragment>
               {posts.map((post, index) => <PostDetail post={post} key={key} />)}
           </Fragment>
        );
    }
}

PostList.propTypes = {
    dataProvider: PropTypes.func.isRequired,
};

export default withDataProvider(PostList);

Query

const UserProfile = ({ record }) => (
    <Query type="GET_ONE" resource="users" payload={{ id: record.id }}>
        {({ data, loading, error }) => {
            if (loading) { return <Loading />; }
            if (error) { return <p>ERROR</p>; }
            return <div>User {data.username}</div>;
        }}
    </Query>
);

Mutation

const ApproveButton = ({ record }) => (
    <Mutation
        type="UPDATE"
        resource="comments"
        payload={{ id: record.id, data: { isApproved: true } }}
    >
        {(approve) => (
            <FlatButton label="Approve" onClick={approve} />
        )}
    </Mutation>
);

Closes #2645

@djhi djhi changed the title [WIP]Dashboard custom action [WIP] Dashboard custom action Feb 18, 2019
@fzaninotto fzaninotto changed the base branch from master to next February 18, 2019 15:54
@fzaninotto fzaninotto changed the title [WIP] Dashboard custom action [WIP] Add withDataProvider HOC and Query/Mutation components to ease custom queries Feb 18, 2019
@fzaninotto fzaninotto added this to the 2.8.0 milestone Feb 18, 2019
@fzaninotto fzaninotto changed the title [WIP] Add withDataProvider HOC and Query/Mutation components to ease custom queries [RFR] Add withDataProvider HOC and Query/Mutation components to ease custom queries Feb 22, 2019
@fzaninotto
Copy link
Member

withDataProvider isn't tested directly, but across <Query> and <Mutation>. This should be enough. Switching to RFR.

Copy link
Collaborator

@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.

Awesome! And the documentation is really good too 👍

docs/Actions.md Outdated Show resolved Hide resolved
docs/Actions.md Show resolved Hide resolved
docs/Actions.md Show resolved Hide resolved
docs/Actions.md Outdated Show resolved Hide resolved
docs/Actions.md Show resolved Hide resolved
docs/Actions.md Show resolved Hide resolved
docs/Actions.md Outdated Show resolved Hide resolved
packages/ra-core/src/sideEffect/callback.ts Show resolved Hide resolved
packages/ra-core/src/util/Mutation.tsx Show resolved Hide resolved
packages/ra-core/src/util/Query.tsx Show resolved Hide resolved
Copy link
Contributor

@Kmaschta Kmaschta left a comment

Choose a reason for hiding this comment

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

A big step for React Admin users 🎉

The file Actions.md should be renamed to Querying.md.
That said, all the links to the Actions will be broken. (which is already the case, because you renamed all the titles)

IMO, it would be nice the keep the file Actions.md with the current titles but with a big red warning at the top to notice users that the documentation has been updated.
This Actions.md should not be linked to the menu, but still should be available.

docs/Actions.md Outdated Show resolved Hide resolved
docs/Actions.md Outdated Show resolved Hide resolved
docs/Actions.md Outdated Show resolved Hide resolved
docs/Actions.md Outdated Show resolved Hide resolved
docs/Actions.md Outdated Show resolved Hide resolved
docs/Actions.md Outdated Show resolved Hide resolved
@fzaninotto
Copy link
Member

I'm not fan of writing a redirection page in Markdown - and I 'm not even sure it's possible. Let's keep the file name (and the URL) as it is, as the menu label has been properly changed.

@Kmaschta Kmaschta merged commit aab31fe into next Feb 28, 2019
@Kmaschta Kmaschta deleted the dashboard-custom-action branch February 28, 2019 13:37
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.

4 participants