summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/groups/components/app.vue
blob: 241e026b84cd0296bba6d2666d294e5bc58b5c7a (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<script>
/* global Flash */

import eventHub from '../event_hub';
import { getParameterByName } from '../../lib/utils/common_utils';
import loadingIcon from '../../vue_shared/components/loading_icon.vue';
import { COMMON_STR } from '../constants';
import { mergeUrlParams } from '../../lib/utils/url_utility';
import groupsComponent from './groups.vue';

export default {
  components: {
    loadingIcon,
    groupsComponent,
  },
  props: {
    store: {
      type: Object,
      required: true,
    },
    service: {
      type: Object,
      required: true,
    },
    hideProjects: {
      type: Boolean,
      required: true,
    },
  },
  data() {
    return {
      isLoading: true,
      isSearchEmpty: false,
      searchEmptyMessage: '',
    };
  },
  computed: {
    groups() {
      return this.store.getGroups();
    },
    pageInfo() {
      return this.store.getPaginationInfo();
    },
  },
  methods: {
    fetchGroups({ parentId, page, filterGroupsBy, sortBy, archived, updatePagination }) {
      return this.service.getGroups(parentId, page, filterGroupsBy, sortBy, archived)
                .then((res) => {
                  if (updatePagination) {
                    this.updatePagination(res.headers);
                  }

                  return res;
                })
                .then(res => res.json())
                .catch(() => {
                  this.isLoading = false;
                  $.scrollTo(0);

                  Flash(COMMON_STR.FAILURE);
                });
    },
    fetchAllGroups() {
      const page = getParameterByName('page') || null;
      const sortBy = getParameterByName('sort') || null;
      const archived = getParameterByName('archived') || null;
      const filterGroupsBy = getParameterByName('filter') || null;

      this.isLoading = true;
      // eslint-disable-next-line promise/catch-or-return
      this.fetchGroups({
        page,
        filterGroupsBy,
        sortBy,
        archived,
        updatePagination: true,
      }).then((res) => {
        this.isLoading = false;
        this.updateGroups(res, Boolean(filterGroupsBy));
      });
    },
    fetchPage(page, filterGroupsBy, sortBy, archived) {
      this.isLoading = true;

      // eslint-disable-next-line promise/catch-or-return
      this.fetchGroups({
        page,
        filterGroupsBy,
        sortBy,
        archived,
        updatePagination: true,
      }).then((res) => {
        this.isLoading = false;
        $.scrollTo(0);

        const currentPath = mergeUrlParams({ page }, window.location.href);
        window.history.replaceState({
          page: currentPath,
        }, document.title, currentPath);

        this.updateGroups(res);
      });
    },
    toggleChildren(group) {
      const parentGroup = group;
      if (!parentGroup.isOpen) {
        if (parentGroup.children.length === 0) {
          parentGroup.isChildrenLoading = true;
          // eslint-disable-next-line promise/catch-or-return
          this.fetchGroups({
            parentId: parentGroup.id,
          }).then((res) => {
            this.store.setGroupChildren(parentGroup, res);
          }).catch(() => {
            parentGroup.isChildrenLoading = false;
          });
        } else {
          parentGroup.isOpen = true;
        }
      } else {
        parentGroup.isOpen = false;
      }
    },
    leaveGroup(group, parentGroup) {
      const targetGroup = group;
      targetGroup.isBeingRemoved = true;
      this.service.leaveGroup(targetGroup.leavePath)
        .then(res => res.json())
        .then((res) => {
          $.scrollTo(0);
          this.store.removeGroup(targetGroup, parentGroup);
          Flash(res.notice, 'notice');
        })
        .catch((err) => {
          let message = COMMON_STR.FAILURE;
          if (err.status === 403) {
            message = COMMON_STR.LEAVE_FORBIDDEN;
          }
          Flash(message);
          targetGroup.isBeingRemoved = false;
        });
    },
    updatePagination(headers) {
      this.store.setPaginationInfo(headers);
    },
    updateGroups(groups, fromSearch) {
      this.isSearchEmpty = groups ? groups.length === 0 : false;
      if (fromSearch) {
        this.store.setSearchedGroups(groups);
      } else {
        this.store.setGroups(groups);
      }
    },
  },
  created() {
    this.searchEmptyMessage = this.hideProjects ?
      COMMON_STR.GROUP_SEARCH_EMPTY : COMMON_STR.GROUP_PROJECT_SEARCH_EMPTY;

    eventHub.$on('fetchPage', this.fetchPage);
    eventHub.$on('toggleChildren', this.toggleChildren);
    eventHub.$on('leaveGroup', this.leaveGroup);
    eventHub.$on('updatePagination', this.updatePagination);
    eventHub.$on('updateGroups', this.updateGroups);
  },
  mounted() {
    this.fetchAllGroups();
  },
  beforeDestroy() {
    eventHub.$off('fetchPage', this.fetchPage);
    eventHub.$off('toggleChildren', this.toggleChildren);
    eventHub.$off('leaveGroup', this.leaveGroup);
    eventHub.$off('updatePagination', this.updatePagination);
    eventHub.$off('updateGroups', this.updateGroups);
  },
};
</script>

<template>
  <div>
    <loading-icon
      class="loading-animation prepend-top-20"
      size="2"
      v-if="isLoading"
      :label="s__('GroupsTree|Loading groups')"
    />
    <groups-component
      v-if="!isLoading"
      :groups="groups"
      :search-empty="isSearchEmpty"
      :search-empty-message="searchEmptyMessage"
      :page-info="pageInfo"
    />
  </div>
</template>