summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/groups_list.js
blob: 0ef81e494449ed0ff4aba5dcc710451e0c06248b (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
/**
 * Based on project list search.
 * Makes search request for groups when user types a value in the search input.
 * Updates the html content of the page with the received one.
 */
export default class GroupsList {
  constructor() {
    this.groupsListFilterElement = document.querySelector('.js-groups-list-filter');
    this.groupsListHolderElement = document.querySelector('.js-groups-list-holder');

    this.initSearch();
  }

  initSearch() {
    this.debounceFilter = _.debounce(this.filterResults.bind(this), 500);

    this.groupsListFilterElement.removeEventListener('input', this.debounceFilter);
    this.groupsListFilterElement.addEventListener('input', this.debounceFilter);
  }

  filterResults() {
    const form = document.querySelector('form#group-filter-form');
    const groupFilterUrl = `${form.getAttribute('action')}?${$(form).serialize()}`;

    $(this.groupsListHolderElement).fadeTo(250, 0.5);

    return $.ajax({
      url: form.getAttribute('action'),
      data: $(form).serialize(),
      type: 'GET',
      dataType: 'json',
      context: this,
      complete() {
        $(this.groupsListHolderElement).fadeTo(250, 1);
      },
      success(data) {
        this.groupsListHolderElement.innerHTML = data.html;

       // Change url so if user reload a page - search results are saved
        return window.history.replaceState({
          page: groupFilterUrl,

        }, document.title, groupFilterUrl);
      },
    });
  }
}