Skip to content

Commit

Permalink
Revert "Domains: Implement multi-target email forwards (#98837)"
Browse files Browse the repository at this point in the history
This reverts commit a1abc2c.
  • Loading branch information
alshakero authored Feb 4, 2025
1 parent a1abc2c commit 20eebd6
Show file tree
Hide file tree
Showing 35 changed files with 754 additions and 1,012 deletions.
31 changes: 0 additions & 31 deletions client/data/emails/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,6 @@ export type EmailAccountEmail = {
warnings: Warning[];
};

export type ResponseError = {
error:
| 'destination_failed'
| 'invalid_input'
| 'not_valid_destination'
| 'destination_failed'
| 'too_many_destinations'
| 'exceeded_mailbox_forwards'
| 'mailbox_too_long'
| 'not_valid_mailbox'
| 'empty_destination'
| 'same_destination_domain'
| 'forward_exists';
message:
| string
| {
error_message: string;
/**
* The index of the faulty email address in the `destinations` array
*/
index: number;
};
};

export type AlterDestinationParams = {
mailbox: string;
destination: string;
domain: string;
};

type EmailAccountDomain = {
domain: string;
is_primary: boolean;
Expand Down Expand Up @@ -75,5 +45,4 @@ export type Mailbox = {
mailbox: string;
warnings?: Warning[];
temporary?: boolean;
target: string;
};
27 changes: 11 additions & 16 deletions client/data/emails/use-add-email-forward-mutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import { useDispatch, useSelector } from 'calypso/state';
import { errorNotice } from 'calypso/state/notices/actions';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';
import { getCacheKey as getEmailAccountsQueryKey } from './use-get-email-accounts-query';
import type { ResponseError } from './types';
import type { UseMutationOptions } from '@tanstack/react-query';

const ArrayOfFive = new Array( 5 );

type AddMailboxFormData = {
destinations: typeof ArrayOfFive;
destination: string;
mailbox: string;
};

Expand All @@ -41,7 +38,7 @@ export function useIsLoading() {
export default function useAddEmailForwardMutation(
domainName: string,
mutationOptions: Omit<
UseMutationOptions< any, ResponseError, AddMailboxFormData, Context >,
UseMutationOptions< any, unknown, AddMailboxFormData, Context >,
'mutationFn'
> = {}
) {
Expand All @@ -68,7 +65,7 @@ export default function useAddEmailForwardMutation(
};

mutationOptions.onMutate = async ( variables ) => {
const { mailbox, destinations } = variables;
const { mailbox, destination } = variables;
suppliedOnMutate?.( variables );

await queryClient.cancelQueries( { queryKey: emailAccountsQueryKey } );
Expand All @@ -82,16 +79,16 @@ export default function useAddEmailForwardMutation(
const newEmailForwards = orderBy(
[
...emailForwards,
...destinations.map( ( d ) => ( {
{
domain: domainName,
email_type: 'email_forward',
is_verified: false,
mailbox,
role: 'standard',
target: d,
target: destination,
temporary: true,
warnings: [],
} ) ),
},
],
[ 'mailbox' ],
[ 'asc' ]
Expand Down Expand Up @@ -138,7 +135,7 @@ export default function useAddEmailForwardMutation(
};
};

mutationOptions.onError = ( error: ResponseError, variables, context ) => {
mutationOptions.onError = ( error, variables, context ) => {
suppliedOnError?.( error, variables, context );

if ( context ) {
Expand Down Expand Up @@ -166,14 +163,12 @@ export default function useAddEmailForwardMutation(
);

if ( error ) {
const message =
typeof error.message === 'object' ? error.message.error_message : error.message;
errorMessage = translate(
'Failed to add email forward for {{strong}}%(emailAddress)s{{/strong}} with message "%(message)s". Please try again or {{contactSupportLink}}contact support{{/contactSupportLink}}.',
{
args: {
emailAddress: variables.mailbox,
message,
message: error as string,
},
components: noticeComponents,
}
Expand All @@ -183,11 +178,11 @@ export default function useAddEmailForwardMutation(
dispatch( errorNotice( errorMessage ) );
};

return useMutation< any, ResponseError, AddMailboxFormData, Context >( {
mutationFn: ( { mailbox, destinations } ) =>
return useMutation< any, unknown, AddMailboxFormData, Context >( {
mutationFn: ( { mailbox, destination } ) =>
wp.req.post( `/domains/${ encodeURIComponent( domainName ) }/email/new`, {
mailbox,
destinations,
destination,
} ),
...mutationOptions,
} );
Expand Down
21 changes: 8 additions & 13 deletions client/data/emails/use-remove-email-forward-mutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useDispatch, useSelector } from 'calypso/state';
import { errorNotice } from 'calypso/state/notices/actions';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';
import { getCacheKey as getEmailAccountsQueryKey } from './use-get-email-accounts-query';
import type { EmailAccountEmail, AlterDestinationParams, Mailbox } from './types';
import type { EmailAccountEmail } from './types';
import type { UseMutationOptions } from '@tanstack/react-query';

type Context = {
Expand All @@ -26,7 +26,7 @@ const MUTATION_KEY = 'removeEmailForward';
export default function useRemoveEmailForwardMutation(
domainName: string,
mutationOptions: Omit<
UseMutationOptions< any, unknown, AlterDestinationParams, Context >,
UseMutationOptions< any, unknown, EmailAccountEmail, Context >,
'mutationFn'
> = {}
) {
Expand Down Expand Up @@ -70,11 +70,7 @@ export default function useRemoveEmailForwardMutation(
{
...previousEmailAccountsQueryData.accounts[ 0 ],
emails: emailForwards.filter(
( forward: EmailAccountEmail ) =>
! (
forward.mailbox === emailForward.mailbox &&
forward.target === emailForward.destination
)
( forward: EmailAccountEmail ) => forward.mailbox !== emailForward.mailbox
),
},
],
Expand Down Expand Up @@ -144,14 +140,13 @@ export default function useRemoveEmailForwardMutation(
dispatch( errorMessage );
};

return useMutation< Mailbox, unknown, AlterDestinationParams, Context >( {
mutationFn: ( { mailbox, destination } ) => {
return wp.req.post(
return useMutation< any, unknown, EmailAccountEmail, Context >( {
mutationFn: ( { mailbox } ) =>
wp.req.post(
`/domains/${ encodeURIComponent( domainName ) }/email/${ encodeURIComponent(
mailbox
) }/${ encodeURIComponent( destination ) }/delete`
);
},
) }/delete`
),
...mutationOptions,
} );
}
16 changes: 7 additions & 9 deletions client/data/emails/use-resend-verify-email-forward-mutation.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { CALYPSO_CONTACT } from '@automattic/urls';
import { useMutation } from '@tanstack/react-query';
import { useTranslate } from 'i18n-calypso';
import { getEmailForwardAddress } from 'calypso/lib/emails';
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 { EmailAccountEmail } from './types';
import type { UseMutationOptions } from '@tanstack/react-query';

const MUTATION_KEY = 'reverifyEmailForward';
Expand All @@ -17,10 +18,7 @@ const MUTATION_KEY = 'reverifyEmailForward';
*/
export default function useResendVerifyEmailForwardMutation(
domainName: string,
mutationOptions: Omit<
UseMutationOptions< any, unknown, AlterDestinationParams >,
'mutationFn'
> = {}
mutationOptions: Omit< UseMutationOptions< any, unknown, EmailAccountEmail >, 'mutationFn' > = {}
) {
const dispatch = useDispatch();
const translate = useTranslate();
Expand All @@ -33,7 +31,7 @@ export default function useResendVerifyEmailForwardMutation(
mutationOptions.onSuccess = ( data, emailForward, context ) => {
suppliedOnSuccess?.( data, emailForward, context );

const { destination } = emailForward;
const destination = getEmailForwardAddress( emailForward );

const successMessage = translate(
'Successfully sent confirmation email for %(email)s to %(destination)s.',
Expand Down Expand Up @@ -71,12 +69,12 @@ export default function useResendVerifyEmailForwardMutation(
dispatch( errorNotice( failureMessage ) );
};

return useMutation< any, unknown, AlterDestinationParams >( {
mutationFn: ( { mailbox, destination } ) =>
return useMutation< any, unknown, EmailAccountEmail >( {
mutationFn: ( { mailbox } ) =>
wp.req.post(
`/domains/${ encodeURIComponent( domainName ) }/email/${ encodeURIComponent(
mailbox
) }/${ encodeURIComponent( destination ) }/resend-verification`
) }/resend-verification`
),
...mutationOptions,
} );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @param newEmailForward a string representing a new email forward
* @returns { boolean } If the email forward is present in the existing email forwards collection
*/
export function hasDuplicatedEmailForwards( newEmailForward, existingEmailForwards ) {
return existingEmailForwards?.some( ( forward ) => forward.mailbox === newEmailForward );
}
35 changes: 35 additions & 0 deletions client/lib/domains/email-forwarding/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
import emailValidator from 'email-validator';
import { mapValues } from 'lodash';
import { hasDuplicatedEmailForwards } from 'calypso/lib/domains/email-forwarding/has-duplicated-email-forwards';

function validateAllFields( fieldValues, existingEmailForwards = [] ) {
return mapValues( fieldValues, ( value, fieldName ) => {
const isValid = validateField( {
value,
name: fieldName,
} );

if ( ! isValid ) {
return [ 'Invalid' ];
}

if ( fieldName !== 'mailbox' ) {
return [];
}

return hasDuplicatedEmailForwards( value, existingEmailForwards ) ? [ 'Duplicated' ] : [];
} );
}

function validateField( { name, value } ) {
switch ( name ) {
case 'mailbox':
return /^[a-z0-9._+-]{1,64}$/i.test( value ) && ! /(^\.)|(\.{2,})|(\.$)/.test( value );
case 'destination':
return emailValidator.validate( value );
default:
return true;
}
}

export { getEmailForwardsCount } from './get-email-forwards-count';
export { hasEmailForwards } from './has-email-forwards';
export { getDomainsWithEmailForwards } from './get-domains-with-email-forwards';
export { validateAllFields };
80 changes: 0 additions & 80 deletions client/my-sites/email/email-forwarding/actions-menu/index.tsx

This file was deleted.

This file was deleted.

Loading

0 comments on commit 20eebd6

Please sign in to comment.