From 58cda9cc7fb79ca9df6746de7f9662bc08dc156a Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Fri, 13 Oct 2017 12:29:40 +0200 Subject: initial commit --- src/stores/RecipePreviewsStore.js | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/stores/RecipePreviewsStore.js (limited to 'src/stores/RecipePreviewsStore.js') diff --git a/src/stores/RecipePreviewsStore.js b/src/stores/RecipePreviewsStore.js new file mode 100644 index 000000000..e25936f15 --- /dev/null +++ b/src/stores/RecipePreviewsStore.js @@ -0,0 +1,50 @@ +import { action, computed, observable } from 'mobx'; +import { debounce } from 'lodash'; + +import Store from './lib/Store'; +import CachedRequest from './lib/CachedRequest'; +import Request from './lib/Request'; +import { gaEvent } from '../lib/analytics'; + +export default class RecipePreviewsStore extends Store { + @observable allRecipePreviewsRequest = new CachedRequest(this.api.recipePreviews, 'all'); + @observable featuredRecipePreviewsRequest = new CachedRequest(this.api.recipePreviews, 'featured'); + @observable searchRecipePreviewsRequest = new Request(this.api.recipePreviews, 'search'); + + constructor(...args) { + super(...args); + + // Register action handlers + this.actions.recipePreview.search.listen(this._search.bind(this)); + } + + @computed get all() { + return this.allRecipePreviewsRequest.execute().result || []; + } + + @computed get featured() { + return this.featuredRecipePreviewsRequest.execute().result || []; + } + + @computed get searchResults() { + return this.searchRecipePreviewsRequest.result || []; + } + + @computed get dev() { + return this.stores.recipes.all.filter(r => r.local); + } + + // Actions + @action _search({ needle }) { + if (needle !== '') { + this.searchRecipePreviewsRequest.execute(needle); + + this._analyticsSearch(needle); + } + } + + // Helper + _analyticsSearch = debounce((needle) => { + gaEvent('Recipe', 'search', needle); + }, 3000); +} -- cgit v1.2.3-54-g00ecf