Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: disable options for downgrading the only admin #931

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@graasp/query-client": "2.2.1",
"@graasp/sdk": "3.3.0",
"@graasp/translations": "1.21.1",
"@graasp/ui": "4.2.0",
"@graasp/ui": "https://github.com/graasp/graasp-ui#add-disabled-to-select",
"@mui/icons-material": "5.14.19",
"@mui/lab": "5.0.0-alpha.151",
"@mui/material": "5.14.19",
Expand Down
25 changes: 24 additions & 1 deletion src/components/item/sharing/ItemMembershipSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,31 @@ import {
} from '../../../config/selectors';
import { BUILDER } from '../../../langs/constants';

/**
* Type of the `disabled` prop which allows to disable a subset of the options.
* Undefined keys are considered as not disabled.
*/
type DisabledMap = {
[key in PermissionLevel]?: boolean;
};

const defaultDisabledMap: DisabledMap = {
admin: false,
read: false,
write: false,
};

export type ItemMembershipSelectProps = {
value: string;
onChange?: SelectProps['onChange'];
color?: SelectProps['color'];
showLabel?: boolean;
displayEmpty?: boolean;
/**
* This prop allows to disable the select when passed the value `true`
* or to disable only certain options when passed an object where the keys are the values of the options
*/
disabled?: boolean | DisabledMap;
};

const ItemMembershipSelect = ({
Expand All @@ -29,14 +48,16 @@ const ItemMembershipSelect = ({
color,
showLabel = true,
displayEmpty = false,
disabled = false,
}: ItemMembershipSelectProps): JSX.Element => {
const { t: translateBuilder } = useBuilderTranslation();
const { t: enumT } = useEnumsTranslation();
const [permission, setPermission] = useState(value);
const label = showLabel
? translateBuilder(BUILDER.ITEM_MEMBERSHIP_PERMISSION_LABEL)
: undefined;

const disabledMap =
typeof disabled === 'boolean' ? defaultDisabledMap : disabled;
useEffect(() => {
if (permission !== value) {
setPermission(value);
Expand All @@ -50,7 +71,9 @@ const ItemMembershipSelect = ({
values={Object.values(PermissionLevel).map((v) => ({
value: v,
text: enumT(v),
disabled: disabledMap[v],
}))}
disabled={typeof disabled === 'boolean' ? disabled : undefined}
buildOptionId={buildPermissionOptionId}
value={permission}
defaultValue={permission}
Expand Down
2 changes: 1 addition & 1 deletion src/components/item/sharing/ItemMembershipsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ const ItemMembershipsTable = ({
},
]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [item, showEmail, readOnly]);
}, [item, showEmail, readOnly, memberships]);

const countTextFunction = (selected: string[]) =>
translateBuilder(BUILDER.ITEMS_TABLE_SELECTION_TEXT, {
Expand Down
9 changes: 7 additions & 2 deletions src/components/item/sharing/TableRowPermissionRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ function TableRowPermissionRenderer<
const handleClose = () => {
setOpen(false);
};
const isEditingOwnPermission =
instance?.member && instance?.member?.id === currentMember?.id;

const isEditingTheOnlyAdmin = Boolean(
hasOnlyOneAdmin && instance.permission === PermissionLevel.Admin,
);
const isParentMembership = useIsParentInstance({
instance,
item,
Expand All @@ -74,8 +79,7 @@ function TableRowPermissionRenderer<
if (
(value === PermissionLevel.Read || value === PermissionLevel.Write) &&
instance.permission === PermissionLevel.Admin &&
instance?.member &&
instance?.member?.id === currentMember?.id
isEditingOwnPermission
) {
setOpen(true);
} else {
Expand All @@ -90,6 +94,7 @@ function TableRowPermissionRenderer<
<ItemMembershipSelect
value={instance.permission}
showLabel={false}
disabled={isEditingTheOnlyAdmin}
onChange={onChangePermission}
/>
<Dialog
Expand Down
Loading