aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/RecipePreviewsStore.js
blob: 382820d58ce8d3aff865b6fb39a8d53cec009926 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { action, computed, observable } from 'mobx';
import { debounce } from 'lodash';
import ms from 'ms';

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);
  }, ms('3s'));
}