summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/groups/store/groups_store.js
blob: a1689f4c5cc7c3de50fc2445fd9ad3d02c168d69 (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
import { normalizeHeaders, parseIntPagination } from '../../lib/utils/common_utils';

export default class GroupsStore {
  constructor(hideProjects) {
    this.state = {};
    this.state.groups = [];
    this.state.pageInfo = {};
    this.hideProjects = hideProjects;
  }

  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;
    });

    if (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;

    return {
      id: rawGroupItem.id,
      name: rawGroupItem.name,
      fullName: rawGroupItem.full_name,
      description: rawGroupItem.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,
    };
  }

  removeGroup(group, parentGroup) {
    const updatedParentGroup = parentGroup;
    if (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);
    }
  }
}