Skip to content

Select filtered relations #1445

@webstik1811

Description

@webstik1811

Here is my case. I have two stores: orders and articles. Order is connected with articles with one to many relation.
artciles contain orderId which is the relational key .
Here is the structure of both objects:

export interface IOrders {
  id: string;
  tableName: string;
  operator: string;
  createdAt: Date;
  elements: IArticles[];
  ...
}

export interface IArticles {
  id: string;
  orderId: string;
  quantity: number;
  article: string;
  description: string;
  status: ArticleStatus
  ...
}

So, what I have into the selectors:
First I need to fetch all articleIds which have a specific status

const allArticleIds = createSelector(selectArticleState, state => Object.values(state.entities)
  .filter(article => [ArticleStatus.ORDERED, ArticleStatus.RETURNED, ArticleStatus.LOCKED, ArticleStatus.CANCELED].includes(article.status))
  .map(article => article.id));

then lets try to create a selector which will return only desired articles:

...
export const selectArticleState = createFeatureSelector<fromArticles.State>('articles');
...
const selectAllOrderedArticles = createSelector(
  s => s,
  // selecting ids
  allOrderedArticleIds,
  selectArticleState,
);

If I create a store selector from the last:

public readonly articles$ = this.store.select(selectAllOrderedArticles);

and subscribe to that, as result will have only the filtered articles. This is great, but now I want to select orders, with their ONLY FILTERED articles! How I can do that?

What I've trying is following:

export const selectOrderState = createFeatureSelector<fromOrders.State>('orders');
...

const selectOrderWithArticles = rootEntity(
  selectOrderState,
  childrenEntities(
    selectAllOrderedArticles,
    'orderId',
    'elements',
  ),
);

const selectOrdersWithArticles = rootEntities(selectOrderWithArticles);

const allOrdersIds = createSelector(selectOrderState, state => state.ids);

const selectAllOrders = createSelector(
  // selecting the whole store
  s => s,
  // selecting ids
  allOrdersIds,
  // selecting all users with desired relationships
  selectOrdersWithArticles,
);

I expected selectOrderWithArticles to return orders with the filtered articles because I've used selectAllOrderedArticles, but actually I receive all articles instead.

Do I have something wrong, or there is a bug in the package?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions