summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/members/utils.js
blob: 2bf30dd7b6e7ba87fecfa77b2ba9af54a459c61a (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
import { isUndefined } from 'lodash';
import {
  getParameterByName,
  convertObjectPropsToCamelCase,
  parseBoolean,
} from '~/lib/utils/common_utils';
import { setUrlParams } from '~/lib/utils/url_utility';
import { __ } from '~/locale';
import {
  FIELDS,
  DEFAULT_SORT,
  GROUP_LINK_BASE_PROPERTY_NAME,
  GROUP_LINK_ACCESS_LEVEL_PROPERTY_NAME,
} from './constants';

export const generateBadges = ({ member, isCurrentUser, canManageMembers }) => [
  {
    show: isCurrentUser,
    text: __("It's you"),
    variant: 'success',
  },
  {
    show: member.user?.blocked,
    text: __('Blocked'),
    variant: 'danger',
  },
  {
    show: member.user?.twoFactorEnabled && (canManageMembers || isCurrentUser),
    text: __('2FA'),
    variant: 'info',
  },
];

export const isGroup = (member) => {
  return Boolean(member.sharedWithGroup);
};

export const isDirectMember = (member) => {
  return isGroup(member) || member.isDirectMember;
};

export const isCurrentUser = (member, currentUserId) => {
  return member.user?.id === currentUserId;
};

export const canRemove = (member) => {
  return isDirectMember(member) && member.canRemove;
};

export const canResend = (member) => {
  return Boolean(member.invite?.canResend);
};

export const canUpdate = (member, currentUserId) => {
  return !isCurrentUser(member, currentUserId) && isDirectMember(member) && member.canUpdate;
};

export const parseSortParam = (sortableFields) => {
  const sortParam = getParameterByName('sort');

  const sortedField = FIELDS.filter((field) => sortableFields.includes(field.key)).find(
    (field) => field.sort?.asc === sortParam || field.sort?.desc === sortParam,
  );

  if (!sortedField) {
    return DEFAULT_SORT;
  }

  return {
    sortByKey: sortedField.key,
    sortDesc: sortedField?.sort?.desc === sortParam,
  };
};

export const buildSortHref = ({
  sortBy,
  sortDesc,
  filteredSearchBarTokens,
  filteredSearchBarSearchParam,
}) => {
  const sortDefinition = FIELDS.find((field) => field.key === sortBy)?.sort;

  if (!sortDefinition) {
    return '';
  }

  const sortParam = sortDesc ? sortDefinition.desc : sortDefinition.asc;

  const filterParams =
    filteredSearchBarTokens?.reduce((accumulator, token) => {
      return {
        ...accumulator,
        [token]: getParameterByName(token),
      };
    }, {}) || {};

  if (filteredSearchBarSearchParam) {
    filterParams[filteredSearchBarSearchParam] = getParameterByName(filteredSearchBarSearchParam);
  }

  return setUrlParams({ ...filterParams, sort: sortParam }, window.location.href, true);
};

// Defined in `ee/app/assets/javascripts/vue_shared/components/members/utils.js`
export const canOverride = () => false;

export const parseDataAttributes = (el) => {
  const { members, sourceId, memberPath, canManageMembers } = el.dataset;

  return {
    members: convertObjectPropsToCamelCase(JSON.parse(members), { deep: true }),
    sourceId: parseInt(sourceId, 10),
    memberPath,
    canManageMembers: parseBoolean(canManageMembers),
  };
};

export const baseRequestFormatter = (basePropertyName, accessLevelPropertyName) => ({
  accessLevel,
  ...otherProperties
}) => {
  const accessLevelProperty = !isUndefined(accessLevel)
    ? { [accessLevelPropertyName]: accessLevel }
    : {};

  return {
    [basePropertyName]: {
      ...accessLevelProperty,
      ...otherProperties,
    },
  };
};

export const groupLinkRequestFormatter = baseRequestFormatter(
  GROUP_LINK_BASE_PROPERTY_NAME,
  GROUP_LINK_ACCESS_LEVEL_PROPERTY_NAME,
);