Skip to content

Commit

Permalink
fix: fix public items memberships
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia committed Nov 5, 2021
1 parent d43d984 commit 07d6b3d
Show file tree
Hide file tree
Showing 14 changed files with 826 additions and 895 deletions.
7 changes: 0 additions & 7 deletions cypress/fixtures/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,6 @@ export const SAMPLE_PUBLIC_ITEMS = {
itemPath: 'egafbd2a_5688_11eb_ae93_0242ac130002',
},
],
memberships: [
{
itemPath: 'fdf09f5a_5688_11eb_ae93_0242ac130002',
permission: PERMISSION_LEVELS.ADMIN,
memberId: MEMBERS.ANNA.id,
},
],
},
{
...DEFAULT_FOLDER_ITEM,
Expand Down
2 changes: 1 addition & 1 deletion cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Cypress.Commands.add(

mockPutItemLogin(items, putItemLoginError);

mockGetItemMembershipsForItem(items);
mockGetItemMembershipsForItem(items, currentMember);

mockGetTags(tags);

Expand Down
35 changes: 26 additions & 9 deletions cypress/support/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ export const mockGetItemLogin = (items) => {
).as('getItemLogin');
};

export const mockGetItemMembershipsForItem = (items) => {
export const mockGetItemMembershipsForItem = (items, currentMember) => {
cy.intercept(
{
method: DEFAULT_GET.method,
Expand All @@ -910,14 +910,31 @@ export const mockGetItemMembershipsForItem = (items) => {
const { itemId } = qs.parse(url.slice(url.indexOf('?') + 1));
const selectedItems = items.filter(({ id }) => itemId.includes(id));
const allMemberships = selectedItems.map(
({ creator, id, memberships }) =>
memberships || [
{
permission: PERMISSION_LEVELS.ADMIN,
memberId: creator,
itemId: id,
},
],
({ creator, id, memberships }) => {
// build default membership depending on current member
// if the current member is the creator, it has membership
// otherwise it should return an error
const defaultMembership =
creator === currentMember?.id
? [
{
permission: PERMISSION_LEVELS.ADMIN,
memberId: creator,
itemId: id,
},
]
: { statusCode: StatusCodes.UNAUTHORIZED };

// if the defined memberships does not contain currentMember, it should throw
const currentMemberHasMembership = memberships?.find(
({ memberId }) => memberId === currentMember?.id,
);
if (!currentMemberHasMembership) {
return defaultMembership;
}

return memberships || defaultMembership;
},
);
reply(allMemberships);
},
Expand Down
6 changes: 3 additions & 3 deletions src/components/item/ItemMemberships.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { hooks } from '../../config/queryClient';
import MemberAvatar from '../common/MemberAvatar';
import { PERMISSION_LEVELS } from '../../enums';
import { ITEM_MEMBERSHIPS_CONTENT_ID } from '../../config/selectors';
import { membershipsWithoutUser } from '../../utils/membership';
import { getMembership, membershipsWithoutUser } from '../../utils/membership';
import { CurrentUserContext } from '../context/CurrentUserContext';

const useStyles = makeStyles({
Expand All @@ -36,12 +36,12 @@ const ItemMemberships = ({ id, maxAvatar, onClick }) => {
}

const filteredMemberships = membershipsWithoutUser(
memberships?.get(0),
getMembership(memberships),
currentUser?.get('id'),
);

// display only if has more than 2 memberships
if (!filteredMemberships.length) {
if (!filteredMemberships?.length) {
return null;
}

Expand Down
1 change: 1 addition & 0 deletions src/components/item/ItemSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ const useItemSearch = (items) => {

const itemSearchInput = (
<ItemSearchInput
key="searchInput"
searchInputHandler={handleSearchInput}
searchTextState={searchText}
/>
Expand Down
7 changes: 5 additions & 2 deletions src/components/item/header/ItemHeaderActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import ShareButton from '../../common/ShareButton';
import { ITEM_TYPES_WITH_CAPTIONS } from '../../../config/constants';
import ItemSettingsButton from '../settings/ItemSettingsButton';
import PerformViewButton from '../../common/PerformViewButton';
import { isItemUpdateAllowedForUser } from '../../../utils/membership';
import {
getMembership,
isItemUpdateAllowedForUser,
} from '../../../utils/membership';
import { hooks } from '../../../config/queryClient';
import { CurrentUserContext } from '../../context/CurrentUserContext';

Expand Down Expand Up @@ -56,7 +59,7 @@ const ItemHeaderActions = ({ onClickMetadata, onClickChatbox, item }) => {

const { data: memberships } = useItemMemberships([id]);
const canEdit = isItemUpdateAllowedForUser({
memberships: memberships?.get(0),
memberships: getMembership(memberships),
memberId: member?.get('id'),
});

Expand Down
13 changes: 7 additions & 6 deletions src/components/item/sharing/ItemSharingTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ const ItemSharingTab = ({ item, memberships }) => {
const { data: currentMember, isLoadingCurrentMember } = useContext(
CurrentUserContext,
);
const canEdit = isItemUpdateAllowedForUser({
memberships,
memberId: currentMember?.get('id'),
});
const { data: members } = hooks.useMembers(
memberships?.map(({ memberId }) => memberId),
);
const { setIsItemSharingOpen } = useContext(LayoutContext);

const canEdit = isItemUpdateAllowedForUser({
memberships,
memberId: currentMember?.get('id'),
});

useEffect(
() => () => {
setIsItemSharingOpen(false);
Expand All @@ -70,14 +71,14 @@ const ItemSharingTab = ({ item, memberships }) => {
memberships,
currentMember.get('id'),
);
const authorizedMemberships = membershipsWithoutSelf.filter(
const authorizedMemberships = membershipsWithoutSelf?.filter(
({ memberId }) => {
const member = members?.find(({ id: mId }) => mId === memberId);
return !member?.email?.includes(PSEUDONIMIZED_USER_MAIL);
},
);

const authenticatedMemberships = membershipsWithoutSelf.filter(
const authenticatedMemberships = membershipsWithoutSelf?.filter(
({ memberId }) => {
const member = members?.find(({ id: mId }) => mId === memberId);
return member?.email?.includes(PSEUDONIMIZED_USER_MAIL);
Expand Down
2 changes: 1 addition & 1 deletion src/components/main/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const Home = () => {
id={OWNED_ITEMS_ID}
title={t('My Items')}
items={ownItems}
headerElements={[<NewItemButton fontSize="small" />]}
headerElements={[<NewItemButton key="newButton" fontSize="small" />]}
/>
</Main>
);
Expand Down
18 changes: 12 additions & 6 deletions src/components/main/ItemScreen.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import React, { useContext, useEffect } from 'react';
import { useParams } from 'react-router';
import { hooks } from '../../config/queryClient';
import { isItemUpdateAllowedForUser } from '../../utils/membership';
import {
getMembership,
isItemUpdateAllowedForUser,
} from '../../utils/membership';
import ErrorAlert from '../common/ErrorAlert';
import { CurrentUserContext } from '../context/CurrentUserContext';
import { LayoutContext } from '../context/LayoutContext';
Expand All @@ -26,10 +29,6 @@ const ItemScreen = () => {
} = useContext(LayoutContext);
const { data: currentMember } = useContext(CurrentUserContext);
const { data: memberships } = useItemMemberships([itemId]);
const enableEdition = isItemUpdateAllowedForUser({
memberships: memberships?.get(0),
memberId: currentMember?.get('id'),
});

useEffect(() => {
setEditingItemId(null);
Expand All @@ -42,12 +41,19 @@ const ItemScreen = () => {
return <ErrorAlert />;
}

const enableEdition = isItemUpdateAllowedForUser({
memberships: getMembership(memberships),
memberId: currentMember?.get('id'),
});

const content = (() => {
if (enableEdition && isItemSettingsOpen) {
return <ItemSettings item={item} />;
}
if (isItemSharingOpen) {
return <ItemSharingTab item={item} memberships={memberships?.get(0)} />;
return (
<ItemSharingTab item={item} memberships={getMembership(memberships)} />
);
}
return <ItemContent item={item} enableEdition={enableEdition} />;
})();
Expand Down
6 changes: 2 additions & 4 deletions src/components/main/Items.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const Items = ({
clickable,
defautSortedColumn,
isEditing,
onSaveCaption,
}) => {
const { mode } = useContext(LayoutContext);
const itemSearch = useItemSearch(items);
Expand Down Expand Up @@ -65,7 +64,6 @@ const Items = ({
toolbarActions={toolbarActions}
clickable={clickable}
isEditing={isEditing}
onSaveCaption={onSaveCaption}
/>
);
}
Expand All @@ -85,8 +83,7 @@ Items.propTypes = {
type: PropTypes.string,
name: PropTypes.string,
}),
isEditing: PropTypes.bool.isRequired,
onSaveCaption: PropTypes.func.isRequired,
isEditing: PropTypes.bool,
};

Items.defaultProps = {
Expand All @@ -96,6 +93,7 @@ Items.defaultProps = {
toolbarActions: null,
clickable: true,
defautSortedColumn: {},
isEditing: false,
};

export default Items;
9 changes: 7 additions & 2 deletions src/components/main/TableToolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ DefaultActions.propTypes = {
selectedIds: PropTypes.arrayOf(PropTypes.string).isRequired,
};

const TableToolbar = (props) => {
const TableToolbar = ({
numSelected,
tableTitle,
selected,
headerElements,
actions,
}) => {
const classes = useToolbarStyles();
const { t } = useTranslation();
const { numSelected, tableTitle, selected, headerElements, actions } = props;
const renderActions = actions ?? DefaultActions;

return (
Expand Down
Loading

0 comments on commit 07d6b3d

Please sign in to comment.