File size: 2,015 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
import { translate } from 'i18n-calypso';
import { SITE_PLAN_OWNERSHIP_TRANSFER } 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 { errorNotice, successNotice } from 'calypso/state/notices/actions';
import { refreshSitePlans } from 'calypso/state/sites/plans/actions';

const noticeOptions = ( siteId ) => ( {
	duration: 8000,
	id: `sites-plan-transfer-notice-${ siteId }`,
} );

/**
 * Dispatches a request to transfer a site's plan to another user.
 * @param   {Object} action Redux action
 * @returns {Object} Dispatched http action
 */
export const requestPlanOwnershipTransfer = ( action ) =>
	http(
		{
			apiVersion: '1',
			method: 'POST',
			path: '/sites/' + action.siteId + '/plan-transfer',
			query: {
				new_user_id: action.newOwnerUserId,
			},
		},
		action
	);

/**
 * Dispatches a success notice when the request succeeded.
 * @param   {Object} action Redux action
 * @param   {number} action.siteId
 * @returns {Object} Success notice action
 */
export const handleTransferSuccess = ( { siteId } ) => [
	successNotice(
		translate( 'Plan purchaser has been changed successfully.' ),
		noticeOptions( siteId )
	),
	refreshSitePlans( siteId ),
];

/**
 * Dispatches an error notice when the request failed.
 * @param   {Object} action Redux action
 * @param   {number} action.siteId
 * @param   {Object} error  Error object
 * @param   {string} error.message
 * @returns {Object} Error notice action
 */
export const handleTransferError = ( { siteId }, { message } ) =>
	errorNotice( message, noticeOptions( siteId ) );

registerHandlers( 'state/data-layer/wpcom/sites/plan-transfer/index.js', {
	[ SITE_PLAN_OWNERSHIP_TRANSFER ]: [
		dispatchRequest( {
			fetch: requestPlanOwnershipTransfer,
			onSuccess: handleTransferSuccess,
			onError: handleTransferError,
		} ),
	],
} );