Skip to content

Instantly share code, notes, and snippets.

@nergal
Created February 21, 2018 22:59
Show Gist options
  • Save nergal/591a54437161623584568387ca208be6 to your computer and use it in GitHub Desktop.
Save nergal/591a54437161623584568387ca208be6 to your computer and use it in GitHub Desktop.
class SomeComponent {
public interactionsPublic$: Observable<Interaction[]>;
private interactions$: Observable<Interaction[]>;
public filterField: 'isAccepted' | 'isPending' | 'isRejected';
protected defaultFilter = 'isAccepted';
public filterSubject: BehaviorSubject<string[]> = new BehaviorSubject([]);
constructor(
public store: Store<ProductsState>,
public modalService: NgbModal
) {
this.interactions$ = this.store.select('interactions');
this.interactionsPublic$ = combineLatest(
this.interactions$,
this.filterSubject
).pipe(
map(([items, ids]: [Interaction[], string[]]) =>
filter((item: Interaction) => includes(item.interactionId)(ids))(items)
)
);
}
onFilterChange($event) {
this.filterField = $event;
this.interactions$.pipe(
first(),
map(filter(this.filterField)),
map(flatMap('interactionId')),
).subscribe((items: string[]) => this.filterSubject.next(items));
}
ngOnInit() {
const subscription = this.interactions$.pipe(
filter$(x => !!x.length)
).subscribe(() => {
this.onFilterChange(this.defaultFilter);
subscription.unsubscribe();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment