-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to transfer organization ownership
Fixes: #58
- Loading branch information
1 parent
11afa83
commit 4dab464
Showing
4 changed files
with
132 additions
and
6 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
80 changes: 80 additions & 0 deletions
80
apps/client/src/components/Organization/OrganizationMemberDropdown/ConfirmTransferModal.tsx
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,80 @@ | ||
import axios from 'axios'; | ||
import { useState } from 'react'; | ||
import { useQueryClient } from 'react-query'; | ||
import { | ||
IDeleteSecurityRuleDto, | ||
ITransferOrganizationDto, | ||
OrganizationDto, | ||
SecurityRuleDto, | ||
} from 'shared-types'; | ||
import useUserData from '../../../hooks/useUserData'; | ||
import { Utils } from '../../../utils/utils'; | ||
import Button from '../../Button'; | ||
import Alert from '../../Helpers/Alert'; | ||
import ModalBody from '../../Modal/ModalBody'; | ||
import ModalCloseButton from '../../Modal/ModalCloseButton'; | ||
import ModalDialog, { ModalDialogProps } from '../../Modal/ModalDialog'; | ||
import ModalHeader from '../../Modal/ModalHeader'; | ||
import ModalTitle from '../../Modal/ModalTitle'; | ||
import ModelFooter from '../../Modal/ModelFooter'; | ||
import { BsArrowRight } from 'react-icons/bs'; | ||
import IconButton from '../../IconButton'; | ||
import toast from 'react-hot-toast'; | ||
|
||
export interface ConfirmTransferModalProps extends ModalDialogProps { | ||
organization: OrganizationDto; | ||
rule: SecurityRuleDto; | ||
} | ||
|
||
function ConfirmTransferModal({ organization, rule, ...props }: ConfirmTransferModalProps) { | ||
const { user } = useUserData(rule.user); | ||
const queryClient = useQueryClient(); | ||
|
||
const [loading, setLoading] = useState(false); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
function handleClick() { | ||
setLoading(true); | ||
setError(null); | ||
|
||
const dto: ITransferOrganizationDto = { user: rule.user }; | ||
axios | ||
.post(`/api/security/${organization.id}/transfer`, dto) | ||
.then(() => { | ||
queryClient.invalidateQueries(['security', organization.id]); | ||
toast.success(`Transfered organization to ${user?.username}`); | ||
if (!props.handleClose) return; | ||
props.handleClose(); | ||
}) | ||
.catch((err) => setError(Utils.requestErrorToString(err))) | ||
.finally(() => setLoading(false)); | ||
} | ||
|
||
return ( | ||
<ModalDialog {...props}> | ||
<ModalHeader closeButton> | ||
<ModalTitle>Transfer organization ownership</ModalTitle> | ||
</ModalHeader> | ||
|
||
<ModalBody> | ||
{error && <Alert>{error}</Alert>} | ||
You are about to transfer ownership of this organization ({organization.name}) to{' '} | ||
{user?.username || '...'}. You will immediately lose owner access, this action is not | ||
reversible. Are you sure? | ||
</ModalBody> | ||
|
||
<ModelFooter> | ||
<IconButton | ||
icon={BsArrowRight} | ||
variant="danger" | ||
loading={loading} | ||
onClick={handleClick} | ||
> | ||
Transfer | ||
</IconButton> | ||
<ModalCloseButton /> | ||
</ModelFooter> | ||
</ModalDialog> | ||
); | ||
} | ||
export default ConfirmTransferModal; |
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
45 changes: 45 additions & 0 deletions
45
...client/src/components/Organization/OrganizationMemberDropdown/TransferOwnershipOption.tsx
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,45 @@ | ||
import { useState } from 'react'; | ||
import { createPortal } from 'react-dom'; | ||
import { BsArrowRight, BsPersonXFill } from 'react-icons/bs'; | ||
import { OrganizationDto, OrganizationSecurityRole, SecurityRuleDto } from 'shared-types'; | ||
import useUserRole from '../../../hooks/useUserRole'; | ||
import { SecurityUtils } from '../../../utils/securityUtils'; | ||
import { ActionButtonProps } from '../../ActionButton'; | ||
import DropdownItem from '../../Dropdown/DropdownItem'; | ||
import ConfirmMemberDeleteModal from './ConfirmMemberDeleteModal'; | ||
import ConfirmTransferModal from './ConfirmTransferModal'; | ||
|
||
export interface TransferOwnershipOptionProps extends Omit<ActionButtonProps, 'icon' | 'onClick'> { | ||
organization: OrganizationDto; | ||
rule: SecurityRuleDto; | ||
} | ||
|
||
function TransferOwnershipOption({ rule, organization, ...props }: TransferOwnershipOptionProps) { | ||
const [open, setOpen] = useState(false); | ||
|
||
const { role } = useUserRole(organization.id); | ||
const isOwner = role == OrganizationSecurityRole.OWNER; | ||
|
||
return ( | ||
<> | ||
{createPortal( | ||
<ConfirmTransferModal | ||
open={open} | ||
handleClose={() => setOpen(false)} | ||
organization={organization} | ||
rule={rule} | ||
/>, | ||
document.body, | ||
)} | ||
<DropdownItem | ||
icon={BsArrowRight} | ||
title="Transfer ownership" | ||
disabled={!isOwner || props.disabled} | ||
onClick={() => setOpen(true)} | ||
> | ||
Transfer ownership | ||
</DropdownItem> | ||
</> | ||
); | ||
} | ||
export default TransferOwnershipOption; |