All files / app/assets/javascripts/groups/store groups_store.js

93.02% Statements 40/43
91.18% Branches 31/34
93.33% Functions 14/15
94.87% Lines 37/39

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            13x 13x 13x 13x 13x       39x 254x   1x         1x 5x 6x 6x 4x   6x     1x 1x             3x 3x 2x 2x       59x           35x 1x 1x   34x     35x       74x       267x 267x 267x       267x                                                               267x 1x             267x       2x 2x 2x            
import { isEmpty } from 'lodash';
import { normalizeHeaders, parseIntPagination } from '~/lib/utils/common_utils';
import { getGroupItemMicrodata } from './utils';
 
export default class GroupsStore {
  constructor({ hideProjects = false, showSchemaMarkup = false } = {}) {
    this.state = {};
    this.state.groups = [];
    this.state.pageInfo = {};
    this.hideProjects = hideProjects;
    this.showSchemaMarkup = showSchemaMarkup;
  }
 
  setGroups(rawGroups) {
    if (rawGroups && rawGroups.length) {
      this.state.groups = rawGroups.map((rawGroup) => this.formatGroupItem(rawGroup));
    } else {
      this.state.groups = [];
    }
  }
 
  setSearchedGroups(rawGroups) {
    const formatGroups = (groups) =>
      groups.map((group) => {
        const formattedGroup = this.formatGroupItem(group);
        if (formattedGroup.children && formattedGroup.children.length) {
          formattedGroup.children = formatGroups(formattedGroup.children);
        }
        return formattedGroup;
      });
 
    Eif (rawGroups && rawGroups.length) {
      this.state.groups = formatGroups(rawGroups);
    } else {
      this.state.groups = [];
    }
  }
 
  setGroupChildren(parentGroup, children) {
    const updatedParentGroup = parentGroup;
    updatedParentGroup.children = children.map((rawChild) => this.formatGroupItem(rawChild));
    updatedParentGroup.isOpen = true;
    updatedParentGroup.isChildrenLoading = false;
  }
 
  getGroups() {
    return this.state.groups;
  }
 
  setPaginationInfo(pagination = {}) {
    let paginationInfo;
 
    if (Object.keys(pagination).length) {
      const normalizedHeaders = normalizeHeaders(pagination);
      paginationInfo = parseIntPagination(normalizedHeaders);
    } else {
      paginationInfo = pagination;
    }
 
    this.state.pageInfo = paginationInfo;
  }
 
  getPaginationInfo() {
    return this.state.pageInfo;
  }
 
  formatGroupItem(rawGroupItem) {
    const groupChildren = rawGroupItem.children || [];
    const groupIsOpen = groupChildren.length > 0 || false;
    const childrenCount = this.hideProjects
      ? rawGroupItem.subgroup_count
      : rawGroupItem.children_count;
 
    const groupItem = {
      id: rawGroupItem.id,
      name: rawGroupItem.name,
      fullName: rawGroupItem.full_name,
      description: rawGroupItem.markdown_description,
      visibility: rawGroupItem.visibility,
      avatarUrl: rawGroupItem.avatar_url,
      relativePath: rawGroupItem.relative_path,
      editPath: rawGroupItem.edit_path,
      leavePath: rawGroupItem.leave_path,
      canEdit: rawGroupItem.can_edit,
      canLeave: rawGroupItem.can_leave,
      type: rawGroupItem.type,
      permission: rawGroupItem.permission,
      children: groupChildren,
      isOpen: groupIsOpen,
      isChildrenLoading: false,
      isBeingRemoved: false,
      parentId: rawGroupItem.parent_id,
      childrenCount,
      projectCount: rawGroupItem.project_count,
      subgroupCount: rawGroupItem.subgroup_count,
      memberCount: rawGroupItem.number_users_with_delimiter,
      starCount: rawGroupItem.star_count,
      updatedAt: rawGroupItem.updated_at,
      pendingRemoval: rawGroupItem.marked_for_deletion,
      microdata: this.showSchemaMarkup ? getGroupItemMicrodata(rawGroupItem) : {},
      lastActivityAt: rawGroupItem.last_activity_at
        ? rawGroupItem.last_activity_at
        : rawGroupItem.updated_at,
    };
 
    if (!isEmpty(rawGroupItem.compliance_management_framework)) {
      groupItem.complianceFramework = {
        name: rawGroupItem.compliance_management_framework.name,
        color: rawGroupItem.compliance_management_framework.color,
        description: rawGroupItem.compliance_management_framework.description,
      };
    }
 
    return groupItem;
  }
 
  removeGroup(group, parentGroup) {
    const updatedParentGroup = parentGroup;
    Eif (updatedParentGroup.children && updatedParentGroup.children.length) {
      updatedParentGroup.children = parentGroup.children.filter((child) => group.id !== child.id);
    } else {
      this.state.groups = this.state.groups.filter((child) => group.id !== child.id);
    }
  }
}