summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/filtered_search/recent_searches_root.js
blob: 4e38409e12a5aab137dc53f963db059b4f79be1f (plain)
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
54
55
56
57
58
59
import Vue from 'vue';
import RecentSearchesDropdownContent from './components/recent_searches_dropdown_content';
import eventHub from './event_hub';

class RecentSearchesRoot {
  constructor(
    recentSearchesStore,
    recentSearchesService,
    wrapperElement,
  ) {
    this.store = recentSearchesStore;
    this.service = recentSearchesService;
    this.wrapperElement = wrapperElement;
  }

  init() {
    this.bindEvents();
    this.render();
  }

  bindEvents() {
    this.onRequestClearRecentSearchesWrapper = this.onRequestClearRecentSearches.bind(this);

    eventHub.$on('requestClearRecentSearches', this.onRequestClearRecentSearchesWrapper);
  }

  unbindEvents() {
    eventHub.$off('requestClearRecentSearches', this.onRequestClearRecentSearchesWrapper);
  }

  render() {
    this.vm = new Vue({
      el: this.wrapperElement,
      data: this.store.state,
      template: `
        <recent-searches-dropdown-content
          :items="recentSearches" />
      `,
      components: {
        'recent-searches-dropdown-content': RecentSearchesDropdownContent,
      },
    });
  }

  onRequestClearRecentSearches() {
    const resultantSearches = this.store.setRecentSearches([]);
    this.service.save(resultantSearches);
  }

  destroy() {
    this.unbindEvents();
    if (this.vm) {
      this.vm.$destroy();
    }
  }

}

export default RecentSearchesRoot;