File size: 2,525 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {
	PURCHASE_CANCELLATION_OFFER_REQUEST,
	PURCHASE_CANCELLATION_OFFER_RECEIVE,
	PURCHASE_CANCELLATION_OFFER_REQUEST_FAILURE,
	PURCHASE_CANCELLATION_OFFER_APPLY_SUCCESS,
	PURCHASE_CANCELLATION_OFFER_APPLY_FAILURE,
	PURCHASE_CANCELLATION_OFFER_APPLY,
} 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 { noRetry } from 'calypso/state/data-layer/wpcom-http/pipeline/retry-on-failure/policies';
import { dispatchRequest } from 'calypso/state/data-layer/wpcom-http/utils';

// API request to get the cancellation offers
const fetchCancellationOffers = ( action: { siteId: number; purchaseId: number } ) => {
	return http(
		{
			method: 'GET',
			path: '/cancellation-offers',
			apiNamespace: 'wpcom/v2',
			query: {
				site: action.siteId,
				purchase: action.purchaseId,
			},
			retryPolicy: noRetry(),
		},
		action
	);
};

const onFetchSuccess = ( action: { purchaseId: number }, response: unknown ) => {
	return [
		{
			type: PURCHASE_CANCELLATION_OFFER_RECEIVE,
			purchaseId: action.purchaseId,
			offers: response,
		},
	];
};

const onFetchError = ( action: { purchaseId: number }, error: unknown ) => {
	return [
		{
			type: PURCHASE_CANCELLATION_OFFER_REQUEST_FAILURE,
			purchaseId: action.purchaseId,
			error,
		},
	];
};

const applyCancellationOffer = ( action: { siteId: number; purchaseId: number } ) => {
	return http(
		{
			method: 'POST',
			path: '/cancellation-offers/apply',
			apiNamespace: 'wpcom/v2',
			body: {
				site: action.siteId,
				purchase: action.purchaseId,
			},
		},
		action
	);
};

const onApplySuccess = ( action: { purchaseId: number }, response: { success: boolean } ) => {
	return [
		{
			type: PURCHASE_CANCELLATION_OFFER_APPLY_SUCCESS,
			purchaseId: action.purchaseId,
			success: response.success,
		},
	];
};

const onApplyFailure = ( action: { purchaseId: number }, error: unknown ) => {
	return [
		{
			type: PURCHASE_CANCELLATION_OFFER_APPLY_FAILURE,
			purchaseId: action.purchaseId,
			error,
		},
	];
};

registerHandlers( 'state/data-layer/wpcom/cancellation-offers/index.js', {
	[ PURCHASE_CANCELLATION_OFFER_REQUEST ]: [
		dispatchRequest( {
			fetch: fetchCancellationOffers,
			onSuccess: onFetchSuccess,
			onError: onFetchError,
		} ),
	],
	[ PURCHASE_CANCELLATION_OFFER_APPLY ]: [
		dispatchRequest( {
			fetch: applyCancellationOffer,
			onSuccess: onApplySuccess,
			onError: onApplyFailure,
		} ),
	],
} );