mirror of
https://gitlab.com/wheres-the-tp/ui-mobile.git
synced 2026-01-25 09:54:55 -06:00
41 lines
1 KiB
JavaScript
41 lines
1 KiB
JavaScript
//@flow
|
|
import { BehaviorSubject } from 'rxjs';
|
|
import { Record, List, get } from 'immutable';
|
|
import filter$ from './FilterStream';
|
|
import foodItems$ from './FoodItemsStream';
|
|
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(foodItems$)
|
|
.map(([faves, foodItems]) => {
|
|
return faves && faves.map(fave => get(foodItems, get(fave, 'food_item_id')));
|
|
})
|
|
.combineLatest(filter$)
|
|
.map(([faveFoodItems, filter]) => {
|
|
const filterTest = filter.search && test(new RegExp(filter.search, 'i'));
|
|
return (
|
|
faveFoodItems &&
|
|
faveFoodItems.filter(faveFoodItem => {
|
|
if (!faveFoodItem) {
|
|
return false;
|
|
}
|
|
return !filterTest || filterTest(faveFoodItem.name);
|
|
})
|
|
);
|
|
});
|