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

Expose Enums #2

Open
BowlingX opened this issue Sep 30, 2019 · 1 comment
Open

Expose Enums #2

BowlingX opened this issue Sep 30, 2019 · 1 comment

Comments

@BowlingX
Copy link
Owner

To make usage of enums easier in react-admin. We should provide a hook to query those.

@jonArzelus
Copy link

jonArzelus commented Sep 23, 2020

Hi, I recently have been struggling to make this work for value filtering and I found a workaround for simple string based enums. The thing is that we define all our enum types as ('aa','bb'....) all named as XXXchoices, this way we can identify them easily. With a quick check in the filters.js file we check if the normalizedName ends with choices and we treat them as they where strings but only for equalTo values as like does not make much sense here. I leave the code sample below hoping someone finds it interesting:
(in build/module/filters.js

// ...
// code omitted as nothing changed
export var mapFilterType = function (type, value, key) {
    var _a, _b, _c, _d, _e;
    var normalizedName = type.name.toLowerCase();
    // precheck for choices enums
    if (normalizedName.endsWith('choices')) {
        // returns the same as 'string' but without the '%like%' part
        return {
                or: [
                    (_b = {},
                        _b[key] = {
                            equalTo: value,
                        },
                        _b),
                ],
            }; 
    } else {
        // normal type matching
        switch (normalizedName) {
            // ...
            // code omitted as nothing changed
        }
    }
};
// ...
// code omitted as nothing changed

Sorry but I don't use too much TS, thanks for the great package BTW!

EDIT (26/11/2020): as for the 4.2.0 update of ra-postgraphile, this workaround is no longer required so I strongly advise anyone facing my same issue to update to that version. Thanks a lot!

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

2 participants