-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨(frontend) disable mailbox and allow to create pending mailbox
Allow to disable or enable mailboxes. And enable mailbox creation button when domain is pending to allow creation of mailboxes in pending status.
- Loading branch information
1 parent
bcdc579
commit 4e43346
Showing
7 changed files
with
281 additions
and
48 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
48 changes: 48 additions & 0 deletions
48
src/frontend/apps/desk/src/features/mail-domains/mailboxes/api/useUpdateMailboxStatus.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,48 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
|
||
import { APIError, errorCauses, fetchAPI } from '@/api'; | ||
|
||
import { KEY_LIST_MAILBOX } from './useMailboxes'; | ||
|
||
export interface DisableMailboxParams { | ||
mailDomainSlug: string; | ||
mailboxId: string; | ||
isEnabled: boolean; | ||
} | ||
|
||
export const disableMailbox = async ({ | ||
mailDomainSlug, | ||
mailboxId, | ||
isEnabled, | ||
}: DisableMailboxParams): Promise<void> => { | ||
const response = await fetchAPI( | ||
`mail-domains/${mailDomainSlug}/mailboxes/${mailboxId}/${ | ||
isEnabled ? 'enable' : 'disable' | ||
}/`, | ||
{ | ||
method: 'POST', | ||
}, | ||
); | ||
|
||
if (!response.ok) { | ||
throw new APIError( | ||
'Failed to disable the mailbox', | ||
await errorCauses(response), | ||
); | ||
} | ||
}; | ||
|
||
export const useUpdateMailboxStatus = () => { | ||
const queryClient = useQueryClient(); | ||
return useMutation<void, APIError, DisableMailboxParams>({ | ||
mutationFn: disableMailbox, | ||
onSuccess: (_data, variables) => { | ||
void queryClient.invalidateQueries({ | ||
queryKey: [ | ||
KEY_LIST_MAILBOX, | ||
{ mailDomainSlug: variables.mailDomainSlug }, | ||
], | ||
}); | ||
}, | ||
}); | ||
}; |
115 changes: 115 additions & 0 deletions
115
src/frontend/apps/desk/src/features/mail-domains/mailboxes/components/MailDomainsActions.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,115 @@ | ||
import { | ||
Button, | ||
Modal, | ||
ModalSize, | ||
VariantType, | ||
useModal, | ||
useToastProvider, | ||
} from '@openfun/cunningham-react'; | ||
import { useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { Box, DropButton, IconOptions, Text } from '@/components'; | ||
|
||
import { MailDomain } from '../../domains/types'; | ||
import { useUpdateMailboxStatus } from '../api/useUpdateMailboxStatus'; | ||
import { MailDomainMailbox } from '../types'; | ||
|
||
interface MailDomainsActionsProps { | ||
mailbox: MailDomainMailbox; | ||
mailDomain: MailDomain; | ||
} | ||
|
||
export const MailDomainsActions = ({ | ||
mailDomain, | ||
mailbox, | ||
}: MailDomainsActionsProps) => { | ||
const { t } = useTranslation(); | ||
const [isDropOpen, setIsDropOpen] = useState(false); | ||
const isEnabled = mailbox.status === 'enabled'; | ||
const disableModal = useModal(); | ||
const { toast } = useToastProvider(); | ||
|
||
const { mutate: updateMailboxStatus } = useUpdateMailboxStatus(); | ||
|
||
const handleUpdateMailboxStatus = () => { | ||
disableModal.close(); | ||
updateMailboxStatus( | ||
{ | ||
mailDomainSlug: mailDomain.slug, | ||
mailboxId: mailbox.id, | ||
isEnabled: !isEnabled, | ||
}, | ||
{ | ||
onError: () => | ||
toast(t('Failed to update mailbox status'), VariantType.ERROR), | ||
}, | ||
); | ||
}; | ||
|
||
if (mailbox.status === 'pending' || mailbox.status === 'failed') { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
<DropButton | ||
button={ | ||
<IconOptions | ||
isOpen={isDropOpen} | ||
aria-label={t('Open the access options modal')} | ||
/> | ||
} | ||
onOpenChange={(isOpen) => setIsDropOpen(isOpen)} | ||
isOpen={isDropOpen} | ||
> | ||
<Box> | ||
<Button | ||
aria-label={t('Open the modal to update the role of this access')} | ||
onClick={() => { | ||
setIsDropOpen(false); | ||
if (isEnabled) { | ||
disableModal.open(); | ||
} else { | ||
handleUpdateMailboxStatus(); | ||
} | ||
}} | ||
fullWidth | ||
color="primary-text" | ||
icon={ | ||
<span className="material-icons" aria-hidden="true"> | ||
{isEnabled ? 'lock' : 'lock_open'} | ||
</span> | ||
} | ||
> | ||
<Text $theme="primary"> | ||
{isEnabled ? t('Disable mailbox') : t('Enable mailbox')} | ||
</Text> | ||
</Button> | ||
</Box> | ||
</DropButton> | ||
<Modal | ||
isOpen={disableModal.isOpen} | ||
onClose={disableModal.close} | ||
title={<Text $size="h3">{t('Disable mailbox')}</Text>} | ||
size={ModalSize.MEDIUM} | ||
rightActions={ | ||
<Box $direction="row" $justify="flex-end" $gap="0.5rem"> | ||
<Button color="secondary" onClick={disableModal.close}> | ||
{t('Cancel')} | ||
</Button> | ||
<Button color="danger" onClick={handleUpdateMailboxStatus}> | ||
{t('Disable')} | ||
</Button> | ||
</Box> | ||
} | ||
> | ||
<Text> | ||
{t( | ||
'Are you sure you want to disable this mailbox? This action results in the deletion of the calendar, address book, etc.', | ||
)} | ||
</Text> | ||
</Modal> | ||
</> | ||
); | ||
}; |
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
Oops, something went wrong.