summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/groups/index.js
blob: 35eac409afadc4b7ff0acb9c8cbe0db1f6984eda (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
/* eslint-disable no-unused-vars */

import Vue from 'vue';
import GroupFilterableList from './groups_filterable_list';
import GroupsComponent from './components/groups.vue';
import GroupFolder from './components/group_folder.vue';
import GroupItem from './components/group_item.vue';
import GroupsStore from './stores/groups_store';
import GroupsService from './services/groups_service';
import eventHub from './event_hub';

$(() => {
  const appEl = document.querySelector('#dashboard-group-app');
  const form = document.querySelector('form#group-filter-form');
  const filter = document.querySelector('.js-groups-list-filter');
  const holder = document.querySelector('.js-groups-list-holder');

  const store = new GroupsStore();
  const service = new GroupsService(appEl.dataset.endpoint);

  Vue.component('groups-component', GroupsComponent);
  Vue.component('group-folder', GroupFolder);
  Vue.component('group-item', GroupItem);

  const GroupsApp = new Vue({
    el: appEl,
    data() {
      return {
        store,
        state: store.state,
      };
    },
    methods: {
      fetchGroups(parentGroup) {
        let parentId = null;
        let getGroups = null;

        if (parentGroup) {
          parentId = parentGroup.id;
        }

        const page = gl.utils.getParameterByName('page');

        getGroups = service.getGroups(parentId, page);
        getGroups.then((response) => {
          store.setGroups(response.json(), parentGroup);
        })
        .catch(() => {
          // TODO: Handler error
        });

        return getGroups;
      },
      toggleSubGroups(parentGroup = null) {
        if (!parentGroup.isOpen) {
          this.fetchGroups(parentGroup);
        }
        GroupsStore.toggleSubGroups(parentGroup);
      },
    },
    created() {
      let groupFilterList = null;

      groupFilterList = new GroupFilterableList(form, filter, holder, store);
      groupFilterList.initSearch();

      this.fetchGroups()
        .then((response) => {
          store.storePagination(response.headers);
        })
        .catch(() => {
          // TODO: Handle error
        });

      eventHub.$on('toggleSubGroups', this.toggleSubGroups);
    },
  });
});