2022-09-15 03:58:47 +00:00
|
|
|
|
/**
|
|
|
|
|
* External dependencies
|
|
|
|
|
*/
|
|
|
|
|
import { useState, useCallback } from '@wordpress/element';
|
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
|
import { WC_ADMIN_NAMESPACE } from '@woocommerce/data';
|
|
|
|
|
import { useDispatch } from '@wordpress/data';
|
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
2022-10-13 08:12:40 +00:00
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2022-09-15 03:58:47 +00:00
|
|
|
|
|
2022-10-13 08:12:40 +00:00
|
|
|
|
export const SendMagicLinkStates = {
|
2022-09-15 03:58:47 +00:00
|
|
|
|
INIT: 'initializing',
|
|
|
|
|
FETCHING: 'fetching',
|
|
|
|
|
SUCCESS: 'success',
|
|
|
|
|
ERROR: 'error',
|
|
|
|
|
} as const;
|
2022-10-13 08:12:40 +00:00
|
|
|
|
export type SendMagicLinkStates =
|
2023-05-30 23:45:10 +00:00
|
|
|
|
( typeof SendMagicLinkStates )[ keyof typeof SendMagicLinkStates ];
|
2022-09-15 03:58:47 +00:00
|
|
|
|
|
|
|
|
|
export type MagicLinkResponse = {
|
|
|
|
|
data: unknown;
|
|
|
|
|
code: string;
|
|
|
|
|
message: string;
|
|
|
|
|
} & Response;
|
|
|
|
|
|
|
|
|
|
export const sendMagicLink = () => {
|
|
|
|
|
return apiFetch< MagicLinkResponse >( {
|
|
|
|
|
path: `${ WC_ADMIN_NAMESPACE }/mobile-app/send-magic-link`,
|
|
|
|
|
} );
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useSendMagicLink = () => {
|
|
|
|
|
const [ requestState, setRequestState ] = useState< SendMagicLinkStates >(
|
|
|
|
|
SendMagicLinkStates.INIT
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const { createNotice } = useDispatch( 'core/notices' );
|
|
|
|
|
|
|
|
|
|
const fetchMagicLinkApiCall = useCallback( () => {
|
|
|
|
|
setRequestState( SendMagicLinkStates.FETCHING );
|
|
|
|
|
|
|
|
|
|
sendMagicLink()
|
|
|
|
|
.then( ( response ) => {
|
|
|
|
|
if ( response.code === 'success' ) {
|
|
|
|
|
setRequestState( SendMagicLinkStates.SUCCESS );
|
|
|
|
|
} else {
|
|
|
|
|
setRequestState( SendMagicLinkStates.ERROR );
|
|
|
|
|
createNotice(
|
|
|
|
|
'error',
|
2023-05-08 07:55:09 +00:00
|
|
|
|
__( 'Sorry, an unknown error occurred.', 'woocommerce' )
|
2022-09-15 03:58:47 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} )
|
|
|
|
|
.catch( ( response ) => {
|
|
|
|
|
setRequestState( SendMagicLinkStates.ERROR );
|
2022-10-13 08:12:40 +00:00
|
|
|
|
recordEvent( 'magic_prompt_send_magic_link_error', {
|
|
|
|
|
error: response.message,
|
|
|
|
|
code: response.code,
|
|
|
|
|
} );
|
2022-09-15 03:58:47 +00:00
|
|
|
|
if ( response.code === 'error_sending_mobile_magic_link' ) {
|
|
|
|
|
createNotice(
|
|
|
|
|
'error',
|
|
|
|
|
__(
|
2024-08-20 09:13:17 +00:00
|
|
|
|
'We couldn’t send the link. Try again in a few seconds.',
|
2022-09-15 03:58:47 +00:00
|
|
|
|
'woocommerce'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
} else if (
|
|
|
|
|
response.code === 'invalid_user_permission_view_admin'
|
|
|
|
|
) {
|
|
|
|
|
createNotice(
|
|
|
|
|
'error',
|
|
|
|
|
__(
|
2024-08-20 09:13:17 +00:00
|
|
|
|
'Sorry, your account doesn’t have sufficient permission.',
|
2022-09-15 03:58:47 +00:00
|
|
|
|
'woocommerce'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
} else if ( response.code === 'jetpack_not_connected' ) {
|
|
|
|
|
createNotice( 'error', response.message );
|
|
|
|
|
} else {
|
2022-10-13 08:12:40 +00:00
|
|
|
|
createNotice(
|
|
|
|
|
'error',
|
2024-08-20 09:13:17 +00:00
|
|
|
|
'We couldn’t send the link. Try again in a few seconds.'
|
2022-10-13 08:12:40 +00:00
|
|
|
|
);
|
2022-09-15 03:58:47 +00:00
|
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
}, [ createNotice ] );
|
|
|
|
|
|
|
|
|
|
return { requestState, fetchMagicLinkApiCall };
|
|
|
|
|
};
|