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

Best practice to reuse parametric selectors in arrays #30

Open
martinkutter opened this issue May 20, 2021 · 1 comment
Open

Best practice to reuse parametric selectors in arrays #30

martinkutter opened this issue May 20, 2021 · 1 comment

Comments

@martinkutter
Copy link

What is the best practice (if it is possible) to reuse the getCarView selector in the following example?

reduxStore = {
    defaultFront: "defaultFront.jpg",
    defaultBack: "defaultBack.jpg",
    view: "front",
    favorits: ["uid1", "uid2"],
    cars: {
        "uid1": {
            front: "front.jpg",
            back: "back.jpg",
        },
        "uid2": {},
        ...
    }
}

const getUidFromProps = createIdSelector(props => props.uid);
const getView = state => state.view;
const getCars = state => state.cars;
const getDefaultBack = state => state.defaultBack;
const getDefaultFront = state => state.defaultFront;
const getFavorits = state => state.favorits;

const getCar = createSelector(
    [getUidFromProps, getCars],
    (uid, cars) => cars[uid]
);

const getCustomCarFront = createSelector([getCar], car => car.front);
const getCustomCarBack = createSelector([getCar], car => car.back);

const getCustomCarView = createSelector(
    [getCustomCarFront, getCustomCarBack, getView],
    (front, back, view) => view === "front" ? front : back
);
const getDefaultCarView = createSelector(
    [getDefaultBack, getDefaultFront, getView],
    (front, back, view) => view === "front" ? front : back
);
const getCarView = createSelector(
    [getCustomCarView, getDefaultCarView],
    (customView, defaultView) => customView || defaultView
);

const getViewsOfFavoritCars = createSelector(
    [getFavorits], 
    (favorits) => favorits.map(uid => {
        // reuse "getCarView" with given "uid"
    })
);

Thanks in advance

@martinkutter
Copy link
Author

After rethinking about this situation with a colleague, it is the same as described in #28.
Unfortunately without good caching...

reduxStore = {
    defaultFront: "defaultFront.jpg",
    defaultBack: "defaultBack.jpg",
    view: "front",
    favorits: ["uid1", "uid2"],
    cars: {
        "uid1": {
            front: "front.jpg",
            back: "back.jpg",
        },
        "uid2": {},
        ...
    }
}

const identity = x => x;
const getUidFromProps = createIdSelector(props => props.uid);
const getView = state => state.view;
const getCars = state => state.cars;
const getDefaultBack = state => state.defaultBack;
const getDefaultFront = state => state.defaultFront;
const getFavorits = state => state.favorits;

const getCar = createSelector(
    [getUidFromProps, getCars],
    (uid, cars) => cars[uid]
);

const getCustomCarFront = createSelector([getCar], car => car.front);
const getCustomCarBack = createSelector([getCar], car => car.back);

const getCustomCarView = createSelector(
    [getCustomCarFront, getCustomCarBack, getView],
    (front, back, view) => view === "front" ? front : back
);
const getDefaultCarView = createSelector(
    [getDefaultBack, getDefaultFront, getView],
    (front, back, view) => view === "front" ? front : back
);
const getCarView = createSelector(
    [getCustomCarView, getDefaultCarView],
    (customView, defaultView) => customView || defaultView
);

const getViewsOfFavoritCars = createSelector(
    [identity, getFavorits], 
    (state, favorits) => favorits.map(uid => getCarView(state, { uid })
);

Or is there any better solution?

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

1 participant