Skip to content

Commit

Permalink
enhance: set up observers for various searches
Browse files Browse the repository at this point in the history
  • Loading branch information
Valik3201 committed Dec 29, 2023
1 parent bfa4701 commit 487752e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/app/js/formSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
clearGallery,
} from "./searchService.js";

import { intersectionObserver } from "./popularSearch.js";

/**
* Handle the submission of the search form.
*
Expand All @@ -26,11 +28,13 @@ const handleSearchFormSubmit = async (event) => {
// If the search query is not empty, perform Pixabay image search and render the results
if (searchQuery !== "") {
// Perform Pixabay image search and get the total number of hits
const totalHits = await searchAndRenderPixabayImages(searchQuery);
const totalHits = await searchAndRenderPixabayImages(searchQuery, "form");

// Update the UI with information about the search results
updateResultsInfo(searchQuery, totalHits);
}

intersectionObserver.disconnect();
};

// Add an event listener to the search form for form submission handling
Expand Down
2 changes: 1 addition & 1 deletion src/app/js/loadMoreButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const handleLoadMoreButtonClick = async () => {
// If the search query is not empty, perform Pixabay image search and render more results
if (searchQuery !== "") {
// Perform Pixabay image search and render additional results
await searchAndRenderPixabayImages(searchQuery);
await searchAndRenderPixabayImages(searchQuery, "form");
}
};

Expand Down
10 changes: 7 additions & 3 deletions src/app/js/searchService.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const updateResultsInfo = (query, totalHits) => {
* @param {string} query - The search query.
* @returns {Promise<number>} - The total number of hits.
*/
export const searchAndRenderPixabayImages = async (query) => {
export const searchAndRenderPixabayImages = async (query, typeSearch) => {
try {
// Display loading dots while fetching data.
Loading.dots();
Expand All @@ -98,8 +98,12 @@ export const searchAndRenderPixabayImages = async (query) => {
Notify.info("We're sorry, but you've reached the end of search results.");
}

// Show or hide the load more button based on the number of hits.
loadMoreButton.style.display = hits.length >= perPage ? "block" : "none";
if (typeSearch === "form") {
// Show or hide the load more button based on the number of hits.
loadMoreButton.style.display = hits.length >= perPage ? "block" : "none";
} else {
loadMoreButton.style.display = "none";
}

// Increment the page for the next search.
incrementPage();
Expand Down

0 comments on commit 487752e

Please sign in to comment.