2021-02-24 01:36:24 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-12-15 12:45:38 +00:00
|
|
|
import type {
|
|
|
|
Cart,
|
|
|
|
CartMeta,
|
|
|
|
ApiErrorResponse,
|
|
|
|
CartShippingAddress,
|
|
|
|
CartBillingAddress,
|
|
|
|
} from '@woocommerce/types';
|
|
|
|
import { AddressField, defaultFields } from '@woocommerce/settings';
|
2021-02-24 01:36:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-08-13 13:28:41 +00:00
|
|
|
import {
|
|
|
|
EMPTY_CART_COUPONS,
|
|
|
|
EMPTY_CART_ITEMS,
|
2022-09-21 06:04:15 +00:00
|
|
|
EMPTY_CART_CROSS_SELLS,
|
2021-08-13 13:28:41 +00:00
|
|
|
EMPTY_CART_FEES,
|
|
|
|
EMPTY_CART_ITEM_ERRORS,
|
|
|
|
EMPTY_CART_ERRORS,
|
|
|
|
EMPTY_SHIPPING_RATES,
|
|
|
|
EMPTY_TAX_LINES,
|
2023-03-13 10:29:17 +00:00
|
|
|
EMPTY_PAYMENT_METHODS,
|
2021-08-13 13:28:41 +00:00
|
|
|
EMPTY_PAYMENT_REQUIREMENTS,
|
|
|
|
EMPTY_EXTENSIONS,
|
2022-07-08 05:53:24 +00:00
|
|
|
} from '../constants';
|
|
|
|
|
|
|
|
const EMPTY_PENDING_QUANTITY: [] = [];
|
|
|
|
const EMPTY_PENDING_DELETE: [] = [];
|
2021-02-24 01:36:24 +00:00
|
|
|
|
|
|
|
export interface CartState {
|
2022-12-19 15:30:13 +00:00
|
|
|
cartItemsPendingQuantity: string[];
|
|
|
|
cartItemsPendingDelete: string[];
|
2021-02-24 01:36:24 +00:00
|
|
|
cartData: Cart;
|
|
|
|
metaData: CartMeta;
|
2022-12-19 15:30:13 +00:00
|
|
|
errors: ApiErrorResponse[];
|
2021-02-24 01:36:24 +00:00
|
|
|
}
|
2023-12-15 12:45:38 +00:00
|
|
|
|
|
|
|
const shippingAddress: Partial<
|
|
|
|
CartShippingAddress & { email: AddressField }
|
|
|
|
> = {};
|
|
|
|
Object.keys( defaultFields ).forEach( ( key ) => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore the default fields contain keys for each field.
|
|
|
|
shippingAddress[ key ] = '';
|
|
|
|
} );
|
|
|
|
delete shippingAddress.email;
|
|
|
|
|
|
|
|
const billingAddress: Partial< CartBillingAddress & { email: AddressField } > =
|
|
|
|
{};
|
|
|
|
Object.keys( defaultFields ).forEach( ( key ) => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore the default fields contain keys for each field.
|
|
|
|
billingAddress[ key ] = '';
|
|
|
|
} );
|
|
|
|
|
2021-02-24 01:36:24 +00:00
|
|
|
export const defaultCartState: CartState = {
|
2021-08-13 13:28:41 +00:00
|
|
|
cartItemsPendingQuantity: EMPTY_PENDING_QUANTITY,
|
|
|
|
cartItemsPendingDelete: EMPTY_PENDING_DELETE,
|
2020-06-10 18:21:34 +00:00
|
|
|
cartData: {
|
2021-08-13 13:28:41 +00:00
|
|
|
coupons: EMPTY_CART_COUPONS,
|
|
|
|
shippingRates: EMPTY_SHIPPING_RATES,
|
2023-12-15 12:45:38 +00:00
|
|
|
shippingAddress: shippingAddress as CartShippingAddress,
|
|
|
|
billingAddress: billingAddress as CartBillingAddress,
|
2021-08-13 13:28:41 +00:00
|
|
|
items: EMPTY_CART_ITEMS,
|
2020-06-10 18:21:34 +00:00
|
|
|
itemsCount: 0,
|
|
|
|
itemsWeight: 0,
|
2022-09-21 06:04:15 +00:00
|
|
|
crossSells: EMPTY_CART_CROSS_SELLS,
|
2020-06-10 18:21:34 +00:00
|
|
|
needsShipping: true,
|
2021-02-24 01:36:24 +00:00
|
|
|
needsPayment: false,
|
|
|
|
hasCalculatedShipping: true,
|
2021-08-13 13:28:41 +00:00
|
|
|
fees: EMPTY_CART_FEES,
|
2020-06-10 18:21:34 +00:00
|
|
|
totals: {
|
|
|
|
currency_code: '',
|
|
|
|
currency_symbol: '',
|
|
|
|
currency_minor_unit: 2,
|
|
|
|
currency_decimal_separator: '.',
|
|
|
|
currency_thousand_separator: ',',
|
|
|
|
currency_prefix: '',
|
|
|
|
currency_suffix: '',
|
|
|
|
total_items: '0',
|
|
|
|
total_items_tax: '0',
|
|
|
|
total_fees: '0',
|
|
|
|
total_fees_tax: '0',
|
|
|
|
total_discount: '0',
|
|
|
|
total_discount_tax: '0',
|
|
|
|
total_shipping: '0',
|
|
|
|
total_shipping_tax: '0',
|
|
|
|
total_price: '0',
|
|
|
|
total_tax: '0',
|
2021-08-13 13:28:41 +00:00
|
|
|
tax_lines: EMPTY_TAX_LINES,
|
2020-06-10 18:21:34 +00:00
|
|
|
},
|
2021-08-13 13:28:41 +00:00
|
|
|
errors: EMPTY_CART_ITEM_ERRORS,
|
2023-03-13 10:29:17 +00:00
|
|
|
paymentMethods: EMPTY_PAYMENT_METHODS,
|
2021-08-13 13:28:41 +00:00
|
|
|
paymentRequirements: EMPTY_PAYMENT_REQUIREMENTS,
|
|
|
|
extensions: EMPTY_EXTENSIONS,
|
2021-02-24 01:36:24 +00:00
|
|
|
},
|
|
|
|
metaData: {
|
|
|
|
updatingCustomerData: false,
|
|
|
|
updatingSelectedRate: false,
|
|
|
|
applyingCoupon: '',
|
|
|
|
removingCoupon: '',
|
2021-03-24 13:28:11 +00:00
|
|
|
isCartDataStale: false,
|
2020-06-10 18:21:34 +00:00
|
|
|
},
|
2021-08-13 13:28:41 +00:00
|
|
|
errors: EMPTY_CART_ERRORS,
|
2020-06-10 18:21:34 +00:00
|
|
|
};
|