@tapcart/mobile-components 0.8.8 → 0.8.9
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 +5 -1
- package/dist/components/hooks/use-mock-cart.d.ts.map +1 -1
- package/dist/components/hooks/use-mock-cart.js +15 -9
- package/dist/lib/cart.util.d.ts +30 -2
- package/dist/lib/cart.util.d.ts.map +1 -1
- package/dist/lib/cart.util.js +47 -6
- package/package.json +1 -1
|
@@ -69,7 +69,11 @@ export declare const useMockCart: ({ apiUrl, appId, enabled, limit, }: UseMockCa
|
|
|
69
69
|
amount: string;
|
|
70
70
|
currencyCode: string;
|
|
71
71
|
};
|
|
72
|
-
|
|
72
|
+
compareAtPrice: {
|
|
73
|
+
amount: string;
|
|
74
|
+
currencyCode: string;
|
|
75
|
+
};
|
|
76
|
+
}[];
|
|
73
77
|
sellingPlan: {
|
|
74
78
|
id: string;
|
|
75
79
|
name: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-mock-cart.d.ts","sourceRoot":"","sources":["../../../components/hooks/use-mock-cart.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,6BAA6B,EAAE,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"use-mock-cart.d.ts","sourceRoot":"","sources":["../../../components/hooks/use-mock-cart.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,6BAA6B,EAAE,MAAM,kBAAkB,CAAA;AAsLhF,KAAK,iBAAiB,GAAG;IACvB,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;IACb,OAAO,EAAE,OAAO,CAAA;IAChB,KAAK,CAAC,EAAE,MAAM,CAAA;CACf,CAAA;AAED,eAAO,MAAM,WAAW,uCAKrB,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkCnB,CAAA"}
|
|
@@ -60,16 +60,22 @@ const transformCart = ({ products, cartOrigin = cartMock, }) => {
|
|
|
60
60
|
}
|
|
61
61
|
const sellingPlanAllocation = i === 0
|
|
62
62
|
? {
|
|
63
|
-
priceAdjustments:
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
priceAdjustments: [
|
|
64
|
+
{
|
|
65
|
+
perDeliveryPrice: {
|
|
66
|
+
amount: price.toString(),
|
|
67
|
+
currencyCode: "USD",
|
|
68
|
+
},
|
|
69
|
+
price: {
|
|
70
|
+
amount: price.toString(),
|
|
71
|
+
currencyCode: "USD",
|
|
72
|
+
},
|
|
73
|
+
compareAtPrice: {
|
|
74
|
+
amount: (price * 1.2).toString(),
|
|
75
|
+
currencyCode: "USD",
|
|
76
|
+
},
|
|
67
77
|
},
|
|
68
|
-
|
|
69
|
-
amount: price.toString(),
|
|
70
|
-
currencyCode: "USD",
|
|
71
|
-
},
|
|
72
|
-
},
|
|
78
|
+
],
|
|
73
79
|
sellingPlan: {
|
|
74
80
|
id: "gid://shopify/SellingPlan/123",
|
|
75
81
|
name: "Delivery every 30 Days",
|
package/dist/lib/cart.util.d.ts
CHANGED
|
@@ -1,4 +1,29 @@
|
|
|
1
|
-
import { EnrichedCart, CartCalculatedDiscount, CartCalculatedAppliedGiftCard } from "app-studio-types";
|
|
1
|
+
import { EnrichedCart, CartCalculatedDiscount, CartCalculatedAppliedGiftCard, ProductVariant } from "app-studio-types";
|
|
2
|
+
export declare const getVariantPrice: ({ quantity, variant, sellingPlanAllocation, }: {
|
|
3
|
+
quantity: number;
|
|
4
|
+
variant: ProductVariant;
|
|
5
|
+
sellingPlanAllocation?: {
|
|
6
|
+
priceAdjustments: {
|
|
7
|
+
perDeliveryPrice?: {
|
|
8
|
+
amount: string;
|
|
9
|
+
currencyCode: string;
|
|
10
|
+
};
|
|
11
|
+
price: {
|
|
12
|
+
amount: string;
|
|
13
|
+
currencyCode: string;
|
|
14
|
+
};
|
|
15
|
+
compareAtPrice?: {
|
|
16
|
+
amount: string;
|
|
17
|
+
currencyCode: string;
|
|
18
|
+
};
|
|
19
|
+
}[];
|
|
20
|
+
} | undefined;
|
|
21
|
+
}) => {
|
|
22
|
+
price: number;
|
|
23
|
+
compareAtPrice: number;
|
|
24
|
+
currency: string;
|
|
25
|
+
isSale: boolean;
|
|
26
|
+
};
|
|
2
27
|
export declare const isOrderLevelDiscount: (code: string, cart: EnrichedCart) => boolean;
|
|
3
28
|
export declare const isLineItemDiscount: (code: string, cart: EnrichedCart) => boolean;
|
|
4
29
|
export type CalculatedData = {
|
|
@@ -10,11 +35,14 @@ export type CalculatedData = {
|
|
|
10
35
|
salesAmount: number;
|
|
11
36
|
totalCompareAtPrice: number;
|
|
12
37
|
totalDiscountedPrice: number;
|
|
38
|
+
totalSavedAmount: number;
|
|
13
39
|
};
|
|
14
40
|
export declare const DEFAULT_CALCULATED_DATA: CalculatedData;
|
|
15
41
|
export declare const getCalculatedCartData: (cart: EnrichedCart) => CalculatedData;
|
|
16
42
|
export declare const getCartItemKey: (variantId: string, sellingPlanId?: string, attributes?: {
|
|
43
|
+
[key: string]: string;
|
|
44
|
+
} | {
|
|
17
45
|
key: string;
|
|
18
46
|
value: string;
|
|
19
|
-
}[]) => string;
|
|
47
|
+
}[] | undefined) => string;
|
|
20
48
|
//# sourceMappingURL=cart.util.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cart.util.d.ts","sourceRoot":"","sources":["../../lib/cart.util.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,sBAAsB,EACtB,6BAA6B,
|
|
1
|
+
{"version":3,"file":"cart.util.d.ts","sourceRoot":"","sources":["../../lib/cart.util.ts"],"names":[],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
2
|
+
export const getVariantPrice = ({ quantity, variant, sellingPlanAllocation = { priceAdjustments: [] }, }) => {
|
|
3
|
+
var _a, _b, _c, _d;
|
|
4
|
+
const isSubscription = ((_a = sellingPlanAllocation === null || sellingPlanAllocation === void 0 ? void 0 : sellingPlanAllocation.priceAdjustments) === null || _a === void 0 ? void 0 : _a.length) > 0;
|
|
5
|
+
if (isSubscription) {
|
|
6
|
+
const { perDeliveryPrice, price, compareAtPrice } = sellingPlanAllocation.priceAdjustments[0];
|
|
7
|
+
const effectivePrice = perDeliveryPrice || price;
|
|
8
|
+
const subscriptionUnitPrice = parseFloat((perDeliveryPrice === null || perDeliveryPrice === void 0 ? void 0 : perDeliveryPrice.amount) || "0") || 0;
|
|
9
|
+
const effectiveCompareAtPrice = parseFloat((compareAtPrice === null || compareAtPrice === void 0 ? void 0 : compareAtPrice.amount) || "0") ||
|
|
10
|
+
parseFloat((price === null || price === void 0 ? void 0 : price.amount) || "0") ||
|
|
11
|
+
0;
|
|
12
|
+
return {
|
|
13
|
+
price: subscriptionUnitPrice * quantity,
|
|
14
|
+
compareAtPrice: effectiveCompareAtPrice * quantity,
|
|
15
|
+
currency: effectivePrice === null || effectivePrice === void 0 ? void 0 : effectivePrice.currencyCode,
|
|
16
|
+
isSale: subscriptionUnitPrice < effectiveCompareAtPrice,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
const variantUnitPrice = parseFloat((_b = variant === null || variant === void 0 ? void 0 : variant.price) === null || _b === void 0 ? void 0 : _b.amount) || 0;
|
|
20
|
+
const variantComparePrice = parseFloat((_c = variant === null || variant === void 0 ? void 0 : variant.compareAtPrice) === null || _c === void 0 ? void 0 : _c.amount) || 0;
|
|
21
|
+
return {
|
|
22
|
+
price: variantUnitPrice * quantity,
|
|
23
|
+
compareAtPrice: variantComparePrice * quantity,
|
|
24
|
+
currency: (_d = variant === null || variant === void 0 ? void 0 : variant.price) === null || _d === void 0 ? void 0 : _d.currencyCode,
|
|
25
|
+
isSale: variantUnitPrice < variantComparePrice,
|
|
26
|
+
};
|
|
27
|
+
};
|
|
2
28
|
const getOrderLevelDiscounts = (cart) => {
|
|
3
29
|
var _a;
|
|
4
30
|
const discountMap = (_a = cart.discountAllocations) === null || _a === void 0 ? void 0 : _a.filter((discount) => discount.targetType !== "SHIPPING_LINE").reduce((acc, discount) => {
|
|
@@ -52,12 +78,13 @@ const getAppliedGiftCards = (cart) => {
|
|
|
52
78
|
const getSalesAmount = (cart) => {
|
|
53
79
|
var _a;
|
|
54
80
|
return (_a = cart.items) === null || _a === void 0 ? void 0 : _a.reduce((acc, item) => {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
81
|
+
const { compareAtPrice, price } = getVariantPrice({
|
|
82
|
+
quantity: item.quantity || 1,
|
|
83
|
+
variant: item.variantDetails,
|
|
84
|
+
sellingPlanAllocation: item.sellingPlanAllocation,
|
|
85
|
+
});
|
|
59
86
|
if (compareAtPrice && compareAtPrice > price) {
|
|
60
|
-
return acc + (compareAtPrice - price)
|
|
87
|
+
return acc + (compareAtPrice - price);
|
|
61
88
|
}
|
|
62
89
|
return acc;
|
|
63
90
|
}, 0);
|
|
@@ -120,6 +147,13 @@ const getTotalDiscountedPrice = (totalCompareAtPrice, discountsTotalAmount, sale
|
|
|
120
147
|
: giftCardsTotalAmount);
|
|
121
148
|
return isNaN(total) ? 0 : total;
|
|
122
149
|
};
|
|
150
|
+
const getTotalSavedAmount = (cart) => {
|
|
151
|
+
const salesAmount = getSalesAmount(cart);
|
|
152
|
+
const totalDiscountsAmount = getDiscountsTotalAmount(getOrderLevelDiscounts(cart).concat(getLineItemDiscounts(cart)));
|
|
153
|
+
const totalGiftCardsAmount = getGiftCardsTotalAmount(getAppliedGiftCards(cart));
|
|
154
|
+
const totalSavedAmount = salesAmount + totalDiscountsAmount + totalGiftCardsAmount;
|
|
155
|
+
return totalSavedAmount;
|
|
156
|
+
};
|
|
123
157
|
export const DEFAULT_CALCULATED_DATA = {
|
|
124
158
|
orderAndLineItemDiscounts: [],
|
|
125
159
|
appliedGiftCards: [],
|
|
@@ -129,6 +163,7 @@ export const DEFAULT_CALCULATED_DATA = {
|
|
|
129
163
|
giftCardsTotalAmount: 0,
|
|
130
164
|
totalCompareAtPrice: 0,
|
|
131
165
|
totalDiscountedPrice: 0,
|
|
166
|
+
totalSavedAmount: 0,
|
|
132
167
|
};
|
|
133
168
|
export const getCalculatedCartData = (cart) => {
|
|
134
169
|
const orderAndLineItemDiscounts = getOrderLevelDiscounts(cart).concat(getLineItemDiscounts(cart));
|
|
@@ -139,6 +174,7 @@ export const getCalculatedCartData = (cart) => {
|
|
|
139
174
|
const salesAmount = getSalesAmount(cart);
|
|
140
175
|
const totalCompareAtPrice = getTotalCompareAtPrice(cart);
|
|
141
176
|
const totalDiscountedPrice = getTotalDiscountedPrice(totalCompareAtPrice, discountsTotalAmount, salesAmount, giftCardsTotalAmount);
|
|
177
|
+
const totalSavedAmount = getTotalSavedAmount(cart);
|
|
142
178
|
return {
|
|
143
179
|
orderAndLineItemDiscounts,
|
|
144
180
|
appliedGiftCards,
|
|
@@ -148,9 +184,14 @@ export const getCalculatedCartData = (cart) => {
|
|
|
148
184
|
salesAmount,
|
|
149
185
|
totalCompareAtPrice,
|
|
150
186
|
totalDiscountedPrice,
|
|
187
|
+
totalSavedAmount,
|
|
151
188
|
};
|
|
152
189
|
};
|
|
153
190
|
export const getCartItemKey = (variantId, sellingPlanId, attributes) => {
|
|
154
|
-
const key = `${getIdFromGid(variantId)}:${sellingPlanId || "no-plan"}:${
|
|
191
|
+
const key = `${getIdFromGid(variantId)}:${sellingPlanId || "no-plan"}:${Array.isArray(attributes)
|
|
192
|
+
? attributes === null || attributes === void 0 ? void 0 : attributes.map((attr) => `${attr.key}-${attr.value}`).join("-")
|
|
193
|
+
: Object.entries(attributes || {})
|
|
194
|
+
.map(([key, value]) => `${key}-${value}`)
|
|
195
|
+
.join("-")}`;
|
|
155
196
|
return key;
|
|
156
197
|
};
|