@ravishranjan/cart 2.0.8 → 2.0.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/chunk-JIOUNG7W.mjs +43 -0
- package/dist/chunk-JRM56HH5.mjs +15 -0
- package/dist/chunk-TVKKRH3O.mjs +53 -0
- package/dist/core/cart.d.mts +1 -1
- package/dist/core/cart.d.ts +1 -1
- package/dist/core/cart.js +4 -4
- package/dist/core/cart.mjs +1 -1
- package/dist/index.js +4 -4
- package/dist/index.mjs +1 -1
- package/dist/react/CartContext.js +4 -4
- package/dist/react/CartContext.mjs +2 -2
- package/dist/react/index.js +4 -4
- package/dist/react/index.mjs +3 -3
- package/dist/react/useCart.mjs +3 -3
- package/package.json +1 -1
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Cart
|
|
3
|
+
} from "./chunk-TVKKRH3O.mjs";
|
|
4
|
+
|
|
5
|
+
// src/react/CartContext.tsx
|
|
6
|
+
import { createContext, useEffect, useState } from "react";
|
|
7
|
+
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
var CartContext = createContext(null);
|
|
9
|
+
var CartProvider = ({ children }) => {
|
|
10
|
+
const cartInstance = new Cart();
|
|
11
|
+
const [items, setItems] = useState(cartInstance.getItems());
|
|
12
|
+
const sync = () => setItems(cartInstance.getItems());
|
|
13
|
+
const api = {
|
|
14
|
+
cart: items,
|
|
15
|
+
addItem: (item) => {
|
|
16
|
+
cartInstance.addItem(item);
|
|
17
|
+
sync();
|
|
18
|
+
},
|
|
19
|
+
removeItem: (id) => {
|
|
20
|
+
cartInstance.removeItem(id);
|
|
21
|
+
sync();
|
|
22
|
+
},
|
|
23
|
+
getItems: (category) => {
|
|
24
|
+
cartInstance.getItems(category);
|
|
25
|
+
},
|
|
26
|
+
clear: () => {
|
|
27
|
+
cartInstance.clear();
|
|
28
|
+
sync();
|
|
29
|
+
},
|
|
30
|
+
total: cartInstance.getTotal()
|
|
31
|
+
};
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
const handleSync = () => sync();
|
|
34
|
+
window.addEventListener("storage", handleSync);
|
|
35
|
+
return () => window.removeEventListener("storage", handleSync);
|
|
36
|
+
}, []);
|
|
37
|
+
return /* @__PURE__ */ jsx(CartContext.Provider, { value: api, children });
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export {
|
|
41
|
+
CartContext,
|
|
42
|
+
CartProvider
|
|
43
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CartContext
|
|
3
|
+
} from "./chunk-JIOUNG7W.mjs";
|
|
4
|
+
|
|
5
|
+
// src/react/useCart.tsx
|
|
6
|
+
import { useContext } from "react";
|
|
7
|
+
function useCart() {
|
|
8
|
+
const context = useContext(CartContext);
|
|
9
|
+
if (!context) throw new Error("useCart must be used within CartProvider");
|
|
10
|
+
return context;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export {
|
|
14
|
+
useCart
|
|
15
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getStorage
|
|
3
|
+
} from "./chunk-7RBGBF5M.mjs";
|
|
4
|
+
|
|
5
|
+
// src/core/cart.ts
|
|
6
|
+
var Cart = class {
|
|
7
|
+
constructor({
|
|
8
|
+
storage = "localStorage",
|
|
9
|
+
key = "cart"
|
|
10
|
+
} = {}) {
|
|
11
|
+
this.storage = getStorage(storage);
|
|
12
|
+
this.key = key;
|
|
13
|
+
}
|
|
14
|
+
getCart() {
|
|
15
|
+
const data = this.storage.getItem(this.key);
|
|
16
|
+
return data ? JSON.parse(data) : [];
|
|
17
|
+
}
|
|
18
|
+
save(cart) {
|
|
19
|
+
this.storage.setItem(this.key, JSON.stringify(cart));
|
|
20
|
+
}
|
|
21
|
+
addItem(newItem) {
|
|
22
|
+
const cart = this.getCart();
|
|
23
|
+
const exists = cart.find((item) => item.id === newItem.id);
|
|
24
|
+
if (exists)
|
|
25
|
+
exists.quantity = (exists.quantity || 1) + (newItem.quantity || 1);
|
|
26
|
+
else cart.push({ ...newItem, quantity: newItem.quantity || 1 });
|
|
27
|
+
this.save(cart);
|
|
28
|
+
}
|
|
29
|
+
removeItem(id) {
|
|
30
|
+
this.save(this.getCart().filter((item) => item.id !== id));
|
|
31
|
+
}
|
|
32
|
+
clear() {
|
|
33
|
+
this.save([]);
|
|
34
|
+
}
|
|
35
|
+
getItems(category) {
|
|
36
|
+
const items = this.getCart();
|
|
37
|
+
return category ? items.filter((item) => item.category === category) : items;
|
|
38
|
+
}
|
|
39
|
+
getTotal(category) {
|
|
40
|
+
let items = this.getCart();
|
|
41
|
+
if (category) {
|
|
42
|
+
items = items.filter((item) => item.category === category);
|
|
43
|
+
}
|
|
44
|
+
return items.reduce(
|
|
45
|
+
(sum, item) => sum + (item.price || 0) * (item.quantity || 1),
|
|
46
|
+
0
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export {
|
|
52
|
+
Cart
|
|
53
|
+
};
|
package/dist/core/cart.d.mts
CHANGED
package/dist/core/cart.d.ts
CHANGED
package/dist/core/cart.js
CHANGED
|
@@ -46,12 +46,12 @@ var Cart = class {
|
|
|
46
46
|
save(cart) {
|
|
47
47
|
this.storage.setItem(this.key, JSON.stringify(cart));
|
|
48
48
|
}
|
|
49
|
-
addItem(
|
|
49
|
+
addItem(newItem) {
|
|
50
50
|
const cart = this.getCart();
|
|
51
|
-
const exists = cart.find((
|
|
51
|
+
const exists = cart.find((item) => item.id === newItem.id);
|
|
52
52
|
if (exists)
|
|
53
|
-
exists.quantity = (exists.quantity || 1) + (
|
|
54
|
-
else cart.push({ ...
|
|
53
|
+
exists.quantity = (exists.quantity || 1) + (newItem.quantity || 1);
|
|
54
|
+
else cart.push({ ...newItem, quantity: newItem.quantity || 1 });
|
|
55
55
|
this.save(cart);
|
|
56
56
|
}
|
|
57
57
|
removeItem(id) {
|
package/dist/core/cart.mjs
CHANGED
package/dist/index.js
CHANGED
|
@@ -47,12 +47,12 @@ var Cart = class {
|
|
|
47
47
|
save(cart) {
|
|
48
48
|
this.storage.setItem(this.key, JSON.stringify(cart));
|
|
49
49
|
}
|
|
50
|
-
addItem(
|
|
50
|
+
addItem(newItem) {
|
|
51
51
|
const cart = this.getCart();
|
|
52
|
-
const exists = cart.find((
|
|
52
|
+
const exists = cart.find((item) => item.id === newItem.id);
|
|
53
53
|
if (exists)
|
|
54
|
-
exists.quantity = (exists.quantity || 1) + (
|
|
55
|
-
else cart.push({ ...
|
|
54
|
+
exists.quantity = (exists.quantity || 1) + (newItem.quantity || 1);
|
|
55
|
+
else cart.push({ ...newItem, quantity: newItem.quantity || 1 });
|
|
56
56
|
this.save(cart);
|
|
57
57
|
}
|
|
58
58
|
removeItem(id) {
|
package/dist/index.mjs
CHANGED
|
@@ -48,12 +48,12 @@ var Cart = class {
|
|
|
48
48
|
save(cart) {
|
|
49
49
|
this.storage.setItem(this.key, JSON.stringify(cart));
|
|
50
50
|
}
|
|
51
|
-
addItem(
|
|
51
|
+
addItem(newItem) {
|
|
52
52
|
const cart = this.getCart();
|
|
53
|
-
const exists = cart.find((
|
|
53
|
+
const exists = cart.find((item) => item.id === newItem.id);
|
|
54
54
|
if (exists)
|
|
55
|
-
exists.quantity = (exists.quantity || 1) + (
|
|
56
|
-
else cart.push({ ...
|
|
55
|
+
exists.quantity = (exists.quantity || 1) + (newItem.quantity || 1);
|
|
56
|
+
else cart.push({ ...newItem, quantity: newItem.quantity || 1 });
|
|
57
57
|
this.save(cart);
|
|
58
58
|
}
|
|
59
59
|
removeItem(id) {
|
package/dist/react/index.js
CHANGED
|
@@ -53,12 +53,12 @@ var Cart = class {
|
|
|
53
53
|
save(cart) {
|
|
54
54
|
this.storage.setItem(this.key, JSON.stringify(cart));
|
|
55
55
|
}
|
|
56
|
-
addItem(
|
|
56
|
+
addItem(newItem) {
|
|
57
57
|
const cart = this.getCart();
|
|
58
|
-
const exists = cart.find((
|
|
58
|
+
const exists = cart.find((item) => item.id === newItem.id);
|
|
59
59
|
if (exists)
|
|
60
|
-
exists.quantity = (exists.quantity || 1) + (
|
|
61
|
-
else cart.push({ ...
|
|
60
|
+
exists.quantity = (exists.quantity || 1) + (newItem.quantity || 1);
|
|
61
|
+
else cart.push({ ...newItem, quantity: newItem.quantity || 1 });
|
|
62
62
|
this.save(cart);
|
|
63
63
|
}
|
|
64
64
|
removeItem(id) {
|
package/dist/react/index.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
useCart
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-JRM56HH5.mjs";
|
|
4
4
|
import {
|
|
5
5
|
CartProvider
|
|
6
|
-
} from "../chunk-
|
|
7
|
-
import "../chunk-
|
|
6
|
+
} from "../chunk-JIOUNG7W.mjs";
|
|
7
|
+
import "../chunk-TVKKRH3O.mjs";
|
|
8
8
|
import "../chunk-7RBGBF5M.mjs";
|
|
9
9
|
export {
|
|
10
10
|
CartProvider,
|
package/dist/react/useCart.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
useCart
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
5
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-JRM56HH5.mjs";
|
|
4
|
+
import "../chunk-JIOUNG7W.mjs";
|
|
5
|
+
import "../chunk-TVKKRH3O.mjs";
|
|
6
6
|
import "../chunk-7RBGBF5M.mjs";
|
|
7
7
|
export {
|
|
8
8
|
useCart
|