2020-02-25 11:36:53 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-02-24 01:36:24 +00:00
|
|
|
import type {
|
2024-10-23 16:15:41 +00:00
|
|
|
ApiErrorResponse,
|
2021-02-24 01:36:24 +00:00
|
|
|
Cart,
|
|
|
|
CartResponseItem,
|
|
|
|
} from '@woocommerce/types';
|
2022-02-22 17:45:01 +00:00
|
|
|
import { BillingAddress, ShippingAddress } from '@woocommerce/settings';
|
2020-02-25 11:36:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { ACTION_TYPES as types } from './action-types';
|
2021-12-07 15:47:50 +00:00
|
|
|
import { ReturnOrGeneratorYieldUnion } from '../mapped-types';
|
2023-01-30 15:12:17 +00:00
|
|
|
import type { Thunks } from './thunks';
|
Add notice on quantity change and update `wc/store/cart` to use thunks (https://github.com/woocommerce/woocommerce-blocks/pull/7938)
* Add receiveCart thunk
* Add mapCartResponseToCart helper
* Add getItemsPendingQuantityUpdate selector
* Update cart resolvers to be thunks
* Remove RECEIVE_CART action and replace with SET_CART_DATA
receiveCart will turn into a thunk.
* Add notifyQuantityChanges functions
* Remove receiveCart from action type definition, replace with setCartData
* Move apiFetchWithHeaders out of controls
This will just be a normal function since we'll be updating actions to thunks which will use this instead of a control.
* Include thunks in actions file
* Update receiveCart action to setCartData
* Update applyCoupon action to a thunk
* Update useStoreCartCoupons to get action from correct place
* Update StoreCartCoupon types
* Add types for Thunk and ThunkReturnType in mapped-types
* Change applyCoupon to a thunk
* Get applyCoupon, removeCoupon, receiveApplyingCoupon from useDispatch
This is to separate the concerns of actions vs. selectors. Previously the actions were fetched during useSelect which is not a pattern we use anywhere else in the codebase. Since we updated the MapToDispatch type, we can now get correctly typed thunks from the data store.
* Improve apiFetchWithHeaders typings
* Convert removeCoupon from generator to thunk
* Add applyCoupon and removeCoupon to CartAction type
* Remove unused old-style type-def
* Add receiveApplyingCoupon & receiveRemovingCoupon to StoreCartCoupon
* Correct issues with StoreCartCoupon type
These were not intended to reflect the actions in data store, rather the functions offered by the useStoreCartCoupons hook.
* Update applyExtensionCartUpdate to a thunk
* Update addItemToCart to thunk
* Add ResolveSelectFromMap type that works with thunks
* Add CartDispatchFromMap and CartResolveSelectFromMap types
We can add this to all data stores to get them working with thunks properly.
* Add docs and update generic name in ResolveSelectFromMap
* Add correct types for thunk resolvers in cart data store
* Update removeItemFromCart to thunk
* Update apiFetchWithHeaders to use generic
* Update selectShippingRate to thunk
* Update resolver tests to test correct thunk functionality
* Update updateCustomerData to thunk
* Update reducer test to reflect new action name
* Update comments on CartDispatchFromMap and CartResolveSelectFromMap
* Add quantity_limits to preview cart
* Make notices speak when shown
* Remove copilot comment
* Add isWithinQuantityLimits function
This is because we shouldn't show a notice if the quantity limits change, but the item's quantity is still OK.
* Add tests for notifyQuantityChanges
* Show notice when multiple_of is updated
* Update test to test for multiple_of changes
* Remove empty export
* Remove controls from cart data store
Not needed anymore since the exported value from the shared-controls file was empty.
* Export a control and async function for apiFetchWithHeaders
This is required because async functions cannot be called from sync generators.
* Use control version of apiFetchWithHeaders in the collections store
* Improve comments and remove incorrect TypeScript
* Update assets/js/data/cart/actions.ts
Co-authored-by: Mike Jolley <mike.jolley@me.com>
* Update ResolveSelectFromMap to include selectors too
* Update TS in actions
* Use finally to remove duplicate code
* remove item pending delete/qty update after action runs in all cases
This will also reset the state when the request to remove it/change quantity errors
* Remove unnecessary type from param.
Not needed because we have TS now. The description can stay though, it is useful.
* Update snackbar wording to use active voice
* Remove old WP version check
* Set max quantity to high number instead of null
This would only happen in a niche case, and would require several TS changes to fix, so it's better to set it as a number here. 9999 should be high enough, and is the default quantity limit set below in get_product_quantity_limit
* Set code on woocommerce_rest_cart_invalid_key to 409
This is so the cart is returned in the response, so the client can update.
* Fix typo in comment and add CartSelectFromMap
* Remove unnecessary docblock
* Add getItemsPendingDelete selector
This is needed so we can show a notice for items that are unexpectedly removed from the cart. We need to know which ones are pending delete so we can skip showing the notice for them.
* Add type for notifyQuantityChanges args and change args to object
* Add notifyIfRemoved function
This will check items that have been removed and show a notice for them.
* Fix TS in receiveCart & pass itemsPendingDelete to notifyQuantiyChanges
* Update wording on removal notice
* Update types for notifyQuantityChanges args
* Update tests to reflect new wording and args being an object
* Check item is truth before running comparison of keys
* Update tests for unexpectedly and expectedly removed items
* Ignore print_r to satisfy phpcs
* Update PHP tests to reflect correct response code when deleting items
* Remove unnecessary controls and dispatch events directly from thunk
Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-12-16 16:06:37 +00:00
|
|
|
|
|
|
|
// Thunks are functions that can be dispatched, similar to actions creators
|
|
|
|
export * from './thunks';
|
2020-02-25 11:36:53 +00:00
|
|
|
|
|
|
|
/**
|
Add notice on quantity change and update `wc/store/cart` to use thunks (https://github.com/woocommerce/woocommerce-blocks/pull/7938)
* Add receiveCart thunk
* Add mapCartResponseToCart helper
* Add getItemsPendingQuantityUpdate selector
* Update cart resolvers to be thunks
* Remove RECEIVE_CART action and replace with SET_CART_DATA
receiveCart will turn into a thunk.
* Add notifyQuantityChanges functions
* Remove receiveCart from action type definition, replace with setCartData
* Move apiFetchWithHeaders out of controls
This will just be a normal function since we'll be updating actions to thunks which will use this instead of a control.
* Include thunks in actions file
* Update receiveCart action to setCartData
* Update applyCoupon action to a thunk
* Update useStoreCartCoupons to get action from correct place
* Update StoreCartCoupon types
* Add types for Thunk and ThunkReturnType in mapped-types
* Change applyCoupon to a thunk
* Get applyCoupon, removeCoupon, receiveApplyingCoupon from useDispatch
This is to separate the concerns of actions vs. selectors. Previously the actions were fetched during useSelect which is not a pattern we use anywhere else in the codebase. Since we updated the MapToDispatch type, we can now get correctly typed thunks from the data store.
* Improve apiFetchWithHeaders typings
* Convert removeCoupon from generator to thunk
* Add applyCoupon and removeCoupon to CartAction type
* Remove unused old-style type-def
* Add receiveApplyingCoupon & receiveRemovingCoupon to StoreCartCoupon
* Correct issues with StoreCartCoupon type
These were not intended to reflect the actions in data store, rather the functions offered by the useStoreCartCoupons hook.
* Update applyExtensionCartUpdate to a thunk
* Update addItemToCart to thunk
* Add ResolveSelectFromMap type that works with thunks
* Add CartDispatchFromMap and CartResolveSelectFromMap types
We can add this to all data stores to get them working with thunks properly.
* Add docs and update generic name in ResolveSelectFromMap
* Add correct types for thunk resolvers in cart data store
* Update removeItemFromCart to thunk
* Update apiFetchWithHeaders to use generic
* Update selectShippingRate to thunk
* Update resolver tests to test correct thunk functionality
* Update updateCustomerData to thunk
* Update reducer test to reflect new action name
* Update comments on CartDispatchFromMap and CartResolveSelectFromMap
* Add quantity_limits to preview cart
* Make notices speak when shown
* Remove copilot comment
* Add isWithinQuantityLimits function
This is because we shouldn't show a notice if the quantity limits change, but the item's quantity is still OK.
* Add tests for notifyQuantityChanges
* Show notice when multiple_of is updated
* Update test to test for multiple_of changes
* Remove empty export
* Remove controls from cart data store
Not needed anymore since the exported value from the shared-controls file was empty.
* Export a control and async function for apiFetchWithHeaders
This is required because async functions cannot be called from sync generators.
* Use control version of apiFetchWithHeaders in the collections store
* Improve comments and remove incorrect TypeScript
* Update assets/js/data/cart/actions.ts
Co-authored-by: Mike Jolley <mike.jolley@me.com>
* Update ResolveSelectFromMap to include selectors too
* Update TS in actions
* Use finally to remove duplicate code
* remove item pending delete/qty update after action runs in all cases
This will also reset the state when the request to remove it/change quantity errors
* Remove unnecessary type from param.
Not needed because we have TS now. The description can stay though, it is useful.
* Update snackbar wording to use active voice
* Remove old WP version check
* Set max quantity to high number instead of null
This would only happen in a niche case, and would require several TS changes to fix, so it's better to set it as a number here. 9999 should be high enough, and is the default quantity limit set below in get_product_quantity_limit
* Set code on woocommerce_rest_cart_invalid_key to 409
This is so the cart is returned in the response, so the client can update.
* Fix typo in comment and add CartSelectFromMap
* Remove unnecessary docblock
* Add getItemsPendingDelete selector
This is needed so we can show a notice for items that are unexpectedly removed from the cart. We need to know which ones are pending delete so we can skip showing the notice for them.
* Add type for notifyQuantityChanges args and change args to object
* Add notifyIfRemoved function
This will check items that have been removed and show a notice for them.
* Fix TS in receiveCart & pass itemsPendingDelete to notifyQuantiyChanges
* Update wording on removal notice
* Update types for notifyQuantityChanges args
* Update tests to reflect new wording and args being an object
* Check item is truth before running comparison of keys
* Update tests for unexpectedly and expectedly removed items
* Ignore print_r to satisfy phpcs
* Update PHP tests to reflect correct response code when deleting items
* Remove unnecessary controls and dispatch events directly from thunk
Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-12-16 16:06:37 +00:00
|
|
|
* An action creator that dispatches the plain action responsible for setting the cart data in the store.
|
2020-02-25 11:36:53 +00:00
|
|
|
*/
|
2024-10-23 16:15:41 +00:00
|
|
|
export function setCartData( cart: Cart ) {
|
2020-02-25 11:36:53 +00:00
|
|
|
return {
|
Add notice on quantity change and update `wc/store/cart` to use thunks (https://github.com/woocommerce/woocommerce-blocks/pull/7938)
* Add receiveCart thunk
* Add mapCartResponseToCart helper
* Add getItemsPendingQuantityUpdate selector
* Update cart resolvers to be thunks
* Remove RECEIVE_CART action and replace with SET_CART_DATA
receiveCart will turn into a thunk.
* Add notifyQuantityChanges functions
* Remove receiveCart from action type definition, replace with setCartData
* Move apiFetchWithHeaders out of controls
This will just be a normal function since we'll be updating actions to thunks which will use this instead of a control.
* Include thunks in actions file
* Update receiveCart action to setCartData
* Update applyCoupon action to a thunk
* Update useStoreCartCoupons to get action from correct place
* Update StoreCartCoupon types
* Add types for Thunk and ThunkReturnType in mapped-types
* Change applyCoupon to a thunk
* Get applyCoupon, removeCoupon, receiveApplyingCoupon from useDispatch
This is to separate the concerns of actions vs. selectors. Previously the actions were fetched during useSelect which is not a pattern we use anywhere else in the codebase. Since we updated the MapToDispatch type, we can now get correctly typed thunks from the data store.
* Improve apiFetchWithHeaders typings
* Convert removeCoupon from generator to thunk
* Add applyCoupon and removeCoupon to CartAction type
* Remove unused old-style type-def
* Add receiveApplyingCoupon & receiveRemovingCoupon to StoreCartCoupon
* Correct issues with StoreCartCoupon type
These were not intended to reflect the actions in data store, rather the functions offered by the useStoreCartCoupons hook.
* Update applyExtensionCartUpdate to a thunk
* Update addItemToCart to thunk
* Add ResolveSelectFromMap type that works with thunks
* Add CartDispatchFromMap and CartResolveSelectFromMap types
We can add this to all data stores to get them working with thunks properly.
* Add docs and update generic name in ResolveSelectFromMap
* Add correct types for thunk resolvers in cart data store
* Update removeItemFromCart to thunk
* Update apiFetchWithHeaders to use generic
* Update selectShippingRate to thunk
* Update resolver tests to test correct thunk functionality
* Update updateCustomerData to thunk
* Update reducer test to reflect new action name
* Update comments on CartDispatchFromMap and CartResolveSelectFromMap
* Add quantity_limits to preview cart
* Make notices speak when shown
* Remove copilot comment
* Add isWithinQuantityLimits function
This is because we shouldn't show a notice if the quantity limits change, but the item's quantity is still OK.
* Add tests for notifyQuantityChanges
* Show notice when multiple_of is updated
* Update test to test for multiple_of changes
* Remove empty export
* Remove controls from cart data store
Not needed anymore since the exported value from the shared-controls file was empty.
* Export a control and async function for apiFetchWithHeaders
This is required because async functions cannot be called from sync generators.
* Use control version of apiFetchWithHeaders in the collections store
* Improve comments and remove incorrect TypeScript
* Update assets/js/data/cart/actions.ts
Co-authored-by: Mike Jolley <mike.jolley@me.com>
* Update ResolveSelectFromMap to include selectors too
* Update TS in actions
* Use finally to remove duplicate code
* remove item pending delete/qty update after action runs in all cases
This will also reset the state when the request to remove it/change quantity errors
* Remove unnecessary type from param.
Not needed because we have TS now. The description can stay though, it is useful.
* Update snackbar wording to use active voice
* Remove old WP version check
* Set max quantity to high number instead of null
This would only happen in a niche case, and would require several TS changes to fix, so it's better to set it as a number here. 9999 should be high enough, and is the default quantity limit set below in get_product_quantity_limit
* Set code on woocommerce_rest_cart_invalid_key to 409
This is so the cart is returned in the response, so the client can update.
* Fix typo in comment and add CartSelectFromMap
* Remove unnecessary docblock
* Add getItemsPendingDelete selector
This is needed so we can show a notice for items that are unexpectedly removed from the cart. We need to know which ones are pending delete so we can skip showing the notice for them.
* Add type for notifyQuantityChanges args and change args to object
* Add notifyIfRemoved function
This will check items that have been removed and show a notice for them.
* Fix TS in receiveCart & pass itemsPendingDelete to notifyQuantiyChanges
* Update wording on removal notice
* Update types for notifyQuantityChanges args
* Update tests to reflect new wording and args being an object
* Check item is truth before running comparison of keys
* Update tests for unexpectedly and expectedly removed items
* Ignore print_r to satisfy phpcs
* Update PHP tests to reflect correct response code when deleting items
* Remove unnecessary controls and dispatch events directly from thunk
Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-12-16 16:06:37 +00:00
|
|
|
type: types.SET_CART_DATA,
|
2021-02-24 01:36:24 +00:00
|
|
|
response: cart,
|
2021-05-16 17:59:32 +00:00
|
|
|
};
|
2024-10-23 16:15:41 +00:00
|
|
|
}
|
2020-02-25 11:36:53 +00:00
|
|
|
|
2023-01-19 16:40:52 +00:00
|
|
|
/**
|
|
|
|
* An action creator that dispatches the plain action responsible for setting the cart error data in the store.
|
|
|
|
*/
|
2024-10-23 16:15:41 +00:00
|
|
|
export function setErrorData( error: ApiErrorResponse | null ) {
|
2023-01-19 16:40:52 +00:00
|
|
|
return {
|
|
|
|
type: types.SET_ERROR_DATA,
|
|
|
|
error,
|
|
|
|
};
|
2024-10-23 16:15:41 +00:00
|
|
|
}
|
2023-01-19 16:40:52 +00:00
|
|
|
|
2020-02-25 11:36:53 +00:00
|
|
|
/**
|
|
|
|
* Returns an action object used to track when a coupon is applying.
|
|
|
|
*/
|
2024-10-23 16:15:41 +00:00
|
|
|
export function receiveApplyingCoupon( couponCode: string ) {
|
|
|
|
return {
|
2020-02-25 11:36:53 +00:00
|
|
|
type: types.APPLYING_COUPON,
|
|
|
|
couponCode,
|
2024-10-23 16:15:41 +00:00
|
|
|
};
|
|
|
|
}
|
2020-02-25 11:36:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an action object used to track when a coupon is removing.
|
|
|
|
*/
|
2024-10-23 16:15:41 +00:00
|
|
|
export function receiveRemovingCoupon( couponCode: string ) {
|
|
|
|
return {
|
2020-02-25 11:36:53 +00:00
|
|
|
type: types.REMOVING_COUPON,
|
|
|
|
couponCode,
|
2024-10-23 16:15:41 +00:00
|
|
|
};
|
|
|
|
}
|
2020-02-25 11:36:53 +00:00
|
|
|
|
2020-03-19 11:50:51 +00:00
|
|
|
/**
|
|
|
|
* Returns an action object for updating a single cart item in the store.
|
|
|
|
*/
|
2024-10-23 16:15:41 +00:00
|
|
|
export function receiveCartItem( response: CartResponseItem | null = null ) {
|
|
|
|
return {
|
2020-03-19 11:50:51 +00:00
|
|
|
type: types.RECEIVE_CART_ITEM,
|
|
|
|
cartItem: response,
|
2024-10-23 16:15:41 +00:00
|
|
|
};
|
|
|
|
}
|
2020-03-19 11:50:51 +00:00
|
|
|
|
|
|
|
/**
|
2021-02-24 01:36:24 +00:00
|
|
|
* Returns an action object to indicate if the specified cart item quantity is
|
|
|
|
* being updated.
|
2020-03-19 11:50:51 +00:00
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {string} cartItemKey Cart item being updated.
|
|
|
|
* @param {boolean} [isPendingQuantity=true] Flag for update state; true if API
|
|
|
|
* request is pending.
|
2020-03-19 11:50:51 +00:00
|
|
|
*/
|
2024-10-23 16:15:41 +00:00
|
|
|
export function itemIsPendingQuantity(
|
2021-02-24 01:36:24 +00:00
|
|
|
cartItemKey: string,
|
|
|
|
isPendingQuantity = true
|
2024-10-23 16:15:41 +00:00
|
|
|
) {
|
|
|
|
return {
|
2020-04-07 11:03:22 +00:00
|
|
|
type: types.ITEM_PENDING_QUANTITY,
|
2020-03-19 11:50:51 +00:00
|
|
|
cartItemKey,
|
2020-04-07 11:03:22 +00:00
|
|
|
isPendingQuantity,
|
2024-10-23 16:15:41 +00:00
|
|
|
};
|
|
|
|
}
|
2020-03-19 11:50:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an action object to remove a cart item from the store.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {string} cartItemKey Cart item to remove.
|
|
|
|
* @param {boolean} [isPendingDelete=true] Flag for update state; true if API
|
|
|
|
* request is pending.
|
2020-03-19 11:50:51 +00:00
|
|
|
*/
|
2024-10-23 16:15:41 +00:00
|
|
|
export function itemIsPendingDelete(
|
2021-02-24 01:36:24 +00:00
|
|
|
cartItemKey: string,
|
|
|
|
isPendingDelete = true
|
2024-10-23 16:15:41 +00:00
|
|
|
) {
|
|
|
|
return {
|
2020-03-19 11:50:51 +00:00
|
|
|
type: types.RECEIVE_REMOVED_ITEM,
|
|
|
|
cartItemKey,
|
2020-04-07 11:03:22 +00:00
|
|
|
isPendingDelete,
|
2024-10-23 16:15:41 +00:00
|
|
|
};
|
|
|
|
}
|
2023-01-30 15:12:17 +00:00
|
|
|
|
2021-03-24 13:28:11 +00:00
|
|
|
/**
|
|
|
|
* Returns an action object to mark the cart data in the store as stale.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {boolean} [isCartDataStale=true] Flag to mark cart data as stale; true if
|
|
|
|
* lastCartUpdate timestamp is newer than the
|
|
|
|
* one in wcSettings.
|
2021-03-24 13:28:11 +00:00
|
|
|
*/
|
2024-10-23 16:15:41 +00:00
|
|
|
export function setIsCartDataStale( isCartDataStale = true ) {
|
|
|
|
return {
|
2021-03-24 13:28:11 +00:00
|
|
|
type: types.SET_IS_CART_DATA_STALE,
|
|
|
|
isCartDataStale,
|
2024-10-23 16:15:41 +00:00
|
|
|
};
|
|
|
|
}
|
2020-03-19 11:50:51 +00:00
|
|
|
|
|
|
|
/**
|
2021-02-24 01:36:24 +00:00
|
|
|
* Returns an action object used to track when customer data is being updated
|
|
|
|
* (billing and/or shipping).
|
2020-03-19 11:50:51 +00:00
|
|
|
*/
|
2024-10-23 16:15:41 +00:00
|
|
|
export function updatingCustomerData( isResolving: boolean ) {
|
|
|
|
return {
|
2020-11-20 15:13:35 +00:00
|
|
|
type: types.UPDATING_CUSTOMER_DATA,
|
2020-03-19 11:50:51 +00:00
|
|
|
isResolving,
|
2024-10-23 16:15:41 +00:00
|
|
|
};
|
|
|
|
}
|
2020-03-19 11:50:51 +00:00
|
|
|
|
2020-03-31 15:40:27 +00:00
|
|
|
/**
|
|
|
|
* Returns an action object used to track whether the shipping rate is being
|
|
|
|
* selected or not.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {boolean} isResolving True if shipping rate is being selected.
|
2020-03-31 15:40:27 +00:00
|
|
|
*/
|
2024-10-23 16:15:41 +00:00
|
|
|
export function shippingRatesBeingSelected( isResolving: boolean ) {
|
|
|
|
return {
|
2020-03-31 15:40:27 +00:00
|
|
|
type: types.UPDATING_SELECTED_SHIPPING_RATE,
|
|
|
|
isResolving,
|
Add notice on quantity change and update `wc/store/cart` to use thunks (https://github.com/woocommerce/woocommerce-blocks/pull/7938)
* Add receiveCart thunk
* Add mapCartResponseToCart helper
* Add getItemsPendingQuantityUpdate selector
* Update cart resolvers to be thunks
* Remove RECEIVE_CART action and replace with SET_CART_DATA
receiveCart will turn into a thunk.
* Add notifyQuantityChanges functions
* Remove receiveCart from action type definition, replace with setCartData
* Move apiFetchWithHeaders out of controls
This will just be a normal function since we'll be updating actions to thunks which will use this instead of a control.
* Include thunks in actions file
* Update receiveCart action to setCartData
* Update applyCoupon action to a thunk
* Update useStoreCartCoupons to get action from correct place
* Update StoreCartCoupon types
* Add types for Thunk and ThunkReturnType in mapped-types
* Change applyCoupon to a thunk
* Get applyCoupon, removeCoupon, receiveApplyingCoupon from useDispatch
This is to separate the concerns of actions vs. selectors. Previously the actions were fetched during useSelect which is not a pattern we use anywhere else in the codebase. Since we updated the MapToDispatch type, we can now get correctly typed thunks from the data store.
* Improve apiFetchWithHeaders typings
* Convert removeCoupon from generator to thunk
* Add applyCoupon and removeCoupon to CartAction type
* Remove unused old-style type-def
* Add receiveApplyingCoupon & receiveRemovingCoupon to StoreCartCoupon
* Correct issues with StoreCartCoupon type
These were not intended to reflect the actions in data store, rather the functions offered by the useStoreCartCoupons hook.
* Update applyExtensionCartUpdate to a thunk
* Update addItemToCart to thunk
* Add ResolveSelectFromMap type that works with thunks
* Add CartDispatchFromMap and CartResolveSelectFromMap types
We can add this to all data stores to get them working with thunks properly.
* Add docs and update generic name in ResolveSelectFromMap
* Add correct types for thunk resolvers in cart data store
* Update removeItemFromCart to thunk
* Update apiFetchWithHeaders to use generic
* Update selectShippingRate to thunk
* Update resolver tests to test correct thunk functionality
* Update updateCustomerData to thunk
* Update reducer test to reflect new action name
* Update comments on CartDispatchFromMap and CartResolveSelectFromMap
* Add quantity_limits to preview cart
* Make notices speak when shown
* Remove copilot comment
* Add isWithinQuantityLimits function
This is because we shouldn't show a notice if the quantity limits change, but the item's quantity is still OK.
* Add tests for notifyQuantityChanges
* Show notice when multiple_of is updated
* Update test to test for multiple_of changes
* Remove empty export
* Remove controls from cart data store
Not needed anymore since the exported value from the shared-controls file was empty.
* Export a control and async function for apiFetchWithHeaders
This is required because async functions cannot be called from sync generators.
* Use control version of apiFetchWithHeaders in the collections store
* Improve comments and remove incorrect TypeScript
* Update assets/js/data/cart/actions.ts
Co-authored-by: Mike Jolley <mike.jolley@me.com>
* Update ResolveSelectFromMap to include selectors too
* Update TS in actions
* Use finally to remove duplicate code
* remove item pending delete/qty update after action runs in all cases
This will also reset the state when the request to remove it/change quantity errors
* Remove unnecessary type from param.
Not needed because we have TS now. The description can stay though, it is useful.
* Update snackbar wording to use active voice
* Remove old WP version check
* Set max quantity to high number instead of null
This would only happen in a niche case, and would require several TS changes to fix, so it's better to set it as a number here. 9999 should be high enough, and is the default quantity limit set below in get_product_quantity_limit
* Set code on woocommerce_rest_cart_invalid_key to 409
This is so the cart is returned in the response, so the client can update.
* Fix typo in comment and add CartSelectFromMap
* Remove unnecessary docblock
* Add getItemsPendingDelete selector
This is needed so we can show a notice for items that are unexpectedly removed from the cart. We need to know which ones are pending delete so we can skip showing the notice for them.
* Add type for notifyQuantityChanges args and change args to object
* Add notifyIfRemoved function
This will check items that have been removed and show a notice for them.
* Fix TS in receiveCart & pass itemsPendingDelete to notifyQuantiyChanges
* Update wording on removal notice
* Update types for notifyQuantityChanges args
* Update tests to reflect new wording and args being an object
* Check item is truth before running comparison of keys
* Update tests for unexpectedly and expectedly removed items
* Ignore print_r to satisfy phpcs
* Update PHP tests to reflect correct response code when deleting items
* Remove unnecessary controls and dispatch events directly from thunk
Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-12-16 16:06:37 +00:00
|
|
|
};
|
2024-10-23 16:15:41 +00:00
|
|
|
}
|
2020-03-05 19:54:05 +00:00
|
|
|
|
2022-02-22 17:45:01 +00:00
|
|
|
/**
|
2022-06-10 16:59:25 +00:00
|
|
|
* Sets billing address locally, as opposed to updateCustomerData which sends it to the server.
|
2022-02-22 17:45:01 +00:00
|
|
|
*/
|
2024-10-23 16:15:41 +00:00
|
|
|
export function setBillingAddress( billingAddress: Partial< BillingAddress > ) {
|
|
|
|
return { type: types.SET_BILLING_ADDRESS, billingAddress };
|
|
|
|
}
|
2022-02-22 17:45:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets shipping address locally, as opposed to updateCustomerData which sends it to the server.
|
|
|
|
*/
|
2024-10-23 16:15:41 +00:00
|
|
|
export function setShippingAddress(
|
2022-02-22 17:45:01 +00:00
|
|
|
shippingAddress: Partial< ShippingAddress >
|
2024-10-23 16:15:41 +00:00
|
|
|
) {
|
|
|
|
return { type: types.SET_SHIPPING_ADDRESS, shippingAddress };
|
|
|
|
}
|
2021-02-24 01:36:24 +00:00
|
|
|
|
2024-10-23 16:15:41 +00:00
|
|
|
export type Actions =
|
2023-01-30 15:12:17 +00:00
|
|
|
| typeof itemIsPendingDelete
|
|
|
|
| typeof itemIsPendingQuantity
|
|
|
|
| typeof receiveApplyingCoupon
|
|
|
|
| typeof receiveCartItem
|
|
|
|
| typeof receiveRemovingCoupon
|
2022-06-10 16:59:25 +00:00
|
|
|
| typeof setBillingAddress
|
2023-01-30 15:12:17 +00:00
|
|
|
| typeof setCartData
|
2023-01-19 16:40:52 +00:00
|
|
|
| typeof setErrorData
|
2021-04-06 11:15:14 +00:00
|
|
|
| typeof setIsCartDataStale
|
2023-01-30 15:12:17 +00:00
|
|
|
| typeof setShippingAddress
|
|
|
|
| typeof shippingRatesBeingSelected
|
|
|
|
| typeof updatingCustomerData;
|
|
|
|
|
|
|
|
export type CartAction = ReturnOrGeneratorYieldUnion< Actions | Thunks >;
|