@ravishranjan/cart 2.0.4 → 2.0.5
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/react/index.d.mts +3 -0
- package/dist/react/index.d.ts +3 -0
- package/dist/react/index.js +88 -0
- package/dist/react/index.mjs +6 -1
- package/package.json +1 -1
package/dist/react/index.d.mts
CHANGED
package/dist/react/index.d.ts
CHANGED
package/dist/react/index.js
CHANGED
@@ -20,6 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
20
20
|
// src/react/index.ts
|
21
21
|
var react_exports = {};
|
22
22
|
__export(react_exports, {
|
23
|
+
CartContext: () => CartContext,
|
24
|
+
CartProvider: () => CartProvider,
|
23
25
|
useCart: () => useCart
|
24
26
|
});
|
25
27
|
module.exports = __toCommonJS(react_exports);
|
@@ -29,8 +31,92 @@ var import_react2 = require("react");
|
|
29
31
|
|
30
32
|
// src/react/CartContext.tsx
|
31
33
|
var import_react = require("react");
|
34
|
+
|
35
|
+
// src/core/storage.ts
|
36
|
+
function getStorage(type) {
|
37
|
+
if (typeof window == "undefined") throw new Error("No window object found");
|
38
|
+
return type == "localStorage" ? window.localStorage : window.sessionStorage;
|
39
|
+
}
|
40
|
+
|
41
|
+
// src/core/cart.ts
|
42
|
+
var Cart = class {
|
43
|
+
constructor({
|
44
|
+
storage = "localStorage",
|
45
|
+
key = "cart"
|
46
|
+
} = {}) {
|
47
|
+
this.storage = getStorage(storage);
|
48
|
+
this.key = key;
|
49
|
+
}
|
50
|
+
getCart() {
|
51
|
+
const data = this.storage.getItem(this.key);
|
52
|
+
return data ? JSON.parse(data) : [];
|
53
|
+
}
|
54
|
+
save(cart) {
|
55
|
+
this.storage.setItem(this.key, JSON.stringify(cart));
|
56
|
+
}
|
57
|
+
addItem(item) {
|
58
|
+
const cart = this.getCart();
|
59
|
+
const exists = cart.find((item2) => item2.id === item2.id);
|
60
|
+
if (exists)
|
61
|
+
exists.quantity = (exists.quantity || 1) + (item.quantity || 1);
|
62
|
+
else cart.push({ ...item, quantity: item.quantity || 1 });
|
63
|
+
this.save(cart);
|
64
|
+
}
|
65
|
+
removeItem(id) {
|
66
|
+
this.save(this.getCart().filter((item) => item.id !== id));
|
67
|
+
}
|
68
|
+
clear() {
|
69
|
+
this.save([]);
|
70
|
+
}
|
71
|
+
getItems(category) {
|
72
|
+
const items = this.getCart();
|
73
|
+
return category ? items.filter((item) => item.category === category) : items;
|
74
|
+
}
|
75
|
+
getTotal(category) {
|
76
|
+
let items = this.getCart();
|
77
|
+
if (category) {
|
78
|
+
items = items.filter((item) => item.category === category);
|
79
|
+
}
|
80
|
+
return items.reduce(
|
81
|
+
(sum, item) => sum + (item.price || 0) * (item.quantity || 1),
|
82
|
+
0
|
83
|
+
);
|
84
|
+
}
|
85
|
+
};
|
86
|
+
|
87
|
+
// src/react/CartContext.tsx
|
32
88
|
var import_jsx_runtime = require("react/jsx-runtime");
|
33
89
|
var CartContext = (0, import_react.createContext)(null);
|
90
|
+
var CartProvider = ({ children }) => {
|
91
|
+
const cartInstance = new Cart();
|
92
|
+
const [items, setItems] = (0, import_react.useState)(cartInstance.getItems());
|
93
|
+
const sync = () => setItems(cartInstance.getItems());
|
94
|
+
const api = {
|
95
|
+
cart: items,
|
96
|
+
addItem: (item) => {
|
97
|
+
cartInstance.addItem(item);
|
98
|
+
sync();
|
99
|
+
},
|
100
|
+
removeItem: (id) => {
|
101
|
+
cartInstance.removeItem(id);
|
102
|
+
sync();
|
103
|
+
},
|
104
|
+
getItems: (category) => {
|
105
|
+
cartInstance.getItems(category);
|
106
|
+
},
|
107
|
+
clear: () => {
|
108
|
+
cartInstance.clear();
|
109
|
+
sync();
|
110
|
+
},
|
111
|
+
total: cartInstance.getTotal()
|
112
|
+
};
|
113
|
+
(0, import_react.useEffect)(() => {
|
114
|
+
const handleSync = () => sync();
|
115
|
+
window.addEventListener("storage", handleSync);
|
116
|
+
return () => window.removeEventListener("storage", handleSync);
|
117
|
+
}, []);
|
118
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CartContext.Provider, { value: api, children });
|
119
|
+
};
|
34
120
|
|
35
121
|
// src/react/useCart.tsx
|
36
122
|
var useCart = () => {
|
@@ -40,5 +126,7 @@ var useCart = () => {
|
|
40
126
|
};
|
41
127
|
// Annotate the CommonJS export names for ESM import in node:
|
42
128
|
0 && (module.exports = {
|
129
|
+
CartContext,
|
130
|
+
CartProvider,
|
43
131
|
useCart
|
44
132
|
});
|
package/dist/react/index.mjs
CHANGED
@@ -1,9 +1,14 @@
|
|
1
1
|
import {
|
2
2
|
useCart
|
3
3
|
} from "../chunk-K4RESSJQ.mjs";
|
4
|
-
import
|
4
|
+
import {
|
5
|
+
CartContext,
|
6
|
+
CartProvider
|
7
|
+
} from "../chunk-3AB6KTDB.mjs";
|
5
8
|
import "../chunk-YTW5MCYO.mjs";
|
6
9
|
import "../chunk-7RBGBF5M.mjs";
|
7
10
|
export {
|
11
|
+
CartContext,
|
12
|
+
CartProvider,
|
8
13
|
useCart
|
9
14
|
};
|