2021-01-21 12:09:05 +00:00
|
|
|
import { isUndefined } from 'lodash';
|
2021-07-09 09:09:53 +00:00
|
|
|
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
|
|
|
|
import { getParameterByName, setUrlParams } from '~/lib/utils/url_utility';
|
2021-02-14 18:09:20 +00:00
|
|
|
import { __ } from '~/locale';
|
2021-01-21 12:09:05 +00:00
|
|
|
import {
|
|
|
|
FIELDS,
|
|
|
|
DEFAULT_SORT,
|
|
|
|
GROUP_LINK_BASE_PROPERTY_NAME,
|
|
|
|
GROUP_LINK_ACCESS_LEVEL_PROPERTY_NAME,
|
|
|
|
} from './constants';
|
2020-10-01 06:09:59 +00:00
|
|
|
|
2021-03-01 00:10:57 +00:00
|
|
|
export const generateBadges = ({ member, isCurrentUser, canManageMembers }) => [
|
2020-10-01 06:09:59 +00:00
|
|
|
{
|
|
|
|
show: isCurrentUser,
|
|
|
|
text: __("It's you"),
|
|
|
|
variant: 'success',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
show: member.user?.blocked,
|
|
|
|
text: __('Blocked'),
|
|
|
|
variant: 'danger',
|
|
|
|
},
|
|
|
|
{
|
2021-03-01 00:10:57 +00:00
|
|
|
show: member.user?.twoFactorEnabled && (canManageMembers || isCurrentUser),
|
2020-10-01 06:09:59 +00:00
|
|
|
text: __('2FA'),
|
|
|
|
variant: 'info',
|
|
|
|
},
|
|
|
|
];
|
2020-11-03 18:09:22 +00:00
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
export const isGroup = (member) => {
|
2020-11-03 18:09:22 +00:00
|
|
|
return Boolean(member.sharedWithGroup);
|
|
|
|
};
|
|
|
|
|
2021-01-28 18:09:27 +00:00
|
|
|
export const isDirectMember = (member) => {
|
|
|
|
return isGroup(member) || member.isDirectMember;
|
2020-11-03 18:09:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const isCurrentUser = (member, currentUserId) => {
|
|
|
|
return member.user?.id === currentUserId;
|
|
|
|
};
|
|
|
|
|
2021-01-28 18:09:27 +00:00
|
|
|
export const canRemove = (member) => {
|
|
|
|
return isDirectMember(member) && member.canRemove;
|
2020-11-03 18:09:22 +00:00
|
|
|
};
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
export const canResend = (member) => {
|
2020-11-03 18:09:22 +00:00
|
|
|
return Boolean(member.invite?.canResend);
|
|
|
|
};
|
|
|
|
|
2021-01-28 18:09:27 +00:00
|
|
|
export const canUpdate = (member, currentUserId) => {
|
|
|
|
return !isCurrentUser(member, currentUserId) && isDirectMember(member) && member.canUpdate;
|
2020-11-03 18:09:22 +00:00
|
|
|
};
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
export const parseSortParam = (sortableFields) => {
|
2020-12-08 09:09:41 +00:00
|
|
|
const sortParam = getParameterByName('sort');
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
const sortedField = FIELDS.filter((field) => sortableFields.includes(field.key)).find(
|
|
|
|
(field) => field.sort?.asc === sortParam || field.sort?.desc === sortParam,
|
2020-12-08 09:09:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!sortedField) {
|
|
|
|
return DEFAULT_SORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2020-12-10 18:10:16 +00:00
|
|
|
sortByKey: sortedField.key,
|
|
|
|
sortDesc: sortedField?.sort?.desc === sortParam,
|
2020-12-08 09:09:41 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-12-10 18:10:16 +00:00
|
|
|
export const buildSortHref = ({
|
2020-12-08 09:09:41 +00:00
|
|
|
sortBy,
|
|
|
|
sortDesc,
|
|
|
|
filteredSearchBarTokens,
|
|
|
|
filteredSearchBarSearchParam,
|
|
|
|
}) => {
|
2020-12-23 21:10:24 +00:00
|
|
|
const sortDefinition = FIELDS.find((field) => field.key === sortBy)?.sort;
|
2020-12-08 09:09:41 +00:00
|
|
|
|
|
|
|
if (!sortDefinition) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2020-12-10 18:10:16 +00:00
|
|
|
const sortParam = sortDesc ? sortDefinition.desc : sortDefinition.asc;
|
2020-12-08 09:09:41 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2020-11-03 18:09:22 +00:00
|
|
|
// Defined in `ee/app/assets/javascripts/vue_shared/components/members/utils.js`
|
|
|
|
export const canOverride = () => false;
|
2021-01-21 12:09:05 +00:00
|
|
|
|
|
|
|
export const parseDataAttributes = (el) => {
|
2021-05-11 18:10:36 +00:00
|
|
|
const { membersData } = el.dataset;
|
2021-01-21 12:09:05 +00:00
|
|
|
|
2021-05-11 18:10:36 +00:00
|
|
|
return convertObjectPropsToCamelCase(JSON.parse(membersData), {
|
|
|
|
deep: true,
|
|
|
|
ignoreKeyNames: ['params'],
|
|
|
|
});
|
2021-01-21 12:09:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
);
|