debian-mirror-gitlab/app/assets/javascripts/members/utils.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

143 lines
3.5 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { isUndefined } from 'lodash';
2021-09-30 23:02:18 +05:30
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import { getParameterByName, setUrlParams } from '~/lib/utils/url_utility';
2021-03-11 19:13:27 +05:30
import {
FIELDS,
DEFAULT_SORT,
GROUP_LINK_BASE_PROPERTY_NAME,
GROUP_LINK_ACCESS_LEVEL_PROPERTY_NAME,
2022-10-11 01:57:18 +05:30
I18N_USER_YOU,
I18N_USER_BLOCKED,
I18N_USER_BOT,
I188N_USER_2FA,
2021-03-11 19:13:27 +05:30
} from './constants';
2021-02-22 17:27:13 +05:30
2021-04-17 20:07:23 +05:30
export const generateBadges = ({ member, isCurrentUser, canManageMembers }) => [
2021-02-22 17:27:13 +05:30
{
show: isCurrentUser,
2022-10-11 01:57:18 +05:30
text: I18N_USER_YOU,
2021-02-22 17:27:13 +05:30
variant: 'success',
},
{
show: member.user?.blocked,
2022-10-11 01:57:18 +05:30
text: I18N_USER_BLOCKED,
2021-02-22 17:27:13 +05:30
variant: 'danger',
},
2022-10-11 01:57:18 +05:30
{
show: member.user?.isBot,
text: I18N_USER_BOT,
variant: 'muted',
},
2021-02-22 17:27:13 +05:30
{
2021-04-17 20:07:23 +05:30
show: member.user?.twoFactorEnabled && (canManageMembers || isCurrentUser),
2022-10-11 01:57:18 +05:30
text: I188N_USER_2FA,
2021-02-22 17:27:13 +05:30
variant: 'info',
},
];
2021-03-08 18:12:59 +05:30
export const isGroup = (member) => {
2021-02-22 17:27:13 +05:30
return Boolean(member.sharedWithGroup);
};
2021-03-11 19:13:27 +05:30
export const isDirectMember = (member) => {
2022-06-21 17:19:12 +05:30
return member.isDirectMember;
2021-02-22 17:27:13 +05:30
};
export const isCurrentUser = (member, currentUserId) => {
return member.user?.id === currentUserId;
};
2021-03-11 19:13:27 +05:30
export const canRemove = (member) => {
return isDirectMember(member) && member.canRemove;
2021-02-22 17:27:13 +05:30
};
2021-03-08 18:12:59 +05:30
export const canResend = (member) => {
2021-02-22 17:27:13 +05:30
return Boolean(member.invite?.canResend);
};
2021-03-11 19:13:27 +05:30
export const canUpdate = (member, currentUserId) => {
return !isCurrentUser(member, currentUserId) && isDirectMember(member) && member.canUpdate;
2021-02-22 17:27:13 +05:30
};
2021-03-08 18:12:59 +05:30
export const parseSortParam = (sortableFields) => {
2021-02-22 17:27:13 +05:30
const sortParam = getParameterByName('sort');
2021-03-08 18:12:59 +05:30
const sortedField = FIELDS.filter((field) => sortableFields.includes(field.key)).find(
(field) => field.sort?.asc === sortParam || field.sort?.desc === sortParam,
2021-02-22 17:27:13 +05:30
);
if (!sortedField) {
return DEFAULT_SORT;
}
return {
sortByKey: sortedField.key,
sortDesc: sortedField?.sort?.desc === sortParam,
};
};
export const buildSortHref = ({
sortBy,
sortDesc,
filteredSearchBarTokens,
filteredSearchBarSearchParam,
}) => {
2021-03-08 18:12:59 +05:30
const sortDefinition = FIELDS.find((field) => field.key === sortBy)?.sort;
2021-02-22 17:27:13 +05:30
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);
};
2022-11-25 23:54:43 +05:30
// Defined in `ee/app/assets/javascripts/members/utils.js`
2021-02-22 17:27:13 +05:30
export const canOverride = () => false;
2021-03-11 19:13:27 +05:30
2022-11-25 23:54:43 +05:30
// Defined in `ee/app/assets/javascripts/members/utils.js`
export const canUnban = () => false;
2021-03-11 19:13:27 +05:30
export const parseDataAttributes = (el) => {
2021-06-08 01:23:25 +05:30
const { membersData } = el.dataset;
2021-03-11 19:13:27 +05:30
2021-06-08 01:23:25 +05:30
return convertObjectPropsToCamelCase(JSON.parse(membersData), {
deep: true,
ignoreKeyNames: ['params'],
});
2021-03-11 19:13:27 +05:30
};
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,
);