File size: 2,754 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
103
104
105
106
107
108
109
import { withStorageKey } from '@automattic/state-utils';
import {
	PURCHASE_CANCELLATION_OFFER_REQUEST,
	PURCHASE_CANCELLATION_OFFER_RECEIVE,
	PURCHASE_CANCELLATION_OFFER_REQUEST_FAILURE,
	PURCHASE_CANCELLATION_OFFER_APPLY,
	PURCHASE_CANCELLATION_OFFER_APPLY_FAILURE,
	PURCHASE_CANCELLATION_OFFER_APPLY_SUCCESS,
} from 'calypso/state/action-types';
import {
	CancellationOffer,
	CancellationOfferAPIResponse,
} from 'calypso/state/cancellation-offers/types';
import { combineReducers, keyedReducer } from 'calypso/state/utils';
import type { AnyAction } from 'redux';

// Map the response to a typed object.
const mapResponseObject = ( {
	currency_code,
	discount_percentage,
	discounted_periods,
	formatted_price,
	original_price,
	raw_price,
}: CancellationOfferAPIResponse ): CancellationOffer => ( {
	currencyCode: currency_code,
	discountPercentage: discount_percentage,
	discountedPeriods: discounted_periods,
	formattedPrice: formatted_price,
	originalPrice: original_price,
	rawPrice: raw_price,
} );

const createCancellationOfferMap = ( payload: CancellationOfferAPIResponse[] ) => {
	return payload.map( mapResponseObject );
};

const isFetching = ( state = null, action: AnyAction ) => {
	switch ( action.type ) {
		case PURCHASE_CANCELLATION_OFFER_REQUEST:
			return true;
		case PURCHASE_CANCELLATION_OFFER_RECEIVE:
		case PURCHASE_CANCELLATION_OFFER_REQUEST_FAILURE:
			return false;
	}

	return state;
};

const error = ( state = {}, action: AnyAction ) => {
	switch ( action.type ) {
		case PURCHASE_CANCELLATION_OFFER_REQUEST_FAILURE:
			return action.error;
	}

	return state;
};

export const offers = ( state = [], action: AnyAction ) => {
	switch ( action.type ) {
		case PURCHASE_CANCELLATION_OFFER_RECEIVE:
			return createCancellationOfferMap( action.offers );
	}

	return state;
};

export const isApplying = ( state = false, action: AnyAction ) => {
	switch ( action.type ) {
		case PURCHASE_CANCELLATION_OFFER_APPLY:
			return true;
		case PURCHASE_CANCELLATION_OFFER_APPLY_FAILURE:
		case PURCHASE_CANCELLATION_OFFER_APPLY_SUCCESS:
			return false;
	}

	return state;
};

export const applyError = ( state = null, action: AnyAction ) => {
	switch ( action.type ) {
		case PURCHASE_CANCELLATION_OFFER_APPLY_FAILURE:
			return action.error;
	}

	return state;
};

export const applySuccess = ( state = false, action: AnyAction ) => {
	switch ( action.type ) {
		case PURCHASE_CANCELLATION_OFFER_APPLY_SUCCESS:
			return action.success;
	}

	return state;
};

const cancellationOffersReducer = combineReducers( {
	isFetching,
	error,
	offers,
	isApplying,
	applyError,
	applySuccess,
} );

const reducer = keyedReducer( 'purchaseId', cancellationOffersReducer );
export default withStorageKey( 'cancellationOffers', reducer );