@open-tender/utils 0.1.122 → 0.1.123
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/cjs/hooks/index.d.ts +1 -0
- package/dist/cjs/hooks/index.js +1 -0
- package/dist/cjs/hooks/useCartPoints.d.ts +11 -0
- package/dist/cjs/hooks/useCartPoints.js +70 -0
- package/dist/esm/hooks/index.d.ts +1 -0
- package/dist/esm/hooks/index.js +1 -0
- package/dist/esm/hooks/useCartPoints.d.ts +11 -0
- package/dist/esm/hooks/useCartPoints.js +66 -0
- package/package.json +2 -2
- package/dist/.DS_Store +0 -0
|
@@ -4,6 +4,7 @@ export * from './useAddressForm';
|
|
|
4
4
|
export * from './useAllergenForm';
|
|
5
5
|
export * from './useBuilder';
|
|
6
6
|
export * from './useCartGuestForm';
|
|
7
|
+
export * from './useCartPoints';
|
|
7
8
|
export * from './useLoginForm';
|
|
8
9
|
export * from './useCreditCard';
|
|
9
10
|
export * from './useCreditCardForm';
|
package/dist/cjs/hooks/index.js
CHANGED
|
@@ -7,6 +7,7 @@ tslib_1.__exportStar(require("./useAddressForm"), exports);
|
|
|
7
7
|
tslib_1.__exportStar(require("./useAllergenForm"), exports);
|
|
8
8
|
tslib_1.__exportStar(require("./useBuilder"), exports);
|
|
9
9
|
tslib_1.__exportStar(require("./useCartGuestForm"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./useCartPoints"), exports);
|
|
10
11
|
tslib_1.__exportStar(require("./useLoginForm"), exports);
|
|
11
12
|
tslib_1.__exportStar(require("./useCreditCard"), exports);
|
|
12
13
|
tslib_1.__exportStar(require("./useCreditCardForm"), exports);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { Cart, CartItemWithPoints, CheckoutCheck, CheckoutForm, CheckoutPoints, OrderCreatePoints } from '@open-tender/types';
|
|
3
|
+
export declare const useCartPoints: (cart: Cart | null, check: CheckoutCheck | null, form: CheckoutForm, updatePoints: (points: OrderCreatePoints) => void) => {
|
|
4
|
+
cartDiff: number;
|
|
5
|
+
cartWithPoints: CartItemWithPoints[];
|
|
6
|
+
points: import("@open-tender/types").CheckoutConfigLoyalty | null | undefined;
|
|
7
|
+
pointsApplied: number;
|
|
8
|
+
pointsName: string | null;
|
|
9
|
+
pointsObj: CheckoutPoints | null;
|
|
10
|
+
setCartLength: import("react").Dispatch<import("react").SetStateAction<number>>;
|
|
11
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useCartPoints = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const useCartPoints = (cart, check, form, updatePoints) => {
|
|
6
|
+
var _a;
|
|
7
|
+
const [cartLength, setCartLength] = (0, react_1.useState)((cart === null || cart === void 0 ? void 0 : cart.length) || 0);
|
|
8
|
+
const { points } = (check === null || check === void 0 ? void 0 : check.config) || {};
|
|
9
|
+
const { cart: items = [] } = check || {};
|
|
10
|
+
const itemsPoints = items.length
|
|
11
|
+
? items.reduce((obj, i, index) => {
|
|
12
|
+
if (!i.points)
|
|
13
|
+
return obj;
|
|
14
|
+
return Object.assign(Object.assign({}, obj), { [index]: Object.assign({ index }, i.points) });
|
|
15
|
+
}, {})
|
|
16
|
+
: null;
|
|
17
|
+
const hasPoints = points && itemsPoints;
|
|
18
|
+
const cartWithPoints = (cart === null || cart === void 0 ? void 0 : cart.map((i, index) => (Object.assign(Object.assign({}, i), { index, points: hasPoints ? itemsPoints[index.toString()] : null })))) || [];
|
|
19
|
+
const pointsApplied = (0, react_1.useMemo)(() => (form.points ? form.points.reduce((t, i) => (t += i.points), 0) : 0), [form.points]);
|
|
20
|
+
const cartDiff = ((_a = cart === null || cart === void 0 ? void 0 : cart.length) !== null && _a !== void 0 ? _a : 0) - (cartLength !== null && cartLength !== void 0 ? cartLength : 0);
|
|
21
|
+
const pointsName = points ? points.name.toLowerCase() : null;
|
|
22
|
+
const applyPoints = (item) => {
|
|
23
|
+
if (!item.points || !item.index)
|
|
24
|
+
return;
|
|
25
|
+
const currentItem = form.points.find(i => i.index === item.index);
|
|
26
|
+
const currentPoints = currentItem ? currentItem.points : 0;
|
|
27
|
+
if (currentPoints >= item.points.total)
|
|
28
|
+
return;
|
|
29
|
+
const updatedItem = {
|
|
30
|
+
index: item.index,
|
|
31
|
+
points: currentPoints + item.points.per
|
|
32
|
+
};
|
|
33
|
+
const otherItems = form.points.filter(i => i.index !== item.index);
|
|
34
|
+
const updatedPoints = [...otherItems, updatedItem];
|
|
35
|
+
updatePoints(updatedPoints);
|
|
36
|
+
};
|
|
37
|
+
const removePoints = (item) => {
|
|
38
|
+
if (!item.points || !item.index)
|
|
39
|
+
return;
|
|
40
|
+
const currentItem = form.points.find(i => i.index === item.index);
|
|
41
|
+
const currentPoints = currentItem ? currentItem.points : 0;
|
|
42
|
+
if (currentPoints === 0)
|
|
43
|
+
return;
|
|
44
|
+
const updatedItem = {
|
|
45
|
+
index: item.index,
|
|
46
|
+
points: currentPoints - item.points.per
|
|
47
|
+
};
|
|
48
|
+
const otherItems = form.points.filter(i => i.index !== item.index);
|
|
49
|
+
const updatedPoints = updatedItem.points > 0 ? [...otherItems, updatedItem] : otherItems;
|
|
50
|
+
updatePoints(updatedPoints);
|
|
51
|
+
};
|
|
52
|
+
const pointsObj = points
|
|
53
|
+
? {
|
|
54
|
+
points,
|
|
55
|
+
applied: form.points || [],
|
|
56
|
+
apply: applyPoints,
|
|
57
|
+
remove: removePoints
|
|
58
|
+
}
|
|
59
|
+
: null;
|
|
60
|
+
return {
|
|
61
|
+
cartDiff,
|
|
62
|
+
cartWithPoints,
|
|
63
|
+
points,
|
|
64
|
+
pointsApplied,
|
|
65
|
+
pointsName,
|
|
66
|
+
pointsObj,
|
|
67
|
+
setCartLength
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
exports.useCartPoints = useCartPoints;
|
|
@@ -4,6 +4,7 @@ export * from './useAddressForm';
|
|
|
4
4
|
export * from './useAllergenForm';
|
|
5
5
|
export * from './useBuilder';
|
|
6
6
|
export * from './useCartGuestForm';
|
|
7
|
+
export * from './useCartPoints';
|
|
7
8
|
export * from './useLoginForm';
|
|
8
9
|
export * from './useCreditCard';
|
|
9
10
|
export * from './useCreditCardForm';
|
package/dist/esm/hooks/index.js
CHANGED
|
@@ -4,6 +4,7 @@ export * from './useAddressForm';
|
|
|
4
4
|
export * from './useAllergenForm';
|
|
5
5
|
export * from './useBuilder';
|
|
6
6
|
export * from './useCartGuestForm';
|
|
7
|
+
export * from './useCartPoints';
|
|
7
8
|
export * from './useLoginForm';
|
|
8
9
|
export * from './useCreditCard';
|
|
9
10
|
export * from './useCreditCardForm';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { Cart, CartItemWithPoints, CheckoutCheck, CheckoutForm, CheckoutPoints, OrderCreatePoints } from '@open-tender/types';
|
|
3
|
+
export declare const useCartPoints: (cart: Cart | null, check: CheckoutCheck | null, form: CheckoutForm, updatePoints: (points: OrderCreatePoints) => void) => {
|
|
4
|
+
cartDiff: number;
|
|
5
|
+
cartWithPoints: CartItemWithPoints[];
|
|
6
|
+
points: import("@open-tender/types").CheckoutConfigLoyalty | null | undefined;
|
|
7
|
+
pointsApplied: number;
|
|
8
|
+
pointsName: string | null;
|
|
9
|
+
pointsObj: CheckoutPoints | null;
|
|
10
|
+
setCartLength: import("react").Dispatch<import("react").SetStateAction<number>>;
|
|
11
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { useMemo, useState } from 'react';
|
|
2
|
+
export const useCartPoints = (cart, check, form, updatePoints) => {
|
|
3
|
+
var _a;
|
|
4
|
+
const [cartLength, setCartLength] = useState((cart === null || cart === void 0 ? void 0 : cart.length) || 0);
|
|
5
|
+
const { points } = (check === null || check === void 0 ? void 0 : check.config) || {};
|
|
6
|
+
const { cart: items = [] } = check || {};
|
|
7
|
+
const itemsPoints = items.length
|
|
8
|
+
? items.reduce((obj, i, index) => {
|
|
9
|
+
if (!i.points)
|
|
10
|
+
return obj;
|
|
11
|
+
return Object.assign(Object.assign({}, obj), { [index]: Object.assign({ index }, i.points) });
|
|
12
|
+
}, {})
|
|
13
|
+
: null;
|
|
14
|
+
const hasPoints = points && itemsPoints;
|
|
15
|
+
const cartWithPoints = (cart === null || cart === void 0 ? void 0 : cart.map((i, index) => (Object.assign(Object.assign({}, i), { index, points: hasPoints ? itemsPoints[index.toString()] : null })))) || [];
|
|
16
|
+
const pointsApplied = useMemo(() => (form.points ? form.points.reduce((t, i) => (t += i.points), 0) : 0), [form.points]);
|
|
17
|
+
const cartDiff = ((_a = cart === null || cart === void 0 ? void 0 : cart.length) !== null && _a !== void 0 ? _a : 0) - (cartLength !== null && cartLength !== void 0 ? cartLength : 0);
|
|
18
|
+
const pointsName = points ? points.name.toLowerCase() : null;
|
|
19
|
+
const applyPoints = (item) => {
|
|
20
|
+
if (!item.points || !item.index)
|
|
21
|
+
return;
|
|
22
|
+
const currentItem = form.points.find(i => i.index === item.index);
|
|
23
|
+
const currentPoints = currentItem ? currentItem.points : 0;
|
|
24
|
+
if (currentPoints >= item.points.total)
|
|
25
|
+
return;
|
|
26
|
+
const updatedItem = {
|
|
27
|
+
index: item.index,
|
|
28
|
+
points: currentPoints + item.points.per
|
|
29
|
+
};
|
|
30
|
+
const otherItems = form.points.filter(i => i.index !== item.index);
|
|
31
|
+
const updatedPoints = [...otherItems, updatedItem];
|
|
32
|
+
updatePoints(updatedPoints);
|
|
33
|
+
};
|
|
34
|
+
const removePoints = (item) => {
|
|
35
|
+
if (!item.points || !item.index)
|
|
36
|
+
return;
|
|
37
|
+
const currentItem = form.points.find(i => i.index === item.index);
|
|
38
|
+
const currentPoints = currentItem ? currentItem.points : 0;
|
|
39
|
+
if (currentPoints === 0)
|
|
40
|
+
return;
|
|
41
|
+
const updatedItem = {
|
|
42
|
+
index: item.index,
|
|
43
|
+
points: currentPoints - item.points.per
|
|
44
|
+
};
|
|
45
|
+
const otherItems = form.points.filter(i => i.index !== item.index);
|
|
46
|
+
const updatedPoints = updatedItem.points > 0 ? [...otherItems, updatedItem] : otherItems;
|
|
47
|
+
updatePoints(updatedPoints);
|
|
48
|
+
};
|
|
49
|
+
const pointsObj = points
|
|
50
|
+
? {
|
|
51
|
+
points,
|
|
52
|
+
applied: form.points || [],
|
|
53
|
+
apply: applyPoints,
|
|
54
|
+
remove: removePoints
|
|
55
|
+
}
|
|
56
|
+
: null;
|
|
57
|
+
return {
|
|
58
|
+
cartDiff,
|
|
59
|
+
cartWithPoints,
|
|
60
|
+
points,
|
|
61
|
+
pointsApplied,
|
|
62
|
+
pointsName,
|
|
63
|
+
pointsObj,
|
|
64
|
+
setCartLength
|
|
65
|
+
};
|
|
66
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-tender/utils",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.123",
|
|
4
4
|
"description": "A library of utils for use with Open Tender applications that utilize our cloud-based Order API.",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"typescript": "^4.8.2"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
|
-
"@open-tender/types": "^0.2.
|
|
57
|
+
"@open-tender/types": "^0.2.55",
|
|
58
58
|
"@reduxjs/toolkit": "^1.8.5",
|
|
59
59
|
"react": "^18.2.0"
|
|
60
60
|
},
|
package/dist/.DS_Store
DELETED
|
Binary file
|