-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserAgent.js
More file actions
280 lines (269 loc) · 11.9 KB
/
userAgent.js
File metadata and controls
280 lines (269 loc) · 11.9 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
class userAgent{
sessionId;
foodCourtId;
tableId;
animator;
merchantAndProduct = [
{merchantName:"SellerName1",products:[{id:1,productName:"Bakso",productImage:null,productDescription:"Baksoka",productRating:5,productPrice:5000}]},
{merchantName:"SellerName1",products:[{id:1,productName:"Bakso",productImage:null,productDescription:"Baksoka",productRating:5,productPrice:5000}]},
{merchantName:"SellerName1",products:[{id:1,productName:"Bakso",productImage:null,productDescription:"Baksoka",productRating:5,productPrice:5000}]},
{merchantName:"SellerName1",products:[{id:1,productName:"Bakso",productImage:null,productDescription:"Baksoka",productRating:5,productPrice:5000}]},
{merchantName:"SellerName1",products:[{id:1,productName:"Bakso",productImage:null,productDescription:"Baksoka",productRating:5,productPrice:5000}]},
];
cart;
constructor(){
//inisialisasi animator;
this.animator = new animator("container");
//Tambahkan event listener kepada hash di url dan bila ada perubahan panggil fungsi page handler
window.addEventListener('hashchange', (e)=>{this.pageHandler(location.hash.replace("#",""))});
//Dapatkan List produk
var getMerchant = new Promise((resolve, reject)=>{
this.universalXmlReq(resolve,reject,"https://api-backend-production-2003.up.railway.app/api/v1/foodit/location/1",null,null)
});
getMerchant.then(
(value)=>{
//Bila berhasil maka set produk dan merchan
this.merchantAndProduct = value;
//Ganti hash ke main dan render laman
location.hash = "main";
this.pageHandler("main");
}
)
getMerchant.catch(
(error)=>{
//Kalau error log error
console.log(error);
}
)
}
//Test
test(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "https://api-backend-production-2003.up.railway.app/api/v1/foodit/location/1", true);
xhttp.send();
}
/**
* Handler Rendering LAmanm
* @param {String} slideRaw
*/
pageHandler(slideRaw){
//Slide special function
//Pisahkan Hash dan param
var param = {};
var slideRaw = slideRaw.split("?");
var slideId = slideRaw[0];
//Buatkan objek param
for (let index = 1; index < slideRaw.length; index++) {
const element = slideRaw[index].split("=");
param[element[0]] = element[1];
}
console.log(param.site);
//Spesial Fungsi untuk halaman
switch (slideId) {
case "main":
var that = this;
this.animator.pageSelect(slideId,function(){
that.loadMerchant();
});
break;
case "detail":
var that = this;
this.animator.pageSelect(slideId,function(){
that.loadDetail(param.param);
});
break;
default:
//Default jiga tidak ada special function
if(this.animator.pageSelect(slideId) === false){
location.hash = "error";
}
break;
}
}
/**
* Muat Laman Detail
* @param {String} index Index Item
*/
loadDetail(index){
//Variable untuk item yg dipilih
var product;
//Keterangan ITem
var detailHeader = document.getElementById("detailHeader");
var detailDesc = document.getElementById("detailDesc");
var detailPrice = document.getElementById("detailPrice");
var detailImage = document.getElementById("detailImage");
console.log(index);
//Dapatkan item dari pilihan
this.merchantAndProduct.forEach((element)=>{
element.products.forEach((element)=>{
if(index == element.id){
product = element;
}
})
})
//Render Gambar
if(!product.productImage == null){
//Kalau null maka pakai dummy
detailImage.src = "/asset/image/image-files.svg";
} else {
detailImage.src = product.productImage;
}
//Pasangkan nilai
detailHeader.innerHTML = product.productName;
detailDesc.innerHTML = product.productDescription;
detailPrice.innerHTML = "Rp. "+product.productPrice;
}
/**
* Muat Konten Laman Main
*/
loadMerchant(){
//Dapatkan Container Selector Merchant 1
var selectMerchantContainer = document.getElementById("bannerSroll2");
//Dapatkan Container Produk
var productContainer = document.getElementById("productContainer");
//Dapatkan Container Selector Float
var selectMerchantFloatContainer = document.getElementById("floatMenu");
this.merchantAndProduct.forEach((element,index)=>{
//Masukkan tombol select merchant ke selector merchant 1
var selectMerchant = document.createElement("section");
selectMerchant.className = "selectMerchant";
selectMerchant.innerHTML = element.merchantName;
selectMerchantContainer.appendChild(selectMerchant);
//Masukkan tombol select merchant ke selector merchant float
var selectMerchantFloat = document.createElement("section");
selectMerchantFloat.className = "element100 floatMenuMerchant";
selectMerchantFloat.style.fontWeight = "600";
selectMerchantFloat.innerHTML = element.merchantName;
selectMerchantFloatContainer.appendChild(selectMerchantFloat);
//Buatkan container merchat dengan index
var merchant = document.createElement("section");
merchant.id = index;
//Tambahkan eventlistiner klik ke tombol selector merchant dan saat klik scroll ke posisi
selectMerchant.addEventListener("click",function () {
merchant.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'start' });
});
selectMerchantFloat.addEventListener("click",function () {
merchant.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'start' });
document.getElementById("floatMenuContainer").className = "floatMenuContainerHidden";
});
//Buat header container merchant
var merchantHeaderC = document.createElement("section");
merchantHeaderC.className = "element100";
merchantHeaderC.style.fontSize = "24px";
merchantHeaderC.style.fontWeight = "600";
merchantHeaderC.style.color = "var(--fooderGreen)";
//Buat Text Header
var merchantHeaderP = document.createElement("p");
merchantHeaderP.innerHTML = element.merchantName;
//Tambahkan ke container
merchantHeaderC.appendChild(merchantHeaderP);
merchant.appendChild(merchantHeaderC);
productContainer.appendChild(merchant);
//Render tiap produk di merchant
element.products.forEach((element,index)=>{
//Buat product container
var productContainerEl = document.createElement("section");
productContainerEl.className = "element100";
//Buat Holder Product
var productEl = document.createElement("section");
productEl.className = "product100";
//Buat flex product kiri untuk foto dan ket
var productElFlex = document.createElement("section");
productElFlex.style.display = "flex";
//Render Gambar item
var productImage = document.createElement("img");
if(element.productImage == null){
//Bila blm di set maka pakai image dummy
productImage.src = "/asset/image/image-files.svg";
} else {
productImage.src = element.productImage;
}
productImage.style.aspectRatio = "1/1";
productImage.style.objectFit = "cover";
productImage.style.borderRadius = "10px 0 0 10px";
//Buat section untuk ket item
var productDesc = document.createElement("section");
productDesc.className = "productDesc";
//Nama item
var productName = document.createElement("p");
productName.innerHTML = element.productName;
productName.style.fontWeight = "600";
//Harga
var productPrice = document.createElement("p");
productPrice.innerHTML = "Rp. "+element.productPrice;
productPrice.style.fontWeight = "600";
//Render Bintang Rating
var productRating = document.createElement("section");
for (let index = 0; index < element.productRating; index++) {
var starIcon = document.createElement("img");
starIcon.src = "/asset/image/star.svg";
productRating.appendChild(starIcon);
}
//Tombol add cart
var addToCartBtn = document.createElement("img");
addToCartBtn.src = "/asset/image/add-one.svg";
addToCartBtn.style.width = "30px";
addToCartBtn.style.height = "30px";
addToCartBtn.style.padding = "10px";
addToCartBtn.style.alignSelf = "flex-end";
//Event saat diklik ke page detail
addToCartBtn.addEventListener("click",function(){
location.hash = "#detail?param="+element.id;
});
//Dipasangkan
productDesc.appendChild(productName);
productDesc.appendChild(productRating);
productDesc.appendChild(productPrice);
productElFlex.appendChild(productImage);
productElFlex.appendChild(productDesc);
productEl.appendChild(productElFlex);
productEl.appendChild(addToCartBtn);
productContainerEl.appendChild(productEl);
merchant.appendChild(productContainerEl);
})
});
}
/**
* Standart Fungsi untuk melakukan AJAX Request
* @param {*} resolve Fungsi saat resolve
* @param {*} reject Fungsi Reject
* @param {*} target URL Target
* @param {*} form Form Data
* @param {*} extra Extra Parameter
*/
universalXmlReq(resolve, reject, target, form, extra){
var xhttp = new XMLHttpRequest;
xhttp.timeout = 20000;
xhttp.ontimeout = (e) => {
reject("Request Timeout");
}
xhttp.onerror = (e) => {
reject(e.type+" : Please Check Your Internet Connection");
}
var formData;
if(form != undefined && form != null){
formData = new FormData(form);
} else {
formData = new FormData();
}
if(extra != undefined && extra != null){
for (const [key, value] of Object.entries(extra)) {
formData.append(key, value);
}
}
xhttp.onreadystatechange = function () {
if(this.readyState == 4 && this.status == 200){
var responseText = JSON.parse(this.responseText)
resolve(responseText);
}
};
xhttp.open("POST", target, true);
xhttp.send(formData);
}
}