-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
216 lines (198 loc) · 6.7 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
document.addEventListener('alpine:init', () => {
Alpine.data('products', () => ({
items: [{
id: 1,
name: 'Chino Kafuu',
img: 'Chino-Kafuu.jpg',
price: 7000,
originPrice: 10000,
detail: 'chino-kafuu'
},
{
id: 2,
name: 'Chiya Ujimatsu',
img: 'Chiya-Ujimatsu.jpg',
price: 12000,
originPrice: 15000,
detail: 'chiya-ujimatsu'
},
{
id: 3,
name: 'Cocoa Hoto',
img: 'Cocoa-Hoto.jpg',
price: 5000,
originPrice: 7000,
detail: 'cocoa-hoto'
},
{
id: 4,
name: 'Midori Aoyama',
img: 'Midori-Aoyama.jpg',
price: 20000,
originPrice: 70000,
detail: 'midori-aoyama'
},
],
}));
Alpine.store('cart', {
items: [],
total: 0,
quantity: 0,
currentDetail: '',
add(newItem) {
// cek apakah ada barang yg sama di cart
const cartItem = this.items.find((item) => item.id === newItem.id)
// jika cart blum ada / kosong
if (!cartItem) {
this.items.push({
...newItem,
quantity: 1,
total: newItem.price
});
this.total += newItem.price;
this.quantity++;
} else {
// jika sudah ada, cek apakah barang beda atau sama
this.items = this.items.map((item) => {
// jika barang beda
if (item.id !== newItem.id) {
return item;
} else {
// jika barang udh ada
item.quantity++;
item.total = item.price * item.quantity;
this.total += item.price;
this.quantity++;
return item;
}
});
}
// console.log(newItem);
},
remove(id) {
// ambil item yg akan di remove
const cartItem = this.items.find((item) => item.id === id);
// console.log(cartItem);
// jika item lebih dari 1
if (cartItem.quantity > 1) {
// telusuri
this.items = this.items.map((item) => {
// jika bukan barang yg diklik
if (item.id !== id) {
return item;
} else {
item.quantity--;
item.total = item.price * item.quantity;
this.total -= item.price;
this.quantity--;
return item;
}
});
} else if (cartItem.quantity === 1) {
// jika barang sisa 1
this.items = this.items.filter((item) => item.id !== id);
this.quantity--;
this.total -= cartItem.price;
} else {
}
},
showDetail(item) {
const detailModal = document.querySelector(`#detail-modal-${item.detail}`);
if (detailModal) {
detailModal.style.display = "flex";
this.currentDetail = item.detail;
}
},
});
});
// form validation
const checkoutBtn = document.querySelector('.checkout-btn');
checkoutBtn.disabled = true;
const submitLimit = 3;
let submitCount = localStorage.getItem("submitCount") || 0;
checkoutBtn.addEventListener("click", (e) => {
e.preventDefault();
if (submitCount < submitLimit) {
submitCount++;
let sisaSubmit = submitLimit - submitCount;
localStorage.setItem("submitCount", submitCount);
alert(`Sisa submit yang tersisa: ${sisaSubmit}`);
console.log(`Submit count: ${submitCount}`);
} else {
alert(
"Anda telah mencapai batas pengiriman formulir. Dan tombol checkout tidak akan berfungsi lagi!");
e.stopImmediatePropagation();
}
});
const form = document.querySelector("#checkout-form");
form.addEventListener('keyup', () => {
for (let i = 0; i < form.elements.length; i++) {
if (form.elements[i].value.length != 0) {
checkoutBtn.classList.remove('disabled');
checkoutBtn.classList.add('disabled');
} else {
return false;
}
}
checkoutBtn.disabled = false;
checkoutBtn.classList.remove('disabled');
});
// kirim data ketika tmbl checkout diklik
checkoutBtn.addEventListener('click', async (e) => {
e.preventDefault();
const formData = new FormData(form);
const data = new URLSearchParams(formData);
const objData = Object.fromEntries(data);
// console.log(objData);
// payment link metode (wa)
// const message = formatPesan(objData);
// window.open('http://wa.me/6285123456789?text=' + encodeURIComponent(message))
// snap metode
// minta transaction token mrnggunakan ajax / fetch
try {
const response = await fetch(
"https://nivek-web.000webhostapp.com/kedaikopi_hosting/index.php", {
method: "POST",
body: data,
}
);
const token = await response.text();
console.log(token);
window.snap.pay(token, {
onSuccess: function (result) {
alert("pembayaran berhasil!✅");
console.log(result);
},
onPending: function (result) {
alert("chotto matte!🔄");
console.log(result);
},
onError: function (result) {
alert("pembayaran gagal coy!❎");
console.log(result);
},
onClose: function () {
alert("belum selesai bayar woy!😐");
},
});
} catch (err) {
console.log(err.message);
}
});
// format pesan wa
const formatPesan = (obj) => {
return `Data customer :
\tNama : ${obj.name},
\tEmail : ${obj.email},
\tNo HP : ${obj.phone},\n\nData Pesanan : \n${JSON.parse(obj.items)
.map((item) => `\t${item.name} (${item.quantity} x ${rupiah(item.total)}) \n`)
.join('')} \nTOTAL: ${rupiah(obj.total)} \nTerima kasih!`;
};
// konversi ke rupiah
const rupiah = (number) => {
return new Intl.NumberFormat('id-ID', {
style: 'currency',
currency: 'IDR',
minimumFractionDigits: 0,
}).format(number);
}