Skip to content

Commit

Permalink
feat: filter memberships for same member to render highest permission…
Browse files Browse the repository at this point in the history
… only (#936)
  • Loading branch information
LinaYahya authored Jan 22, 2024
1 parent 5492143 commit 8cf22c2
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/components/item/sharing/ItemSharingTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Typography from '@mui/material/Typography';
import {
DiscriminatedItem,
ItemMembership,
PermissionLevelCompare,
isPseudonymizedMember,
} from '@graasp/sdk';
import { Loader } from '@graasp/ui';
Expand All @@ -32,6 +33,27 @@ type Props = {
item: DiscriminatedItem;
memberships?: ItemMembership[];
};
interface PermissionMap {
[key: string]: ItemMembership;
}

const selectHighestMemberships = (
memberships: ItemMembership[],
): ItemMembership[] => {
const permissionMap = memberships.reduce<PermissionMap>((acc, curr) => {
const { member, permission } = curr;

if (
!acc[member.id] ||
PermissionLevelCompare.gt(permission, acc[member.id].permission)
) {
acc[member.id] = curr;
}
return acc;
}, {});

return Object.values(permissionMap);
};

const ItemSharingTab = ({ item, memberships }: Props): JSX.Element => {
const { t: translateBuilder } = useBuilderTranslation();
Expand Down Expand Up @@ -89,7 +111,7 @@ const ItemSharingTab = ({ item, memberships }: Props): JSX.Element => {
emptyMessage={translateBuilder(
BUILDER.SHARING_AUTHORIZED_MEMBERS_EMPTY_MESSAGE,
)}
memberships={authorizedMemberships}
memberships={selectHighestMemberships(authorizedMemberships)}
readOnly={!canEditSettings}
/>

Expand All @@ -104,7 +126,7 @@ const ItemSharingTab = ({ item, memberships }: Props): JSX.Element => {
</Typography>
<ItemMembershipsTable
item={item}
memberships={authenticatedMemberships}
memberships={selectHighestMemberships(authenticatedMemberships)}
emptyMessage={translateBuilder(
BUILDER.SHARING_AUTHENTICATED_MEMBERS_EMPTY_MESSAGE,
)}
Expand Down

0 comments on commit 8cf22c2

Please sign in to comment.