File size: 2,331 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
import { translate } from 'i18n-calypso';
import {
EMAIL_VERIFY_REQUEST,
EMAIL_VERIFY_REQUEST_SUCCESS,
EMAIL_VERIFY_REQUEST_FAILURE,
} from 'calypso/state/action-types';
import { registerHandlers } from 'calypso/state/data-layer/handler-registry';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import { dispatchRequest } from 'calypso/state/data-layer/wpcom-http/utils';
import {
infoNotice,
errorNotice,
successNotice,
removeNotice,
} from 'calypso/state/notices/actions';
const infoNoticeId = 'email-verification-info-notice';
/**
* Creates an action for request for email verification
* @param {Object} action The action to dispatch next
* @returns {Object} Redux action
*/
export const requestEmailVerification = ( action ) => [
...( action?.showGlobalNotices
? [ infoNotice( translate( 'Sending email…' ), { id: infoNoticeId, duration: 4000 } ) ]
: [] ),
http(
{
apiVersion: '1.1',
method: 'POST',
path: '/me/send-verification-email',
},
action
),
];
/**
* Creates an action for handling email verification error
* @param {Object} action The action to dispatch next
* @param {Object} rawError The error object
* @returns {Object} Redux action
*/
export const handleError = ( action, rawError ) => [
{
type: EMAIL_VERIFY_REQUEST_FAILURE,
message: rawError.message,
},
...( action?.showGlobalNotices
? [
removeNotice( infoNoticeId ),
errorNotice( translate( 'Sorry, we couldn’t send the email.' ), {
id: 'email-verification-error-notice',
duration: 4000,
} ),
]
: [] ),
];
/**
* Creates an action for email verification success
* @param {Object} action The action to dispatch next
* @returns {Object} Redux action
*/
export const handleSuccess = ( action ) => [
{ type: EMAIL_VERIFY_REQUEST_SUCCESS },
...( action?.showGlobalNotices
? [
removeNotice( infoNoticeId ),
successNotice( translate( 'Email sent' ), {
id: 'email-verification-success-notice',
duration: 4000,
} ),
]
: [] ),
];
export const dispatchEmailVerification = dispatchRequest( {
fetch: requestEmailVerification,
onSuccess: handleSuccess,
onError: handleError,
} );
registerHandlers( 'state/data-layer/wpcom/me/send-verification-email/index.js', {
[ EMAIL_VERIFY_REQUEST ]: [ dispatchEmailVerification ],
} );
|