This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
/
add-to-cart-form-context.js
155 lines (143 loc) · 4.33 KB
/
add-to-cart-form-context.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
/**
* External dependencies
*/
import {
createContext,
useContext,
useState,
useCallback,
} from '@wordpress/element';
import {
useStoreAddToCart,
useTriggerFragmentRefresh,
} from '@woocommerce/base-hooks';
/**
* @typedef {import('@woocommerce/type-defs/contexts').AddToCartFormContext} AddToCartFormContext
*/
const AddToCartFormContext = createContext( {
product: {},
productId: 0,
variationId: 0,
variationData: {},
cartItemData: {},
quantity: 1,
minQuantity: 1,
maxQuantity: 99,
quantityInCart: 0,
setQuantity: ( quantity ) => void { quantity },
setVariationId: ( variationId ) => void { variationId },
setVariationData: ( variationData ) => void { variationData },
setCartItemData: ( cartItemData ) => void { cartItemData },
showFormElements: false,
formInitialized: false,
formDisabled: true,
formSubmitting: false,
onChange: () => void null,
onSubmit: () => void null,
onSuccess: () => void null,
onFail: () => void null,
} );
/**
* @return {AddToCartFormContext} Returns the add to cart form context value.
*/
export const useAddToCartFormContext = () => {
return useContext( AddToCartFormContext );
};
/**
* Provides an interface for blocks to control the add to cart form for a product.
*
* @param {Object} props Incoming props for the provider.
* @param {*} props.children The children being wrapped.
* @param {Object} [props.product] The product for which the form belongs to.
* @param {boolean} [props.showFormElements] Should form elements be shown.
*/
export const AddToCartFormContextProvider = ( {
children,
product,
showFormElements,
} ) => {
const productId = product.id || 0;
const [ variationId, setVariationId ] = useState( 0 );
const [ variationData, setVariationData ] = useState( {} );
const [ cartItemData, setCartItemData ] = useState( {} );
const [ quantity, setQuantity ] = useState( 1 );
const {
addToCart: storeAddToCart,
addingToCart: formSubmitting,
cartQuantity: quantityInCart,
cartIsLoading,
} = useStoreAddToCart( productId );
// This will ensure any add to cart events update legacy fragments using jQuery.
useTriggerFragmentRefresh( quantityInCart );
/**
* @todo Introduce Validation Emitter for the Add to Cart Form
*
* The add to cart form may have several inner form elements which need to run validation and
* change whether or not the form can be submitted. They may also need to show errors and
* validation notices.
*/
const formInitialized = ! cartIsLoading && productId > 0;
const formDisabled =
formSubmitting ||
! formInitialized ||
! productIsPurchasable( product );
// Events.
const onSubmit = useCallback( () => {
/**
* @todo Surface add to cart errors in the single product block
*
* If the addToCart function within useStoreAddToCart fails, a notice should be shown on the product page.
*/
storeAddToCart( quantity );
}, [ storeAddToCart, quantity ] );
/**
* @todo Add Event Callbacks to the Add to Cart Form
*
* onChange should trigger when a form element changes, so for example, a variation picker could indicate that it's ready.
* onSuccess should trigger after a successful add to cart. This could be used to reset form elements, do a redirect, or show something to the user.
* onFail should trigger when adding to cart fails. Form elements might show extra notices or reset. A fallback might be to redirect to the core product page in case of incompatibilities.
*/
const onChange = useCallback( () => {}, [] );
const onSuccess = useCallback( () => {}, [] );
const onFail = useCallback( () => {}, [] );
/**
* @type {AddToCartFormContext}
*/
const contextValue = {
product,
productId,
variationId,
variationData,
cartItemData,
quantity,
minQuantity: 1,
maxQuantity: product.quantity_limit || 99,
quantityInCart,
setQuantity,
setVariationId,
setVariationData,
setCartItemData,
showFormElements,
formInitialized,
formDisabled,
formSubmitting,
onChange,
onSubmit,
onSuccess,
onFail,
};
return (
<AddToCartFormContext.Provider value={ contextValue }>
{ children }
</AddToCartFormContext.Provider>
);
};
/**
* Check a product object to see if it can be purchased.
*
* @param {Object} product Product object.
*/
const productIsPurchasable = ( product ) => {
const { is_purchasable: isPurchasable = false } = product;
return isPurchasable;
};