Skip to content

Commit db5f829

Browse files
committed
New version app async/await
1 parent 45c28c6 commit db5f829

File tree

5 files changed

+244
-460
lines changed

5 files changed

+244
-460
lines changed

JS/compare/compare.js

Lines changed: 76 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,10 @@ const shoppingCard = document.querySelector(".shopping-icon");
5151
const shoppingCardContainer = document.querySelector(".shopping-card");
5252
const addToCardSingles = document.querySelectorAll(".add-to-card");
5353

54-
// get all learn more buttons
55-
setTimeout(() => {
56-
const learnMore = document.querySelectorAll(".learn-card");
57-
58-
learnMore.forEach((button) => {
59-
button.addEventListener("click", () => {
60-
window.location.replace("comingSonn.html");
61-
});
62-
});
63-
}, 1000);
64-
6554
// eventListeners ----------------------------------------
6655
eventListeners();
6756
function eventListeners() {
68-
// add datalist automaticlly (get from Ajax)
57+
// add datalist automaticlly
6958
document.addEventListener("DOMContentLoaded", loadDataLists);
7059

7160
// first search button events
@@ -85,36 +74,16 @@ function eventListeners() {
8574

8675
// functions ----------------------------------------
8776

88-
// add datalist automaticlly (get from Ajax) (1/2)
89-
function loadDataLists() {
90-
// create the object
91-
const XML = new XMLHttpRequest();
92-
93-
// open the object
94-
XML.open("GET", "../JSON/phonesData.json", true);
95-
96-
// load the object
97-
XML.onload = function () {
98-
if (this.readyState == 4 && this.status == 200) {
99-
// get all Samsung phones
100-
let phonesDataSamsung = JSON.parse(this.responseText).samsung;
101-
// send samsung
102-
createDataListOptions(phonesDataSamsung);
103-
104-
// get all iPhone phones
105-
let phonesDataiPhone = JSON.parse(this.responseText).iphone;
106-
// send iPhone
107-
createDataListOptions(phonesDataiPhone);
108-
109-
// get all Xiaomi phones
110-
let phonesDataXiaomi = JSON.parse(this.responseText).xiaomi;
111-
// send Xiaomi
112-
createDataListOptions(phonesDataXiaomi);
113-
}
114-
};
77+
// add datalist (input list + inside HTML) automaticlly
78+
async function loadDataLists() {
79+
// fetch data from json api
80+
const res = await fetch("../../JSON/phonesData.json");
81+
const data = await res.json();
11582

116-
// send the object
117-
XML.send();
83+
// add phones to data list
84+
createDataListOptions(data.samsung);
85+
createDataListOptions(data.iphone);
86+
createDataListOptions(data.xiaomi);
11887
}
11988

12089
// create the option tag of datalists for each brand (2/2)
@@ -170,124 +139,78 @@ function searchSecond() {
170139
}
171140

172141
// function for all search buttons
173-
function allSearchBtns(searchedPhone, index) {
142+
async function allSearchBtns(searchedPhone, index) {
143+
// stop loader func
144+
const stopLoader = () => {
145+
setTimeout(() => {
146+
// hide loader
147+
document
148+
.querySelector(".loader-shadow")
149+
.classList.add("loader-shadow-hidden");
150+
}, 1300);
151+
};
152+
174153
// if the search value is not empty
175154
if (searchedPhone != "") {
176-
// create the object
177-
const XML = new XMLHttpRequest();
178-
179-
// open the object
180-
XML.open("GET", "../JSON/phonesData.json", true);
181-
182-
// load the object
183-
XML.onload = function () {
184-
if (this.readyState == 4 && this.status == 200) {
185-
// get all Samsung phones
186-
let phonesDataSamsung = JSON.parse(this.responseText).samsung;
187-
188-
// get all iPhone phones
189-
let phonesDataiPhone = JSON.parse(this.responseText).iphone;
190-
191-
// get all Xiaomi phones
192-
let phonesDataXiaomi = JSON.parse(this.responseText).xiaomi;
193-
194-
// status of searched value
195-
let status = "";
196-
197-
// find the phone
198-
phonesDataSamsung.forEach((phoneSamsung) => {
199-
if (phoneSamsung.name.toLowerCase() == searchedPhone) {
200-
// start from samsung ***********
201-
202-
// send it to createTheCard function to make the card and append
203-
createTheCard(phoneSamsung, index);
204-
205-
// send it to setSpecsTable function to set the information in table
206-
setSpecsTable(phoneSamsung, index);
207-
208-
// change status
209-
status = "ok";
210-
}
211-
212-
// Next is iPhone ***********
213-
phonesDataiPhone.forEach((phoneIPhone) => {
214-
if (phoneIPhone.name.toLowerCase() == searchedPhone) {
215-
// send it to createTheCard function to make the card and append
216-
createTheCard(phoneIPhone, index);
217-
218-
// send it to setSpecsTable function to set the information in table
219-
setSpecsTable(phoneIPhone, index);
220-
221-
// change status
222-
status = "ok";
223-
}
224-
225-
// Next is Xiaomi ***********
226-
phonesDataXiaomi.forEach((phoneXiaomi) => {
227-
if (phoneXiaomi.name.toLowerCase() == searchedPhone) {
228-
// send it to createTheCard function to make the card and append
229-
createTheCard(phoneXiaomi, index);
230-
231-
// send it to setSpecsTable function to set the information in table
232-
setSpecsTable(phoneXiaomi, index);
233-
234-
// change status
235-
status = "ok";
236-
}
237-
});
238-
});
239-
});
240-
241-
// // get all learn more buttons
242-
setTimeout(() => {
243-
const learnMore = document.querySelectorAll(".learn-card");
244-
245-
learnMore.forEach((button) => {
246-
button.addEventListener("click", () => {
247-
window.location.replace("comingSonn.html");
248-
});
249-
});
250-
}, 500);
251-
252-
// eventListeners for phonecard buttons
253-
if (index == 0) {
254-
setTimeout(() => {
255-
// eventListenr for first phone add-to-card button
256-
document
257-
.querySelector(".card-0")
258-
.addEventListener("click", addToCard);
259-
}, 500);
260-
} else if (index == 1) {
261-
setTimeout(() => {
262-
// eventListenr for first phone add-to-card button
263-
document
264-
.querySelector(".card-1")
265-
.addEventListener("click", addToCard);
266-
}, 500);
267-
}
268-
269-
// if searched value was wrong (not Ok) then alert
270-
if (status != "ok") {
271-
// else if it is empty
272-
alert("Enter a valid value");
273-
seachInputFirst.value = "";
274-
} else if ((status = "ok")) {
275-
setTimeout(() => {
276-
// hide loader
277-
document
278-
.querySelector(".loader-shadow")
279-
.classList.add("loader-shadow-hidden");
280-
}, 1300);
281-
}
282-
}
283-
};
284-
285-
// send the object
286-
XML.send();
155+
// fetch data from json api
156+
const res = await fetch("../../JSON/phonesData.json");
157+
const data = await res.json();
158+
159+
// get all phones
160+
const phonesDataSamsung = data.samsung;
161+
const phonesDataiPhone = data.iphone;
162+
const phonesDataXiaomi = data.xiaomi;
163+
164+
// find searched phone
165+
const samsungFound = phonesDataSamsung.find(
166+
(phone) => phone.name.toLowerCase() == searchedPhone
167+
);
168+
const iphoneFound = phonesDataiPhone.find(
169+
(phone) => phone.name.toLowerCase() == searchedPhone
170+
);
171+
const xiaomiFound = phonesDataXiaomi.find(
172+
(phone) => phone.name.toLowerCase() == searchedPhone
173+
);
174+
175+
if (samsungFound) {
176+
// send it to createTheCard function to make the card and append
177+
createTheCard(samsungFound, index);
178+
// send it to setSpecsTable function to set the information in table
179+
setSpecsTable(samsungFound, index);
180+
// stop loader when phone is found
181+
stopLoader();
182+
} else if (iphoneFound) {
183+
createTheCard(iphoneFound, index);
184+
setSpecsTable(iphoneFound, index);
185+
stopLoader();
186+
} else if (xiaomiFound) {
187+
createTheCard(xiaomiFound, index);
188+
setSpecsTable(xiaomiFound, index);
189+
stopLoader();
190+
} else {
191+
alert("Enter a valid value");
192+
stopLoader();
193+
}
194+
195+
//get all learn more buttons
196+
const learnMore = document.querySelectorAll(".learn-card");
197+
learnMore.forEach((button) => {
198+
button.addEventListener("click", () => {
199+
window.location.replace("comingSonn.html");
200+
});
201+
});
202+
203+
// add eventlistener to add-to-card btn
204+
if (index == 0) {
205+
document.querySelector(".card-0").addEventListener("click", addToCard);
206+
} else {
207+
document.querySelector(".card-1").addEventListener("click", addToCard);
208+
}
287209
} else {
288210
// else if it is empty
289211
alert("Enter a valid value");
290212
seachInputFirst.value = "";
213+
stopLoader();
291214
}
292215
}
293216

0 commit comments

Comments
 (0)