summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/projects_dropdown/components/app.vue
blob: 34a60dd574bccb986fa7bf8321f421ce7921121b (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<script>
import bs from '../../breakpoints';
import eventHub from '../event_hub';
import loadingIcon from '../../vue_shared/components/loading_icon.vue';

import projectsListFrequent from './projects_list_frequent.vue';
import projectsListSearch from './projects_list_search.vue';

import search from './search.vue';

export default {
  components: {
    search,
    loadingIcon,
    projectsListFrequent,
    projectsListSearch,
  },
  props: {
    currentProject: {
      type: Object,
      required: true,
    },
    store: {
      type: Object,
      required: true,
    },
    service: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      isLoadingProjects: false,
      isFrequentsListVisible: false,
      isSearchListVisible: false,
      isLocalStorageFailed: false,
      isSearchFailed: false,
      searchQuery: '',
    };
  },
  computed: {
    frequentProjects() {
      return this.store.getFrequentProjects();
    },
    searchProjects() {
      return this.store.getSearchedProjects();
    },
  },
  created() {
    if (this.currentProject.id) {
      this.logCurrentProjectAccess();
    }

    eventHub.$on('dropdownOpen', this.fetchFrequentProjects);
    eventHub.$on('searchProjects', this.fetchSearchedProjects);
    eventHub.$on('searchCleared', this.handleSearchClear);
    eventHub.$on('searchFailed', this.handleSearchFailure);
  },
  beforeDestroy() {
    eventHub.$off('dropdownOpen', this.fetchFrequentProjects);
    eventHub.$off('searchProjects', this.fetchSearchedProjects);
    eventHub.$off('searchCleared', this.handleSearchClear);
    eventHub.$off('searchFailed', this.handleSearchFailure);
  },
  methods: {
    toggleFrequentProjectsList(state) {
      this.isLoadingProjects = !state;
      this.isSearchListVisible = !state;
      this.isFrequentsListVisible = state;
    },
    toggleSearchProjectsList(state) {
      this.isLoadingProjects = !state;
      this.isFrequentsListVisible = !state;
      this.isSearchListVisible = state;
    },
    toggleLoader(state) {
      this.isFrequentsListVisible = !state;
      this.isSearchListVisible = !state;
      this.isLoadingProjects = state;
    },
    fetchFrequentProjects() {
      const screenSize = bs.getBreakpointSize();
      if (this.searchQuery && (screenSize !== 'sm' && screenSize !== 'xs')) {
        this.toggleSearchProjectsList(true);
      } else {
        this.toggleLoader(true);
        this.isLocalStorageFailed = false;
        const projects = this.service.getFrequentProjects();
        if (projects) {
          this.toggleFrequentProjectsList(true);
          this.store.setFrequentProjects(projects);
        } else {
          this.isLocalStorageFailed = true;
          this.toggleFrequentProjectsList(true);
          this.store.setFrequentProjects([]);
        }
      }
    },
    fetchSearchedProjects(searchQuery) {
      this.searchQuery = searchQuery;
      this.toggleLoader(true);
      this.service.getSearchedProjects(this.searchQuery)
        .then(res => res.json())
        .then((results) => {
          this.toggleSearchProjectsList(true);
          this.store.setSearchedProjects(results);
        })
        .catch(() => {
          this.isSearchFailed = true;
          this.toggleSearchProjectsList(true);
        });
    },
    logCurrentProjectAccess() {
      this.service.logProjectAccess(this.currentProject);
    },
    handleSearchClear() {
      this.searchQuery = '';
      this.toggleFrequentProjectsList(true);
      this.store.clearSearchedProjects();
    },
    handleSearchFailure() {
      this.isSearchFailed = true;
      this.toggleSearchProjectsList(true);
    },
  },
};
</script>

<template>
  <div>
    <search/>
    <loading-icon
      class="loading-animation prepend-top-20"
      size="2"
      v-if="isLoadingProjects"
      :label="s__('ProjectsDropdown|Loading projects')"
    />
    <div
      class="section-header"
      v-if="isFrequentsListVisible"
    >
      {{ s__('ProjectsDropdown|Frequently visited') }}
    </div>
    <projects-list-frequent
      v-if="isFrequentsListVisible"
      :local-storage-failed="isLocalStorageFailed"
      :projects="frequentProjects"
    />
    <projects-list-search
      v-if="isSearchListVisible"
      :search-failed="isSearchFailed"
      :matcher="searchQuery"
      :projects="searchProjects"
    />
  </div>
</template>