File size: 1,743 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
import { ecommerceFlowRecurTypes } from '@automattic/onboarding';
import { useDispatch } from '@wordpress/data';
import { ONBOARD_STORE } from '../stores';
import { useQuery } from './use-query';

/**
 * This method saves some query params to the store. For example,
 * coupon code or storage addons provided in the query param will be
 * persisted to the Store so that they can later be retrieved and applied
 * at checkout.
 */
export function useSaveQueryParams() {
	const urlQueryParams = useQuery();
	const { setCouponCode, setStorageAddonSlug, setEcommerceFlowRecurType, setPartnerBundle } =
		useDispatch( ONBOARD_STORE );

	urlQueryParams.forEach( ( value, key ) => {
		switch ( key ) {
			case 'coupon':
				// This stores the coupon code query param, and the flow declaration
				// will append it to the checkout URL so that it auto-applies the coupon code at
				// checkout. For example, /setup/ecommerce/?coupon=SOMECOUPON will auto-apply the
				// coupon code at the checkout page.
				value && setCouponCode( value );
				break;

			case 'storage':
				// This stores a storage addon, supplied as a query param by landing pages when
				// an addon is selected in the pricing grid.
				value && setStorageAddonSlug( value );
				break;

			case 'partnerBundle':
				// This stores a partner bundle string, supplied as a query param in landing pages
				// related to a partner partnership.
				value && setPartnerBundle( value );
				break;

			case 'recur':
				{
					const isValidRecurType =
						value && Object.values( ecommerceFlowRecurTypes ).includes( value );
					const recurType = isValidRecurType ? value : ecommerceFlowRecurTypes.YEARLY;
					setEcommerceFlowRecurType( recurType );
				}
				break;
		}
	} );
}