-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add a confirmation message when deleting membership (#790)
- Loading branch information
Showing
9 changed files
with
185 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { | ||
Alert, | ||
Dialog, | ||
DialogActions, | ||
DialogContent, | ||
DialogContentText, | ||
DialogTitle, | ||
} from '@mui/material'; | ||
|
||
import { ItemMembership, PermissionLevel } from '@graasp/sdk'; | ||
import { ItemRecord } from '@graasp/sdk/frontend'; | ||
import { Button } from '@graasp/ui'; | ||
|
||
import { useCurrentUserContext } from '@/components/context/CurrentUserContext'; | ||
|
||
import { useBuilderTranslation } from '../../../config/i18n'; | ||
import { mutations } from '../../../config/queryClient'; | ||
import { CONFIRM_MEMBERSHIP_DELETE_BUTTON_ID } from '../../../config/selectors'; | ||
import { BUILDER } from '../../../langs/constants'; | ||
import CancelButton from '../../common/CancelButton'; | ||
|
||
const labelId = 'alert-dialog-title'; | ||
const descriptionId = 'alert-dialog-description'; | ||
|
||
type Props = { | ||
open?: boolean; | ||
handleClose: () => void; | ||
item: ItemRecord; | ||
membershipToDelete: ItemMembership | null; | ||
hasOnlyOneAdmin: boolean; | ||
}; | ||
|
||
const DeleteItemDialog = ({ | ||
item, | ||
open = false, | ||
handleClose, | ||
membershipToDelete, | ||
hasOnlyOneAdmin = false, | ||
}: Props): JSX.Element => { | ||
const { t: translateBuilder } = useBuilderTranslation(); | ||
const { data: member } = useCurrentUserContext(); | ||
|
||
const { mutate: deleteItemMembership } = mutations.useDeleteItemMembership(); | ||
|
||
const onDelete = () => { | ||
if (membershipToDelete?.id) { | ||
deleteItemMembership({ id: membershipToDelete.id, itemId: item.id }); | ||
handleClose(); | ||
} | ||
}; | ||
|
||
let dialogText = ''; | ||
const isDeletingLastAdmin = | ||
hasOnlyOneAdmin && membershipToDelete?.permission === PermissionLevel.Admin; | ||
// incase of deleting the only admin | ||
if (isDeletingLastAdmin) { | ||
dialogText = translateBuilder(BUILDER.DELETE_LAST_ADMIN_ALERT_MESSAGE); | ||
} else if (member?.id === membershipToDelete?.member?.id) { | ||
// deleting yourself | ||
dialogText = translateBuilder(BUILDER.DELETE_OWN_MEMBERSHIP_MESSAGE); | ||
} else { | ||
// delete other members | ||
dialogText = translateBuilder(BUILDER.DELETE_MEMBERSHIP_MESSAGE, { | ||
name: membershipToDelete?.member.name, | ||
permissionLevel: membershipToDelete?.permission, | ||
}); | ||
} | ||
return ( | ||
<Dialog | ||
open={open} | ||
onClose={handleClose} | ||
aria-labelledby={labelId} | ||
aria-describedby={descriptionId} | ||
> | ||
<DialogTitle id={labelId}> | ||
{translateBuilder(BUILDER.DELETE_MEMBERSHIP)} | ||
</DialogTitle> | ||
<DialogContent> | ||
{isDeletingLastAdmin ? ( | ||
<Alert severity="error">{dialogText}</Alert> | ||
) : ( | ||
<DialogContentText id={descriptionId}>{dialogText}</DialogContentText> | ||
)} | ||
</DialogContent> | ||
|
||
<DialogActions> | ||
{isDeletingLastAdmin ? ( | ||
<Button onClick={handleClose} autoFocus variant="text"> | ||
{translateBuilder(BUILDER.APPROVE_BUTTON_TEXT)} | ||
</Button> | ||
) : ( | ||
<> | ||
<CancelButton onClick={handleClose} /> | ||
<Button | ||
id={CONFIRM_MEMBERSHIP_DELETE_BUTTON_ID} | ||
onClick={onDelete} | ||
color="error" | ||
autoFocus | ||
variant="text" | ||
> | ||
{translateBuilder(BUILDER.DELETE_MEMBERSHIP_MODAL_CONFIRM_BUTTON)} | ||
</Button> | ||
</> | ||
)} | ||
</DialogActions> | ||
</Dialog> | ||
); | ||
}; | ||
|
||
export default DeleteItemDialog; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters