summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/issues/dashboard/utils.js
blob: 6fa95b386499441eb602fde8af3b3fcd4f4f76df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import fuzzaldrinPlus from 'fuzzaldrin-plus';
import { MAX_LIST_SIZE } from '~/issues/list/constants';
import axios from '~/lib/utils/axios_utils';

export class AutocompleteCache {
  constructor() {
    this.cache = {};
  }

  fetch({ url, cacheName, searchProperty, search }) {
    if (this.cache[cacheName]) {
      const data = search
        ? fuzzaldrinPlus.filter(this.cache[cacheName], search, { key: searchProperty })
        : this.cache[cacheName].slice(0, MAX_LIST_SIZE);
      return Promise.resolve(data);
    }

    return axios.get(url).then(({ data }) => {
      this.cache[cacheName] = data;
      return data.slice(0, MAX_LIST_SIZE);
    });
  }
}