All files / app/assets/javascripts/groups/components app.vue

80.51% Statements 62/77
88.23% Branches 15/17
96.77% Functions 30/31
80.51% Lines 62/77

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269                  2x                                                               99x                   99x           99x         104x 99x   5x         158x     158x     56x     99x                       99x                             2x     129x     56x 54x   56x     73x 73x           101x 101x   101x   101x           101x 101x       22x   22x         22x 22x       1x   1x           1x 1x   1x 1x               1x       4x 4x 3x 2x 2x       2x     1x     1x     1x       2x 2x 2x     3x 3x     1x 1x 1x     2x 2x 1x     2x       53x     129x   129x 57x   72x                                                                              
<script>
import { GlLoadingIcon, GlModal, GlEmptyState } from '@gitlab/ui';
import { createAlert } from '~/alert';
import { HTTP_STATUS_FORBIDDEN } from '~/lib/utils/http_status';
import { mergeUrlParams, getParameterByName } from '~/lib/utils/url_utility';
import { __, s__, sprintf } from '~/locale';
 
import { COMMON_STR } from '../constants';
import eventHub from '../event_hub';
import GroupsComponent from './groups.vue';
 
export default {
  i18n: {
    searchEmptyState: {
      title: __('No results found'),
      description: __('Edit your search and try again'),
    },
  },
  components: {
    GroupsComponent,
    GlModal,
    GlLoadingIcon,
    GlEmptyState,
  },
  inject: ['emptySearchIllustration'],
  props: {
    action: {
      type: String,
      required: false,
      default: '',
    },
    store: {
      type: Object,
      required: true,
    },
    service: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      isModalVisible: false,
      isLoading: true,
      fromSearch: false,
      targetGroup: null,
      targetParentGroup: null,
    };
  },
  computed: {
    primaryProps() {
      return {
        text: __('Leave group'),
        attributes: { variant: 'danger', category: 'primary' },
      };
    },
    cancelProps() {
      return {
        text: __('Cancel'),
      };
    },
    groupLeaveConfirmationMessage() {
      if (!this.targetGroup) {
        return '';
      }
      return sprintf(s__('GroupsTree|Are you sure you want to leave the "%{fullName}" group?'), {
        fullName: this.targetGroup.fullName,
      });
    },
    groups() {
      return this.store.getGroups();
    },
    hasGroups() {
      return this.groups && this.groups.length > 0;
    },
    pageInfo() {
      return this.store.getPaginationInfo();
    },
    filterGroupsBy() {
      return getParameterByName('filter') || null;
    },
  },
  created() {
    eventHub.$on(`${this.action}fetchPage`, this.fetchPage);
    eventHub.$on(`${this.action}toggleChildren`, this.toggleChildren);
    eventHub.$on(`${this.action}showLeaveGroupModal`, this.showLeaveGroupModal);
    eventHub.$on(`${this.action}updatePagination`, this.updatePagination);
    eventHub.$on(`${this.action}updateGroups`, this.updateGroups);
    eventHub.$on(`${this.action}fetchFilteredAndSortedGroups`, this.fetchFilteredAndSortedGroups);
  },
  mounted() {
    this.fetchAllGroups();
  },
  beforeDestroy() {
    eventHub.$off(`${this.action}fetchPage`, this.fetchPage);
    eventHub.$off(`${this.action}toggleChildren`, this.toggleChildren);
    eventHub.$off(`${this.action}showLeaveGroupModal`, this.showLeaveGroupModal);
    eventHub.$off(`${this.action}updatePagination`, this.updatePagination);
    eventHub.$off(`${this.action}updateGroups`, this.updateGroups);
    eventHub.$off(`${this.action}fetchFilteredAndSortedGroups`, this.fetchFilteredAndSortedGroups);
  },
  methods: {
    hideModal() {
      this.isModalVisible = false;
    },
    showModal() {
      this.isModalVisible = true;
    },
    fetchGroups({ parentId, page, filterGroupsBy, sortBy, updatePagination }) {
      return this.service
        .getGroups(parentId, page, filterGroupsBy, sortBy)
        .then((res) => {
          if (updatePagination) {
            this.updatePagination(res.headers);
          }
          return res.data;
        })
        .catch(() => {
          this.isLoading = false;
          window.scrollTo({ top: 0, behavior: 'smooth' });
 
          createAlert({ message: COMMON_STR.FAILURE });
        });
    },
    fetchAllGroups() {
      const page = getParameterByName('page') || null;
      const sortBy = getParameterByName('sort') || null;
 
      this.isLoading = true;
 
      return this.fetchGroups({
        page,
        filterGroupsBy: this.filterGroupsBy,
        sortBy,
        updatePagination: true,
      }).then((res) => {
        this.isLoading = false;
        this.updateGroups(res, Boolean(this.filterGroupsBy));
      });
    },
    fetchFilteredAndSortedGroups({ filterGroupsBy, sortBy }) {
      this.isLoading = true;
 
      return this.fetchGroups({
        filterGroupsBy,
        sortBy,
        updatePagination: true,
      }).then((res) => {
        this.isLoading = false;
        this.updateGroups(res, Boolean(filterGroupsBy));
      });
    },
    fetchPage({ page, filterGroupsBy, sortBy }) {
      this.isLoading = true;
 
      return this.fetchGroups({
        page,
        filterGroupsBy,
        sortBy,
        updatePagination: true,
      }).then((res) => {
        this.isLoading = false;
        window.scrollTo({ top: 0, behavior: 'smooth' });
 
        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;
          this.fetchGroups({
            parentId: parentGroup.id,
          })
            .then((res) => {
              this.store.setGroupChildren(parentGroup, res);
            })
            .catch(() => {
              parentGroup.isChildrenLoading = false;
            });
        } else {
          parentGroup.isOpen = true;
        }
      } else {
        parentGroup.isOpen = false;
      }
    },
    showLeaveGroupModal(group, parentGroup) {
      this.targetGroup = group;
      this.targetParentGroup = parentGroup;
      this.showModal();
    },
    leaveGroup() {
      this.targetGroup.isBeingRemoved = true;
      this.service
        .leaveGroup(this.targetGroup.leavePath)
        .then((res) => {
          window.scrollTo({ top: 0, behavior: 'smooth' });
          this.store.removeGroup(this.targetGroup, this.targetParentGroup);
          this.$toast.show(res.data.notice);
        })
        .catch((err) => {
          let message = COMMON_STR.FAILURE;
          if (err.status === HTTP_STATUS_FORBIDDEN) {
            message = COMMON_STR.LEAVE_FORBIDDEN;
          }
          createAlert({ message });
          this.targetGroup.isBeingRemoved = false;
        });
    },
    updatePagination(headers) {
      this.store.setPaginationInfo(headers);
    },
    updateGroups(groups, fromSearch) {
      this.fromSearch = fromSearch;
 
      if (fromSearch) {
        this.store.setSearchedGroups(groups);
      } else {
        this.store.setGroups(groups);
      }
    },
  },
};
</script>
 
<template>
  <div>
    <gl-loading-icon
      v-if="isLoading"
      :label="s__('GroupsTree|Loading groups')"
      size="lg"
      class="loading-animation prepend-top-20"
    />
    <template v-else>
      <groups-component v-if="hasGroups" :groups="groups" :page-info="pageInfo" :action="action" />
      <gl-empty-state
        v-else-if="fromSearch"
        :svg-path="emptySearchIllustration"
        :title="$options.i18n.searchEmptyState.title"
        :description="$options.i18n.searchEmptyState.description"
        data-testid="search-empty-state"
      />
      <slot v-else name="empty-state"></slot>
    </template>
    <gl-modal
      modal-id="leave-group-modal"
      :visible="isModalVisible"
      :title="__('Are you sure?')"
      :action-primary="primaryProps"
      :action-cancel="cancelProps"
      @primary="leaveGroup"
      @hide="hideModal"
    >
      {{ groupLeaveConfirmationMessage }}
    </gl-modal>
  </div>
</template>