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

How to get correct action #21

Open
neverlose-lv opened this issue Jul 28, 2023 · 1 comment
Open

How to get correct action #21

neverlose-lv opened this issue Jul 28, 2023 · 1 comment

Comments

@neverlose-lv
Copy link

neverlose-lv commented Jul 28, 2023

Hello.
I use this library in some of my project and I really appreciate it!
Thank you!

But I have a question for a non-particalar use-case..

I have multiple tables on a single view in my projects.

All tables use the same action for loading data, but with a different parameter (id).

action example:

export class LoadFavoriteListObjects {
  static readonly type =
    '[Users -> Lists] Load objects in the specified favorite list';
  constructor(public favoriteListId: number) {}
}

All tables uses a loading spinner, which is show, while some observable returns no null value.

By the default you would use:

  @Select(actionsExecuting([LoadFavoriteListObjects]))
  isLoading$!: Observable<any>;

but this code will trigger to appear the spinner in all tables simultaneously, while the data is fetched only in one of them...

How can I select actionExecuting parameter (favoriteListId) to compare.
lets say the table knows the id of the list, that the data will be loaded for.

.e.g

@Input()
favoriteListId!: number;

isLoading$!: Observable<any>;

constructor(private store: Store) {}

ngOnInit(): void {
    this.isLoading$ = this.store.select(actionsExecuting([LoadFavoriteListObjects]))
      // I would like to have something like pipe to compare the action id, with the input id.
      .pipe(
        filter((action: LoadFavoritesListObjects) => { return action.favoriteListId === this.favoriteListId; })
      )
    ; 
}
@joaqcid
Copy link
Member

joaqcid commented Jul 29, 2023

hi, glad you can use this lib and helps you in your app!

unfortunately this library does not differentiate params on the observed Actions, so i think you have two options:

  • create an action per table, in order to show spinner only in the correspondant table
  • or subscribe to the actions stream and apply filtering based on the param e.g.
this.actions$.pipe(
  map((actionContext) => {
    const actionType = getActionTypeFromInstance(actionContext.action);
    if (actionType === [LoadFavoritesListObjects] && actionContext.action.favoriteListId === [x]) {
      if (actionContext.status === ActionStatus.Dispatched){
        return true;
      } else {
        return false;
      }
    }
    return false;
  }
)

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