@tapcart/mobile-components 0.8.48 → 0.8.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/hooks/use-mock-cart.d.ts +3 -0
- package/dist/components/hooks/use-mock-cart.d.ts.map +1 -1
- package/dist/components/hooks/use-mock-cart.js +19 -2
- package/dist/components/ui/Input/useInput.d.ts +1 -10
- package/dist/components/ui/Input/useInput.d.ts.map +1 -1
- package/dist/components/ui/button.d.ts +825 -8
- package/dist/components/ui/button.d.ts.map +1 -1
- package/dist/components/ui/chip.d.ts +2 -2
- package/dist/components/ui/favorite.d.ts +1 -1
- package/dist/components/ui/progress-bar.d.ts.map +1 -1
- package/dist/components/ui/progress-bar.js +68 -59
- package/dist/components/ui/quantity-picker.d.ts +3 -1
- package/dist/components/ui/quantity-picker.d.ts.map +1 -1
- package/dist/components/ui/quantity-picker.js +49 -12
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/lib/cart.util.d.ts +8 -15
- package/dist/lib/cart.util.d.ts.map +1 -1
- package/dist/lib/cart.util.js +21 -12
- package/dist/lib/utils.d.ts +826 -17
- package/dist/lib/utils.d.ts.map +1 -1
- package/dist/lib/variablesCart.util.d.ts +35 -0
- package/dist/lib/variablesCart.util.d.ts.map +1 -0
- package/dist/lib/variablesCart.util.js +236 -0
- package/dist/lib/variablesCart.util.test.d.ts +2 -0
- package/dist/lib/variablesCart.util.test.d.ts.map +1 -0
- package/dist/lib/variablesCart.util.test.js +387 -0
- package/package.json +2 -2
package/dist/lib/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../lib/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,4BAA4B,EAC5B,yBAAyB,EACzB,UAAU,EACV,eAAe,EACf,OAAO,EACP,QAAQ,EACR,gBAAgB,EACjB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AA;IAC3B,UAAU,EAAE,CAAC,SAAS,GAAG,UAAU,CAAC,EAAE,CAAA;CACvC,CAAA;AAoDD,eAAO,MAAM,kBAAkB,UACtB,UAAU,WACR,GAAG,KACX,OAyBF,CAAA"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { CartState, CartCalculatedDiscount, CartCalculatedAppliedGiftCard } from "app-studio-types";
|
|
2
|
+
export declare const getVariablesVariantPrice: ({ variantPrice, variantCompareAtPrice, quantity, currencyCode, sellingPlanAllocation, }: {
|
|
3
|
+
variantPrice: number;
|
|
4
|
+
variantCompareAtPrice: number;
|
|
5
|
+
quantity: number;
|
|
6
|
+
currencyCode: string;
|
|
7
|
+
sellingPlanAllocation?: {
|
|
8
|
+
priceAdjustments: {
|
|
9
|
+
perDeliveryPrice?: number;
|
|
10
|
+
price: number;
|
|
11
|
+
compareAtPrice?: number;
|
|
12
|
+
}[];
|
|
13
|
+
} | undefined;
|
|
14
|
+
}) => {
|
|
15
|
+
price: number;
|
|
16
|
+
compareAtPrice: number;
|
|
17
|
+
currency: string;
|
|
18
|
+
isSale: boolean;
|
|
19
|
+
};
|
|
20
|
+
export declare const isVariablesOrderLevelDiscount: (code: string, cart: CartState) => boolean;
|
|
21
|
+
export declare const isVariablesLineItemDiscount: (code: string, cart: CartState) => boolean;
|
|
22
|
+
export type VariablesCalculatedData = {
|
|
23
|
+
orderAndLineItemDiscounts: CartCalculatedDiscount[];
|
|
24
|
+
appliedGiftCards: CartCalculatedAppliedGiftCard[];
|
|
25
|
+
isFreeShipping: boolean;
|
|
26
|
+
discountsTotalAmount: number;
|
|
27
|
+
giftCardsTotalAmount: number;
|
|
28
|
+
salesAmount: number;
|
|
29
|
+
totalCompareAtPrice: number;
|
|
30
|
+
totalDiscountedPrice: number;
|
|
31
|
+
totalSavedAmount: number;
|
|
32
|
+
};
|
|
33
|
+
export declare const DEFAULT_VARIABLES_CALCULATED_DATA: VariablesCalculatedData;
|
|
34
|
+
export declare const getVariablesCalculatedCartData: (cart: CartState) => VariablesCalculatedData;
|
|
35
|
+
//# sourceMappingURL=variablesCart.util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"variablesCart.util.d.ts","sourceRoot":"","sources":["../../lib/variablesCart.util.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,sBAAsB,EACtB,6BAA6B,EAO9B,MAAM,kBAAkB,CAAA;AAEzB,eAAO,MAAM,wBAAwB;kBAOrB,MAAM;2BACG,MAAM;cACnB,MAAM;kBACF,MAAM;;0BAEA;YAChB,gBAAgB,CAAC,EAAE,MAAM,CAAA;YACzB,KAAK,EAAE,MAAM,CAAA;YACb,cAAc,CAAC,EAAE,MAAM,CAAA;SACxB,EAAE;;;;;;;CA4BN,CAAA;AAuED,eAAO,MAAM,6BAA6B,SAClC,MAAM,QACN,SAAS,YAOhB,CAAA;AAED,eAAO,MAAM,2BAA2B,SAAU,MAAM,QAAQ,SAAS,YAMxE,CAAA;AAwJD,MAAM,MAAM,uBAAuB,GAAG;IACpC,yBAAyB,EAAE,sBAAsB,EAAE,CAAA;IACnD,gBAAgB,EAAE,6BAA6B,EAAE,CAAA;IACjD,cAAc,EAAE,OAAO,CAAA;IACvB,oBAAoB,EAAE,MAAM,CAAA;IAC5B,oBAAoB,EAAE,MAAM,CAAA;IAC5B,WAAW,EAAE,MAAM,CAAA;IACnB,mBAAmB,EAAE,MAAM,CAAA;IAC3B,oBAAoB,EAAE,MAAM,CAAA;IAC5B,gBAAgB,EAAE,MAAM,CAAA;CACzB,CAAA;AAED,eAAO,MAAM,iCAAiC,EAAE,uBAU/C,CAAA;AAED,eAAO,MAAM,8BAA8B,SACnC,SAAS,KACd,uBA8BF,CAAA"}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import { DiscountApplicationTargetType, } from "app-studio-types";
|
|
3
|
+
export const getVariablesVariantPrice = ({ variantPrice, variantCompareAtPrice, quantity, currencyCode, sellingPlanAllocation = { priceAdjustments: [] }, }) => {
|
|
4
|
+
var _a;
|
|
5
|
+
const isSubscription = ((_a = sellingPlanAllocation === null || sellingPlanAllocation === void 0 ? void 0 : sellingPlanAllocation.priceAdjustments) === null || _a === void 0 ? void 0 : _a.length) > 0;
|
|
6
|
+
if (isSubscription) {
|
|
7
|
+
const { perDeliveryPrice, price, compareAtPrice } = sellingPlanAllocation.priceAdjustments[0];
|
|
8
|
+
const effectivePrice = perDeliveryPrice || price;
|
|
9
|
+
const subscriptionUnitPrice = perDeliveryPrice || 0;
|
|
10
|
+
const effectiveCompareAtPrice = compareAtPrice || price || 0;
|
|
11
|
+
return {
|
|
12
|
+
price: subscriptionUnitPrice * quantity,
|
|
13
|
+
compareAtPrice: effectiveCompareAtPrice * quantity,
|
|
14
|
+
currency: currencyCode,
|
|
15
|
+
isSale: subscriptionUnitPrice < effectiveCompareAtPrice,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
const variantUnitPrice = variantPrice || 0;
|
|
19
|
+
const variantComparePrice = variantCompareAtPrice || 0;
|
|
20
|
+
return {
|
|
21
|
+
price: variantUnitPrice * quantity,
|
|
22
|
+
compareAtPrice: variantComparePrice * quantity,
|
|
23
|
+
currency: currencyCode,
|
|
24
|
+
isSale: variantUnitPrice < variantComparePrice,
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
const getOrderLevelDiscounts = (cart) => {
|
|
28
|
+
// CartState doesn't have discountAllocations directly
|
|
29
|
+
// Need to check if cart has discountAllocations property
|
|
30
|
+
const discountAllocations = cart === null || cart === void 0 ? void 0 : cart.discountAllocations;
|
|
31
|
+
if (!discountAllocations)
|
|
32
|
+
return [];
|
|
33
|
+
const discountMap = discountAllocations
|
|
34
|
+
.filter((discount) => discount.targetType !== DiscountApplicationTargetType.ShippingLine)
|
|
35
|
+
.reduce((acc, discount) => {
|
|
36
|
+
var _a, _b, _c;
|
|
37
|
+
// Handle both traditional format and new format with kind.manual.code
|
|
38
|
+
let code = "unknown";
|
|
39
|
+
if (discount.code) {
|
|
40
|
+
code = discount.code.toUpperCase();
|
|
41
|
+
}
|
|
42
|
+
else if ((_b = (_a = discount.kind) === null || _a === void 0 ? void 0 : _a.manual) === null || _b === void 0 ? void 0 : _b.code) {
|
|
43
|
+
code = discount.kind.manual.code.toUpperCase();
|
|
44
|
+
}
|
|
45
|
+
if (!acc[code]) {
|
|
46
|
+
acc[code] = 0;
|
|
47
|
+
}
|
|
48
|
+
// Handle both formats for amount
|
|
49
|
+
if ((_c = discount.discountedAmount) === null || _c === void 0 ? void 0 : _c.amount) {
|
|
50
|
+
acc[code] += parseFloat(discount.discountedAmount.amount || "0");
|
|
51
|
+
}
|
|
52
|
+
else if (discount.amount) {
|
|
53
|
+
acc[code] += parseFloat(String(discount.amount));
|
|
54
|
+
}
|
|
55
|
+
return acc;
|
|
56
|
+
}, {});
|
|
57
|
+
if (!discountMap)
|
|
58
|
+
return [];
|
|
59
|
+
return Object.entries(discountMap)
|
|
60
|
+
.map(([code, amount]) => ({
|
|
61
|
+
id: code,
|
|
62
|
+
amount: amount,
|
|
63
|
+
name: `Discount - ${code}`,
|
|
64
|
+
type: "ORDER_LEVEL",
|
|
65
|
+
}))
|
|
66
|
+
.filter((discount) => cart.discounts
|
|
67
|
+
.map((d) => d.toUpperCase())
|
|
68
|
+
.includes(discount.id.toUpperCase()));
|
|
69
|
+
};
|
|
70
|
+
const getShippingDiscounts = (cart) => {
|
|
71
|
+
// CartState doesn't have discountAllocations directly
|
|
72
|
+
const discountAllocations = cart.discountAllocations;
|
|
73
|
+
if (!discountAllocations)
|
|
74
|
+
return [];
|
|
75
|
+
return discountAllocations
|
|
76
|
+
.filter((discount) => discount.targetType === DiscountApplicationTargetType.ShippingLine)
|
|
77
|
+
.map((discount) => {
|
|
78
|
+
var _a;
|
|
79
|
+
return ({
|
|
80
|
+
amount: ((_a = discount === null || discount === void 0 ? void 0 : discount.discountedAmount) === null || _a === void 0 ? void 0 : _a.amount) || 0,
|
|
81
|
+
code: discount.code,
|
|
82
|
+
type: discount.targetType,
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
export const isVariablesOrderLevelDiscount = (code, cart) => {
|
|
87
|
+
const orderLevelDiscounts = getOrderLevelDiscounts(cart);
|
|
88
|
+
return orderLevelDiscounts.some((discount) => (discount.id || "").toUpperCase() === (code || "").toUpperCase());
|
|
89
|
+
};
|
|
90
|
+
export const isVariablesLineItemDiscount = (code, cart) => {
|
|
91
|
+
const lineItemDiscounts = getLineItemDiscounts(cart);
|
|
92
|
+
return lineItemDiscounts.some((discount) => (discount.id || "").toUpperCase() === (code || "").toUpperCase());
|
|
93
|
+
};
|
|
94
|
+
const getAppliedGiftCards = (cart) => {
|
|
95
|
+
// Access appliedGiftCards directly from CartState
|
|
96
|
+
const appliedGiftCards = cart === null || cart === void 0 ? void 0 : cart.appliedGiftCards;
|
|
97
|
+
if (!appliedGiftCards ||
|
|
98
|
+
!Array.isArray(appliedGiftCards) ||
|
|
99
|
+
appliedGiftCards.length === 0)
|
|
100
|
+
return [];
|
|
101
|
+
return appliedGiftCards.map((giftCard) => ({
|
|
102
|
+
id: giftCard.id,
|
|
103
|
+
amount: +giftCard.amountUsed.amount,
|
|
104
|
+
name: `Gift Card - ${giftCard.lastCharacters}`,
|
|
105
|
+
}));
|
|
106
|
+
};
|
|
107
|
+
const getSalesAmount = (cart) => {
|
|
108
|
+
return cart.items.reduce((acc, item) => {
|
|
109
|
+
// Get price and compareAtPrice directly from the CartItem
|
|
110
|
+
const itemPrice = parseFloat((item.price || 0).toString());
|
|
111
|
+
const itemCompareAtPrice = parseFloat((item.compareAtPrice || 0).toString());
|
|
112
|
+
// Create a custom sellingPlanAllocation if the item has one
|
|
113
|
+
let customSellingPlanAllocation;
|
|
114
|
+
if (item.sellingPlanAllocation) {
|
|
115
|
+
customSellingPlanAllocation = {
|
|
116
|
+
priceAdjustments: item.sellingPlanAllocation.priceAdjustments.map((adj) => ({
|
|
117
|
+
price: parseFloat(adj.price.amount),
|
|
118
|
+
compareAtPrice: adj.compareAtPrice
|
|
119
|
+
? parseFloat(adj.compareAtPrice.amount)
|
|
120
|
+
: undefined,
|
|
121
|
+
perDeliveryPrice: adj.perDeliveryPrice
|
|
122
|
+
? parseFloat(adj.perDeliveryPrice.amount)
|
|
123
|
+
: undefined,
|
|
124
|
+
})),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
const { compareAtPrice, price } = getVariablesVariantPrice({
|
|
128
|
+
quantity: item.quantity || 1,
|
|
129
|
+
variantPrice: itemPrice,
|
|
130
|
+
variantCompareAtPrice: itemCompareAtPrice,
|
|
131
|
+
currencyCode: item.currencyCode || "USD",
|
|
132
|
+
sellingPlanAllocation: customSellingPlanAllocation,
|
|
133
|
+
});
|
|
134
|
+
if (compareAtPrice && compareAtPrice > price) {
|
|
135
|
+
return acc + (compareAtPrice - price);
|
|
136
|
+
}
|
|
137
|
+
return acc;
|
|
138
|
+
}, 0);
|
|
139
|
+
};
|
|
140
|
+
const getLineItemDiscounts = (cart) => {
|
|
141
|
+
var _a;
|
|
142
|
+
const discountMap = cart.items.reduce((acc, item) => {
|
|
143
|
+
var _a;
|
|
144
|
+
(_a = item === null || item === void 0 ? void 0 : item.discounts) === null || _a === void 0 ? void 0 : _a.forEach((discount) => {
|
|
145
|
+
const code = discount === null || discount === void 0 ? void 0 : discount.code;
|
|
146
|
+
if (!code)
|
|
147
|
+
return;
|
|
148
|
+
if (!acc[code]) {
|
|
149
|
+
acc[code] = {
|
|
150
|
+
amount: 0,
|
|
151
|
+
type: discount.type,
|
|
152
|
+
readOnly: discount.readOnly,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
acc[code].amount += discount.amount;
|
|
156
|
+
});
|
|
157
|
+
return acc;
|
|
158
|
+
}, {});
|
|
159
|
+
if (!discountMap)
|
|
160
|
+
return [];
|
|
161
|
+
return (_a = Object.entries(discountMap)) === null || _a === void 0 ? void 0 : _a.map(([code, { amount, type, readOnly }]) => ({
|
|
162
|
+
id: code,
|
|
163
|
+
name: `Discount - ${code}`,
|
|
164
|
+
amount,
|
|
165
|
+
type,
|
|
166
|
+
readOnly,
|
|
167
|
+
}));
|
|
168
|
+
};
|
|
169
|
+
const getDiscountsTotalAmount = (orderAndLineItemDiscounts) => {
|
|
170
|
+
return orderAndLineItemDiscounts === null || orderAndLineItemDiscounts === void 0 ? void 0 : orderAndLineItemDiscounts.reduce((acc, discount) => acc + +discount.amount, 0);
|
|
171
|
+
};
|
|
172
|
+
const getGiftCardsTotalAmount = (appliedGiftCards) => {
|
|
173
|
+
return appliedGiftCards === null || appliedGiftCards === void 0 ? void 0 : appliedGiftCards.reduce((acc, giftCard) => acc + +giftCard.amount, 0);
|
|
174
|
+
};
|
|
175
|
+
const getTotalCompareAtPrice = (cart) => {
|
|
176
|
+
return cart.items.reduce((acc, item) => {
|
|
177
|
+
// Get compareAtPrice directly from the CartItem
|
|
178
|
+
const itemCompareAtPrice = item.compareAtPrice ||
|
|
179
|
+
(item.price ? parseFloat(item.price.toString()) : 0);
|
|
180
|
+
const quantity = item.quantity || 1;
|
|
181
|
+
return acc + +itemCompareAtPrice * quantity;
|
|
182
|
+
}, 0);
|
|
183
|
+
};
|
|
184
|
+
const getTotalDiscountedPrice = (totalCompareAtPrice, discountsTotalAmount, salesAmount, giftCardsTotalAmount) => {
|
|
185
|
+
const total = (totalCompareAtPrice === undefined || isNaN(totalCompareAtPrice)
|
|
186
|
+
? 0
|
|
187
|
+
: totalCompareAtPrice) -
|
|
188
|
+
(discountsTotalAmount === undefined || isNaN(discountsTotalAmount)
|
|
189
|
+
? 0
|
|
190
|
+
: discountsTotalAmount) -
|
|
191
|
+
(salesAmount === undefined || isNaN(salesAmount) ? 0 : salesAmount) -
|
|
192
|
+
(giftCardsTotalAmount === undefined || isNaN(giftCardsTotalAmount)
|
|
193
|
+
? 0
|
|
194
|
+
: giftCardsTotalAmount);
|
|
195
|
+
return isNaN(total) ? 0 : Math.max(0, total);
|
|
196
|
+
};
|
|
197
|
+
const getTotalSavedAmount = (cart) => {
|
|
198
|
+
const salesAmount = getSalesAmount(cart);
|
|
199
|
+
const totalDiscountsAmount = getDiscountsTotalAmount(getOrderLevelDiscounts(cart).concat(getLineItemDiscounts(cart)));
|
|
200
|
+
const totalGiftCardsAmount = getGiftCardsTotalAmount(getAppliedGiftCards(cart));
|
|
201
|
+
const totalSavedAmount = salesAmount + totalDiscountsAmount + totalGiftCardsAmount;
|
|
202
|
+
return totalSavedAmount;
|
|
203
|
+
};
|
|
204
|
+
export const DEFAULT_VARIABLES_CALCULATED_DATA = {
|
|
205
|
+
orderAndLineItemDiscounts: [],
|
|
206
|
+
appliedGiftCards: [],
|
|
207
|
+
isFreeShipping: false,
|
|
208
|
+
discountsTotalAmount: 0,
|
|
209
|
+
salesAmount: 0,
|
|
210
|
+
giftCardsTotalAmount: 0,
|
|
211
|
+
totalCompareAtPrice: 0,
|
|
212
|
+
totalDiscountedPrice: 0,
|
|
213
|
+
totalSavedAmount: 0,
|
|
214
|
+
};
|
|
215
|
+
export const getVariablesCalculatedCartData = (cart) => {
|
|
216
|
+
const orderAndLineItemDiscounts = getOrderLevelDiscounts(cart).concat(getLineItemDiscounts(cart));
|
|
217
|
+
const appliedGiftCards = getAppliedGiftCards(cart);
|
|
218
|
+
const isFreeShipping = getShippingDiscounts(cart).length > 0;
|
|
219
|
+
const discountsTotalAmount = getDiscountsTotalAmount(orderAndLineItemDiscounts);
|
|
220
|
+
const giftCardsTotalAmount = getGiftCardsTotalAmount(appliedGiftCards);
|
|
221
|
+
const salesAmount = getSalesAmount(cart);
|
|
222
|
+
const totalCompareAtPrice = getTotalCompareAtPrice(cart);
|
|
223
|
+
const totalDiscountedPrice = getTotalDiscountedPrice(totalCompareAtPrice, discountsTotalAmount, salesAmount, giftCardsTotalAmount);
|
|
224
|
+
const totalSavedAmount = getTotalSavedAmount(cart);
|
|
225
|
+
return {
|
|
226
|
+
orderAndLineItemDiscounts,
|
|
227
|
+
appliedGiftCards,
|
|
228
|
+
isFreeShipping,
|
|
229
|
+
discountsTotalAmount,
|
|
230
|
+
giftCardsTotalAmount,
|
|
231
|
+
salesAmount,
|
|
232
|
+
totalCompareAtPrice,
|
|
233
|
+
totalDiscountedPrice,
|
|
234
|
+
totalSavedAmount,
|
|
235
|
+
};
|
|
236
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"variablesCart.util.test.d.ts","sourceRoot":"","sources":["../../lib/variablesCart.util.test.ts"],"names":[],"mappings":""}
|