aretherecookies-mobile/js/streams/FavesStream.js
2020-04-17 22:50:23 -05:00

41 lines
1 KiB
JavaScript

//@flow
import { BehaviorSubject } from 'rxjs';
import { Record, List, get } from 'immutable';
import filter$ from './FilterStream';
import products$ from './ProductsStream';
import { test } from 'ramda';
export type Fave = {
food_item_id: string,
date: number,
};
export type Faves = List<Record>;
const observable: BehaviorSubject<Faves> = new BehaviorSubject(null);
export function emit(fave: ?Faves) {
observable.next(fave);
}
export function emitOne(fave: ?Fave) {
observable.first().subscribe(currentFaves => observable.next(currentFaves.push(fave)));
}
export default observable
.combineLatest(products$)
.map(([faves, products]) => {
return faves && faves.map(fave => get(products, get(fave, 'food_item_id')));
})
.combineLatest(filter$)
.map(([faveProducts, filter]) => {
const filterTest = filter.search && test(new RegExp(filter.search, 'i'));
return (
faveProducts &&
faveProducts.filter(faveProduct => {
if (!faveProduct) {
return false;
}
return !filterTest || filterTest(faveProduct.name);
})
);
});