-
Notifications
You must be signed in to change notification settings - Fork 2k
/
actions.js
192 lines (170 loc) · 4.41 KB
/
actions.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* External dependencies
*/
import { assign } from 'lodash';
import debugModule from 'debug';
/**
* Internal dependencies
*/
import {
CART_COUPON_APPLY,
CART_COUPON_REMOVE,
CART_DISABLE,
CART_ITEM_REMOVE,
CART_ITEM_REPLACE,
CART_ITEMS_ADD,
CART_ITEMS_REPLACE_ALL,
CART_PRIVACY_PROTECTION_ADD,
CART_PRIVACY_PROTECTION_REMOVE,
CART_GOOGLE_APPS_REGISTRATION_DATA_ADD,
CART_TAX_COUNTRY_CODE_SET,
CART_TAX_POSTAL_CODE_SET,
CART_RELOAD,
} from './action-types';
import Dispatcher from 'dispatcher';
import { MARKETING_COUPONS_KEY } from 'lib/analytics/utils';
// We need to load the CartStore to make sure the store is registered with the
// dispatcher even though it's not used directly here
import './store';
/**
* Constants
*/
const debug = debugModule( 'calypso:signup:cart' );
export function disableCart() {
Dispatcher.handleViewAction( { type: CART_DISABLE } );
}
export function addPrivacyToAllDomains() {
Dispatcher.handleViewAction( {
type: CART_PRIVACY_PROTECTION_ADD,
} );
}
export function removePrivacyFromAllDomains() {
Dispatcher.handleViewAction( {
type: CART_PRIVACY_PROTECTION_REMOVE,
} );
}
export function addItem( item ) {
addItems( [ item ] );
}
export function addItems( items ) {
const extendedItems = items.map( ( item ) => {
const extra = assign( {}, item.extra, {
context: 'calypstore',
} );
return assign( {}, item, { extra } );
} );
Dispatcher.handleViewAction( {
type: CART_ITEMS_ADD,
cartItems: extendedItems,
} );
}
export function replaceCartWithItems( items ) {
const extendedItems = items.map( ( item ) => {
const extra = assign( {}, item.extra, {
context: 'calypstore',
} );
return assign( {}, item, { extra } );
} );
Dispatcher.handleViewAction( {
type: CART_ITEMS_REPLACE_ALL,
cartItems: extendedItems,
} );
}
export function removeItem( item, domainsWithPlansOnly ) {
Dispatcher.handleViewAction( {
type: CART_ITEM_REMOVE,
cartItem: item,
domainsWithPlansOnly,
} );
}
export function replaceItem( oldItem, newItem ) {
Dispatcher.handleViewAction( {
type: CART_ITEM_REPLACE,
oldItem,
newItem,
} );
}
export function addGoogleAppsRegistrationData( registrationData ) {
Dispatcher.handleViewAction( {
type: CART_GOOGLE_APPS_REGISTRATION_DATA_ADD,
registrationData: registrationData,
} );
}
export function applyCoupon( coupon ) {
Dispatcher.handleViewAction( {
type: CART_COUPON_APPLY,
coupon,
} );
}
export function removeCoupon() {
Dispatcher.handleViewAction( {
type: CART_COUPON_REMOVE,
} );
}
export function getRememberedCoupon() {
// read coupon list from localStorage, return early if it's not there
const couponsJson = window.localStorage.getItem( MARKETING_COUPONS_KEY );
const coupons = JSON.parse( couponsJson );
if ( ! coupons ) {
debug( 'No coupons found in localStorage: ', coupons );
return null;
}
const ALLOWED_COUPON_CODE_LIST = [
'ALT',
'FBSAVE15',
'FIVERR',
'GENEA',
'KITVISA',
'LINKEDIN',
'PATREON',
'ROCKETLAWYER',
'RBC',
'SAFE',
'SBDC',
'TXAM',
];
const THIRTY_DAYS_MILLISECONDS = 30 * 24 * 60 * 60 * 1000;
const now = Date.now();
debug( 'Found coupons in localStorage: ', coupons );
// delete coupons if they're older than thirty days; find the most recent one
let mostRecentTimestamp = 0;
let mostRecentCouponCode = null;
Object.keys( coupons ).forEach( ( key ) => {
if ( now > coupons[ key ] + THIRTY_DAYS_MILLISECONDS ) {
delete coupons[ key ];
} else if ( coupons[ key ] > mostRecentTimestamp ) {
mostRecentCouponCode = key;
mostRecentTimestamp = coupons[ key ];
}
} );
// write remembered coupons back to localStorage
debug( 'Storing coupons in localStorage: ', coupons );
window.localStorage.setItem( MARKETING_COUPONS_KEY, JSON.stringify( coupons ) );
if (
ALLOWED_COUPON_CODE_LIST.includes(
-1 !== mostRecentCouponCode.indexOf( '_' )
? mostRecentCouponCode.substring( 0, mostRecentCouponCode.indexOf( '_' ) )
: mostRecentCouponCode
)
) {
debug( 'returning coupon code:', mostRecentCouponCode );
return mostRecentCouponCode;
}
debug( 'not returning any coupon code.' );
return null;
}
export function setTaxCountryCode( countryCode ) {
Dispatcher.handleViewAction( {
type: CART_TAX_COUNTRY_CODE_SET,
countryCode,
} );
}
export function setTaxPostalCode( postalCode ) {
Dispatcher.handleViewAction( {
type: CART_TAX_POSTAL_CODE_SET,
postalCode,
} );
}
export function reloadCart() {
Dispatcher.handleViewAction( { type: CART_RELOAD } );
}