Adityadn commited on
Commit
ca08ea8
·
verified ·
1 Parent(s): 9c0bc90

Upload cart.js

Browse files
Files changed (1) hide show
  1. javascript/cart.js +3 -187
javascript/cart.js CHANGED
@@ -1,187 +1,3 @@
1
- async function updateCartIcon() {
2
- const cart = await getCarts();
3
- const count = cart.length;
4
- const cartIcon = document.getElementById("cart-icon");
5
- if (cartIcon) {
6
- cartIcon.innerHTML = `
7
- <i class="bi bi-cart"></i>
8
- <span class="m-1">
9
- <span class="nd-720">Keranjang</span>
10
- <span class="nd-512">(${count})</span>
11
- </span>`;
12
- }
13
- }
14
-
15
- document.addEventListener("DOMContentLoaded", async function () {
16
- setInterval(await updateCartIcon, 10000);
17
- });
18
-
19
- async function promptQuantityAndAdd(productId) {
20
- checkUID("addToCart");
21
- let [qtyStr, isCancel] = await prompt(
22
- "Masukkan jumlah produk yang ingin ditambahkan ke keranjang:",
23
- "number"
24
- );
25
-
26
- console.log(qtyStr, isCancel);
27
-
28
- if (isCancel) {
29
- return;
30
- }
31
-
32
- if (!qtyStr) {
33
- alert("Jumlah tidak boleh kosong.", false);
34
- return;
35
- }
36
-
37
- let qty = parseInt(qtyStr);
38
-
39
- if (isNaN(qty) || qty <= 0) {
40
- alert("Masukkan jumlah produk yang valid.", false);
41
- return;
42
- }
43
-
44
- await addToCart(productId, qty);
45
- }
46
-
47
- async function addToCart(productId, quantity = 1) {
48
- checkUID("addToCart");
49
-
50
- console.log(productId, quantity);
51
-
52
- const cart = await getCarts();
53
-
54
- let userData = await getUserData();
55
-
56
- for (let i = 0; i < quantity; i++) {
57
- cart.push(productId);
58
- }
59
-
60
- userData.cart = cart;
61
- localStorage.setItem("cart", JSON.stringify(cart));
62
-
63
- await updateUserDataToGitHub(userData);
64
- await updateCartIcon();
65
-
66
- alert(
67
- `Produk telah ditambahkan ke keranjang sebanyak ${quantity} item.`,
68
- false
69
- );
70
- }
71
-
72
- function addToCartFromProduct(productId) {
73
- checkUID("addToCart");
74
-
75
- const qtyInput = document.getElementById("quantity");
76
- let quantity = parseInt(qtyInput.value);
77
- if (isNaN(quantity) || quantity <= 0) {
78
- alert("Masukkan jumlah produk yang valid.", false);
79
- return;
80
- }
81
-
82
- addToCart(productId, quantity);
83
- }
84
-
85
- async function loadCart() {
86
- do {
87
- const cart = await getCarts();
88
- const params = new URLSearchParams(window.location.search);
89
- const message = params.get("message") || "";
90
-
91
- if (message) alert(message, false);
92
-
93
- let cartCount = {};
94
- cart.forEach((id) => {
95
- cartCount[id] = (cartCount[id] || 0) + 1;
96
- });
97
-
98
- const cartItemsContainer = document.getElementById("cart-items");
99
- cartItemsContainer.innerHTML = "";
100
- let total = 0;
101
-
102
- if (Object.keys(cartCount).length === 0) {
103
- cartItemsContainer.innerHTML = "<p>Keranjang kosong.</p>";
104
- } else {
105
- cartItemsContainer.innerHTML = `<span class="text-center text-muted">${message}</span>`;
106
- let index = 1;
107
- for (let id in cartCount) {
108
- const product = products.find((p) => p.id === parseInt(id));
109
- if (product) {
110
- const qty = cartCount[id];
111
- const subtotal = product.price * qty;
112
- total += subtotal;
113
- cartItemsContainer.innerHTML += `
114
- <div class="d-flex justify-content-between align-items-center border-bottom pb-2 mb-2">
115
- <div>
116
- <h5>${product.name}</h5>
117
- <p class="mb-0">
118
- ${formatRupiah(product.price)} x
119
- <input class="form-control form-control-lg" type="number" value="${qty}" min="1" style="width:60px;"
120
- onchange="updateQuantity(${product.id}, this.value)">
121
- </p>
122
- </div>
123
- <button class="btn btn-danger btn-lg" onclick="removeFromCart(${
124
- product.id
125
- })">Hapus</button>
126
- </div>`;
127
- index++;
128
- }
129
- }
130
- }
131
- const cartSummary = document.getElementById("cart-summary");
132
- cartSummary.innerHTML = `<h4>Total: ${formatRupiah(total)}</h4>`;
133
-
134
- if (cartItemsContainer.innerHTML.trim() == "") {
135
- cartItemsContainer.innerHTML = "";
136
- alert("Jika daftar keranjang tidak muncul, silahkan muat ulang halaman ini", false)
137
- }
138
-
139
- await new Promise((resolve) => setTimeout(resolve, 1000));
140
- } while (cartItemsContainer.innerHTML.trim() == "");
141
- }
142
-
143
- async function updateQuantity(productId, newQty) {
144
- newQty = parseInt(newQty);
145
- if (isNaN(newQty) || newQty <= 0) {
146
- alert("Masukkan jumlah produk yang valid.", false);
147
- return;
148
- }
149
-
150
- const cart = await getCarts();
151
- const newCart = cart.filter((id) => id !== productId);
152
-
153
- for (let i = 0; i < newQty; i++) {
154
- newCart.push(productId);
155
- }
156
- localStorage.setItem("cart", JSON.stringify(newCart));
157
-
158
- let userData = await getUserData();
159
- userData.cart = newCart;
160
- await updateUserDataToGitHub(userData);
161
-
162
- await loadCart();
163
- await updateCartIcon();
164
- }
165
-
166
- async function removeFromCart(productId) {
167
- const cart = await getCarts();
168
- const index = cart.indexOf(productId);
169
- if (index !== -1) {
170
- cart.splice(index, 1);
171
- localStorage.setItem("cart", JSON.stringify(cart));
172
-
173
- let userData = await getUserData();
174
- userData.cart = cart;
175
-
176
- await updateUserDataToGitHub(userData);
177
- await loadCart();
178
- await updateCartIcon();
179
- }
180
- }
181
-
182
- document.addEventListener("DOMContentLoaded", function () {
183
- AOS.init();
184
- if (document.getElementById("cart-items")) {
185
- loadCart();
186
- }
187
- });
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6b89f15f0d9069982e9d156072572188a88601143e1877aa1cc630e2fc84139f
3
+ size 5529