summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/boards/boards_util.js
blob: e5ff41dab74fd86677d310e78d814f3e0d3241a3 (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
import { sortBy } from 'lodash';
import axios from '~/lib/utils/axios_utils';
import { ListType } from './constants';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';

export function getMilestone() {
  return null;
}

export function updateListPosition(listObj) {
  const { listType } = listObj;
  let { position } = listObj;
  if (listType === ListType.closed) {
    position = Infinity;
  } else if (listType === ListType.backlog) {
    position = -Infinity;
  }

  return { ...listObj, position };
}

export function formatBoardLists(lists) {
  return lists.nodes.reduce((map, list) => {
    return {
      ...map,
      [list.id]: updateListPosition(list),
    };
  }, {});
}

export function formatIssue(issue) {
  return {
    ...issue,
    labels: issue.labels?.nodes || [],
    assignees: issue.assignees?.nodes || [],
  };
}

export function formatListIssues(listIssues) {
  const issues = {};
  let listIssuesCount;

  const listData = listIssues.nodes.reduce((map, list) => {
    listIssuesCount = list.issues.count;
    let sortedIssues = list.issues.edges.map(issueNode => ({
      ...issueNode.node,
    }));
    sortedIssues = sortBy(sortedIssues, 'relativePosition');

    return {
      ...map,
      [list.id]: sortedIssues.map(i => {
        const id = getIdFromGraphQLId(i.id);

        const listIssue = {
          ...i,
          id,
          labels: i.labels?.nodes || [],
          assignees: i.assignees?.nodes || [],
        };

        issues[id] = listIssue;

        return id;
      }),
    };
  }, {});

  return { listData, issues, listIssuesCount };
}

export function formatListsPageInfo(lists) {
  const listData = lists.nodes.reduce((map, list) => {
    return {
      ...map,
      [list.id]: list.issues.pageInfo,
    };
  }, {});
  return listData;
}

export function fullBoardId(boardId) {
  return `gid://gitlab/Board/${boardId}`;
}

export function fullLabelId(label) {
  if (label.project_id !== null) {
    return `gid://gitlab/ProjectLabel/${label.id}`;
  }
  return `gid://gitlab/GroupLabel/${label.id}`;
}

export function moveIssueListHelper(issue, fromList, toList) {
  const updatedIssue = issue;
  if (
    toList.listType === ListType.label &&
    !updatedIssue.labels.find(label => label.id === toList.label.id)
  ) {
    updatedIssue.labels.push(toList.label);
  }
  if (fromList?.label && fromList.listType === ListType.label) {
    updatedIssue.labels = updatedIssue.labels.filter(label => fromList.label.id !== label.id);
  }

  if (
    toList.listType === ListType.assignee &&
    !updatedIssue.assignees.find(assignee => assignee.id === toList.assignee.id)
  ) {
    updatedIssue.assignees.push(toList.assignee);
  }
  if (fromList?.assignee && fromList.listType === ListType.assignee) {
    updatedIssue.assignees = updatedIssue.assignees.filter(
      assignee => assignee.id !== fromList.assignee.id,
    );
  }

  return updatedIssue;
}

export function getBoardsPath(endpoint, board) {
  const path = `${endpoint}${board.id ? `/${board.id}` : ''}.json`;

  if (board.id) {
    return axios.put(path, { board });
  }
  return axios.post(path, { board });
}

export function isListDraggable(list) {
  return list.listType !== ListType.backlog && list.listType !== ListType.closed;
}

// EE-specific feature. Find the implementation in the `ee/`-folder
export function transformBoardConfig() {
  return '';
}

export default {
  getMilestone,
  formatIssue,
  formatListIssues,
  fullBoardId,
  fullLabelId,
  getBoardsPath,
  isListDraggable,
};