aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/RecipePreviewsStore.js
blob: 10c170e81f1cbfbb2087a3454b49f599ff74173d (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
import { action, computed, observable } from 'mobx';

import Store from './lib/Store';
import CachedRequest from './lib/CachedRequest';
import Request from './lib/Request';

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);
    }
  }
}