React-admin: How to Pass more parameters to a dataProvider for type GET_LIST
Asked Answered
P

2

5

I want to pass one custom parameter to the Admin's dataProvider for type 'GET_LIST'.

I have something like this in App.js:

<Admin dataProvider={dataProvider}> 
    <Resource name="posts" list={PostList} myCustomAttr={"10"} /> 
    <Resource name="users" list={UserList} myCustomAttr={"15"} /> 
</Admin>

And 'dataProvider' is a custom dataProvider and I want to have the 'myCustomAttr' when it gets called.

So my custom dataProvider could look like this:

export default (type, resource, params) => {

    if (type == 'GET_LIST') {
       if (params.myCustomAttr == '10') {
           //Do something
       }
    }
}
Prosthodontist answered 16/4, 2019 at 16:53 Comment(0)
P
6

Ok, I've solved this by using the List filter prop:

<Admin dataProvider={dataProvider}> 
    <Resource name="posts" list={PostList} options={{ myCustomAttr: "10" }} /> 
    <Resource name="users" list={UserList} options={{ myCustomAttr: "15" }} /> 
</Admin>

And in the UserList (like) component:

export const UserList = function(props) {
    return <List {...props} filter={{myCustomAttr:props.options.myCustomAttr}} >
        //...
    </List>
};

So, this way, I get what I want in the dataProvider.

export default (type, resource, params) => {

    if (type == 'GET_LIST') {
       if (params.filter.myCustomAttr == '10') {
           //Do something
       }
    }
}
Prosthodontist answered 17/4, 2019 at 15:26 Comment(0)
K
1

You can decorate your Data Provider.

Basically, you intercept the data provider and add your own behavior depending on type and resource:

if (type === 'GET_LIST') {
    params = {...params, myCustomParameter: 10};
}

return requestHandler(type, resource, params);
Klaus answered 16/4, 2019 at 17:6 Comment(6)
Thank you for your answer. The problem is that i need an additional parameter besides the resource. In the case of a decorator like the one in the docs, is for type 'UPDATE' and you have the data of the fields in 'params' parameter to see what to do. But in the case of 'GET_LIST', you don't have any identifiable attribute in params, only sort and pagination. I need to have access to a numeric prop set to a Resource on fetching data and if i use the 'resource' parameter for that, the title of the Admin will show a number instead of a name like 'Users'.Prosthodontist
I edited my answer. Wouldn't that be what you need? You add your custom parameter to the params, and then continue calling the original dataProvider.Klaus
Sure. But where would i do that? The thing is that the '10' value (in your example) belongs to the Resource component, once the dataProvider gets called, i need to be able to pass it some how.Prosthodontist
I just need a way to pass a parameter from the Resource components to the dataProvider on GET_LIST to instruct the server how to return the json to list.Prosthodontist
It's not clear to me what you mean by "the value belongs to the Resource component". In what way does it belong to the resource and where is this value coming from?Klaus
I've edited the original post to be a little more clear (I hope).Prosthodontist

© 2022 - 2024 — McMap. All rights reserved.