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

The example for selectors in docs/entity/adapter.md is not correct. #403

Closed
code2nguyen opened this issue Sep 18, 2017 · 1 comment
Closed

Comments

@code2nguyen
Copy link

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Feature request
[x ] Documentation issue or request

What is the current behavior?

I think the example in the document of entity selector is not correct:
Doc

export const {
  // select the array of user ids
  selectIds: getUserIds,

  // select the dictionary of user entities
  selectEntities: getUserEntities,

  // select the array of users
  selectAll: getAllUsers,

  // select the total user count
  selectTotal: getUserTotal
} = fromUser.adapter.getSelectors(selectUserState);

export const selectUserIds = createSelector(selectUserState, getUserIds);
export const selectUserEntities = createSelector(selectUserState, getUserEntities);
export const selectAllUsers = createSelector(selectUserState, getAllUsers);
export const selectUserCount = createSelector(selectUserState, getUserTotal);
export const selectCurrentUserId = createSelector(selectUserState, fromUser.getSelectedUserId);
export const selectCurrentUser = createSelector(
  selectUserEntities,
  selectCurrentUserId,
  (userEntities, userId) => userEntities[userId]
);

Expected behavior:

export const {
  // select the array of user ids
  selectIds: selectUserIds ,

  // select the dictionary of user entities
  selectEntities: selectUserEntities,

  // select the array of users
  selectAll: selectAllUsers ,

  // select the total user count
  selectTotal: selectUserCount
} = fromUser.adapter.getSelectors(selectUserState);

export const selectCurrentUserId = createSelector(selectUserState, fromUser.getSelectedUserId);
export const selectCurrentUser = createSelector(
  selectUserEntities,
  selectCurrentUserId,
  (userEntities, userId) => userEntities[userId]
);

Because from the source code in state_selector.ts, I think getSelectors function has already created the selectors for ids and entities properties.

    getSelectors<V>(
      selectState: (state: V) => EntityState<T>
    ): EntitySelectors<T, V> {
      const selectIds = (state: EntityState<T>) => state.ids;
      const selectEntities = (state: EntityState<T>) => state.entities;
      const selectAll = createSelector(
        selectIds,
        selectEntities,
        (ids, entities) => ids.map(id => entities[id])
      );

      const selectTotal = createSelector(selectIds, ids => ids.length);

      return {
        selectIds: createSelector(selectState, selectIds),
        selectEntities: createSelector(selectState, selectEntities),
        selectAll: createSelector(selectState, selectAll),
        selectTotal: createSelector(selectState, selectTotal),
      };

Minimal reproduction of the problem with instructions:

Version of affected browser(s),operating system(s), npm, node and ngrx:

Other information:

@MikeRyanDev
Copy link
Member

Closed in ddb2f97

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