@feedmepos/order-plugin-gallery 0.0.1
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/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
const maxItemInCategoryPlugin = ({ sdk, onAfterComputeMenuState, onBeforeSubmitOrder, }) => {
|
|
2
|
+
const menu = sdk.menuManager.state.value.menu;
|
|
3
|
+
// Configuration for categories that should be hidden after exceeding max items
|
|
4
|
+
const categoryLimitConfig = [
|
|
5
|
+
{
|
|
6
|
+
categoryId: "category_2025-12-23T08:14:26.067Z_wovwta",
|
|
7
|
+
hideCategoryWhenOrderedItemExceed: 0,
|
|
8
|
+
maxSubmitableItems: 2,
|
|
9
|
+
},
|
|
10
|
+
// ...
|
|
11
|
+
];
|
|
12
|
+
onBeforeSubmitOrder(() => {
|
|
13
|
+
const currentOrder = sdk.orderManager.state.value.order;
|
|
14
|
+
const cartItems = currentOrder?.draft || [];
|
|
15
|
+
const currentMenu = sdk.menuManager.state.value.menu;
|
|
16
|
+
// Check if any category exceeds maxSubmitableItems
|
|
17
|
+
for (const config of categoryLimitConfig) {
|
|
18
|
+
if (config.maxSubmitableItems === undefined)
|
|
19
|
+
continue;
|
|
20
|
+
const targetCategory = currentMenu?.categories?.find((cat) => cat.categoryId === config.categoryId);
|
|
21
|
+
if (!targetCategory)
|
|
22
|
+
continue;
|
|
23
|
+
const categoryItemIds = new Set(targetCategory.items.map((i) => i.id));
|
|
24
|
+
const numberOfItemsInCartForCategory = cartItems
|
|
25
|
+
.filter((item) => categoryItemIds.has(item.productId))
|
|
26
|
+
.reduce((sum, item) => sum + item.quantity, 0);
|
|
27
|
+
if (numberOfItemsInCartForCategory > config.maxSubmitableItems) {
|
|
28
|
+
sdk.ui.toast.show({
|
|
29
|
+
title: `You can't order more than ${config.maxSubmitableItems} product(s) in category "${targetCategory.categoryName}".`,
|
|
30
|
+
type: "error",
|
|
31
|
+
});
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return true;
|
|
36
|
+
});
|
|
37
|
+
onAfterComputeMenuState(({ menu, overridedMenu }) => {
|
|
38
|
+
if (!menu) {
|
|
39
|
+
return { menu, overridedMenu };
|
|
40
|
+
}
|
|
41
|
+
const slotActiveBills = sdk.orderManager.state.value.slotActiveBills;
|
|
42
|
+
const summarizedItems = slotActiveBills?.[0]?.items || [];
|
|
43
|
+
// Calculate which categories should be hidden
|
|
44
|
+
const categoriesToHide = new Set();
|
|
45
|
+
for (const config of categoryLimitConfig) {
|
|
46
|
+
const targetCategory = menu.categories?.find((cat) => cat.categoryId === config.categoryId);
|
|
47
|
+
if (!targetCategory)
|
|
48
|
+
continue;
|
|
49
|
+
const categoryItems = targetCategory.items.map((i) => i.title) || [];
|
|
50
|
+
const itemsSet = new Set(categoryItems);
|
|
51
|
+
const numberOfOrderedItemsInCategory = summarizedItems
|
|
52
|
+
.filter((item) => itemsSet.has(item.name || ""))
|
|
53
|
+
.reduce((sum, item) => sum + item.quantity, 0);
|
|
54
|
+
console.log(`Category ${config.categoryId}: ${numberOfOrderedItemsInCategory}/${config.hideCategoryWhenOrderedItemExceed} items ordered`);
|
|
55
|
+
if (numberOfOrderedItemsInCategory >
|
|
56
|
+
config.hideCategoryWhenOrderedItemExceed) {
|
|
57
|
+
categoriesToHide.add(config.categoryId);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// Apply hidden flag to categories that exceed their limits
|
|
61
|
+
const modifiedMenu = categoriesToHide.size > 0
|
|
62
|
+
? {
|
|
63
|
+
...menu,
|
|
64
|
+
categories: menu.categories?.map((cat) => categoriesToHide.has(cat.categoryId)
|
|
65
|
+
? { ...cat, hidden: true }
|
|
66
|
+
: cat),
|
|
67
|
+
}
|
|
68
|
+
: menu;
|
|
69
|
+
return {
|
|
70
|
+
menu: modifiedMenu,
|
|
71
|
+
overridedMenu,
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
export default maxItemInCategoryPlugin;
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@feedmepos/order-plugin-gallery",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
15
|
+
},
|
|
16
|
+
"./before-submit-guard": {
|
|
17
|
+
"import": "./dist/plugins/before-submit-guard.js",
|
|
18
|
+
"types": "./dist/plugins/before-submit-guard.d.ts"
|
|
19
|
+
},
|
|
20
|
+
"./menu-debug-logger": {
|
|
21
|
+
"import": "./dist/plugins/menu-debug-logger.js",
|
|
22
|
+
"types": "./dist/plugins/menu-debug-logger.d.ts"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@feedmepos/ordering-sdk": "^0.0.1"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@feedmepos/ordering-sdk": "file:../sdk",
|
|
30
|
+
"typescript": "^5.4.0"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc --project tsconfig.json",
|
|
34
|
+
"clean": "rm -rf dist"
|
|
35
|
+
}
|
|
36
|
+
}
|