2020-02-25 11:36:53 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-03-10 13:22:05 +00:00
|
|
|
import { apiFetch } from '@wordpress/data-controls';
|
|
|
|
import { controls } from '@wordpress/data';
|
2021-02-24 01:36:24 +00:00
|
|
|
import { CartResponse, Cart } from '@woocommerce/types';
|
2020-02-25 11:36:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { receiveCart, receiveError } from './actions';
|
|
|
|
import { STORE_KEY, CART_API_ERROR } from './constants';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolver for retrieving all cart data.
|
|
|
|
*/
|
2021-02-24 01:36:24 +00:00
|
|
|
export function* getCartData(): Generator< unknown, void, CartResponse > {
|
2020-02-25 11:36:53 +00:00
|
|
|
const cartData = yield apiFetch( {
|
2022-02-23 12:00:45 +00:00
|
|
|
path: '/wc/store/v1/cart',
|
2020-02-25 11:36:53 +00:00
|
|
|
method: 'GET',
|
|
|
|
cache: 'no-store',
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( ! cartData ) {
|
|
|
|
yield receiveError( CART_API_ERROR );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-05 19:54:05 +00:00
|
|
|
yield receiveCart( cartData );
|
2020-02-25 11:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolver for retrieving cart totals.
|
|
|
|
*/
|
2021-02-24 01:36:24 +00:00
|
|
|
export function* getCartTotals(): Generator< unknown, void, Cart > {
|
2022-03-10 13:22:05 +00:00
|
|
|
yield controls.resolveSelect( STORE_KEY, 'getCartData' );
|
2020-02-25 11:36:53 +00:00
|
|
|
}
|