-
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
- Loading branch information
1 parent
bcdc579
commit c1761d2
Showing
6 changed files
with
238 additions
and
48 deletions.
There are no files selected for viewing
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 }, | ||
], | ||
}); | ||
}, | ||
}); | ||
}; |
73 changes: 73 additions & 0 deletions
73
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,73 @@ | ||
import { Button } 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 { mutate: updateMailboxStatus } = useUpdateMailboxStatus(); | ||
|
||
const handleUpdateMailboxStatus = () => { | ||
updateMailboxStatus({ | ||
mailDomainSlug: mailDomain.slug, | ||
mailboxId: mailbox.id, | ||
isEnabled: !isEnabled, | ||
}); | ||
}; | ||
|
||
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); | ||
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> | ||
</> | ||
); | ||
}; |
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
Oops, something went wrong.