|
|
import { CALYPSO_CONTACT } from '@automattic/urls'; |
|
|
import { useMutation } from '@tanstack/react-query'; |
|
|
import { useTranslate } from 'i18n-calypso'; |
|
|
import wp from 'calypso/lib/wp'; |
|
|
import { useDispatch } from 'calypso/state'; |
|
|
import { errorNotice, successNotice } from 'calypso/state/notices/actions'; |
|
|
import type { AlterDestinationParams } from './types'; |
|
|
import type { UseMutationOptions } from '@tanstack/react-query'; |
|
|
|
|
|
const MUTATION_KEY = 'reverifyEmailForward'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default function useResendVerifyEmailForwardMutation( |
|
|
domainName: string, |
|
|
mutationOptions: Omit< |
|
|
UseMutationOptions< any, unknown, AlterDestinationParams >, |
|
|
'mutationFn' |
|
|
> = {} |
|
|
) { |
|
|
const dispatch = useDispatch(); |
|
|
const translate = useTranslate(); |
|
|
|
|
|
const suppliedOnError = mutationOptions.onError; |
|
|
const suppliedOnSuccess = mutationOptions.onSuccess; |
|
|
|
|
|
mutationOptions.mutationKey = [ MUTATION_KEY ]; |
|
|
|
|
|
mutationOptions.onSuccess = ( data, emailForward, context ) => { |
|
|
suppliedOnSuccess?.( data, emailForward, context ); |
|
|
|
|
|
const { destination } = emailForward; |
|
|
|
|
|
const successMessage = translate( |
|
|
'Successfully sent confirmation email for %(email)s to %(destination)s.', |
|
|
{ |
|
|
args: { |
|
|
email: emailForward.mailbox + '@' + domainName, |
|
|
destination, |
|
|
}, |
|
|
} |
|
|
); |
|
|
|
|
|
dispatch( |
|
|
successNotice( successMessage, { |
|
|
duration: 7000, |
|
|
} ) |
|
|
); |
|
|
}; |
|
|
|
|
|
mutationOptions.onError = ( error, variables, context ) => { |
|
|
suppliedOnError?.( error, variables, context ); |
|
|
|
|
|
const failureMessage = translate( |
|
|
'Failed to resend verification email for email forwarding record %(email)s. ' + |
|
|
'Please try again or {{contactSupportLink}}contact support{{/contactSupportLink}}.', |
|
|
{ |
|
|
args: { |
|
|
email: variables.mailbox + '@' + domainName, |
|
|
}, |
|
|
components: { |
|
|
contactSupportLink: <a href={ CALYPSO_CONTACT } />, |
|
|
}, |
|
|
} |
|
|
); |
|
|
|
|
|
dispatch( errorNotice( failureMessage ) ); |
|
|
}; |
|
|
|
|
|
return useMutation< any, unknown, AlterDestinationParams >( { |
|
|
mutationFn: ( { mailbox, destination } ) => |
|
|
wp.req.post( |
|
|
`/domains/${ encodeURIComponent( domainName ) }/email/${ encodeURIComponent( |
|
|
mailbox |
|
|
) }/${ encodeURIComponent( destination ) }/resend-verification` |
|
|
), |
|
|
...mutationOptions, |
|
|
} ); |
|
|
} |
|
|
|