Skip to content

Commit

Permalink
added search function
Browse files Browse the repository at this point in the history
  • Loading branch information
EkaterinaBolvakina committed May 11, 2024
1 parent 6f09c03 commit c3c045b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Lesson25/ProductShop/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<input id="input-form-search" type="text" name="search" placeholder=" Search...">
</div>
<div>
<button id="search-button-icon" type="submit"><img id="imgBtn"
<button id="search-button-icon" type="button"><img id="imgBtn"
src="./image/search-button-png-file-download-free.png" alt="search button"></button>
</div>
</div>
Expand Down
35 changes: 34 additions & 1 deletion Lesson25/ProductShop/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const categoriesWrapper = document.getElementById('categories-wrapper');
const productCardsWrapper = document.getElementById('product-cards-wrapper');
const searchBtn = document.getElementById('imgBtn');
const searchInput = document.getElementById('input-form-search')
let categories = [];
let products = [];

Expand Down Expand Up @@ -138,7 +140,38 @@ const start = async () => {
}
}

start()
async function searchProducts(query) {
showSpinner();
try {
const response = await fetch(`https://dummyjson.com/products/search?q=${query}`);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
products = data.products;
displayProducts(products);
} catch (error) {
console.error('Error searching products:', error);
contentBlock.textContent = 'Error searching products.';
} finally {
hideSpinner();
}
}

function searchBtnSubscribeEvent() {
searchBtn.addEventListener('click', () => {
const query = searchInput.value.trim();
if (query !== '') {
searchProducts(query);
} else {
alert("Please enter a product.")
}
});
}



start();
searchBtnSubscribeEvent();


Empty file removed Lesson25/ProductShop/test.txt
Empty file.

0 comments on commit c3c045b

Please sign in to comment.