| 
 | 1 | +// Set the access key for the Unsplash API  | 
 | 2 | +const accessKey = "3I7M0FgMDBz0BC9OMC4e4wi9ByTMXZYt0Rk4fQ15TKs";  | 
 | 3 | + | 
 | 4 | +// Get elements from the HTML document using their IDs  | 
 | 5 | +const searchForm = document.getElementById("search-form");  | 
 | 6 | +const searchInput = document.getElementById("search-input");  | 
 | 7 | +const searchResults = document.getElementById("search-results");  | 
 | 8 | +const showMoreButton = document.getElementById("show-more-button");  | 
 | 9 | + | 
 | 10 | +// Initialize variables  | 
 | 11 | +let page = 1;  | 
 | 12 | +let query = "";  | 
 | 13 | + | 
 | 14 | +// Create an asynchronous function to search for images  | 
 | 15 | +async function searchImages() {  | 
 | 16 | +  // Set the query value to the input value from the search form  | 
 | 17 | +  query = searchInput.value;  | 
 | 18 | +  // Create the URL for the Unsplash API with the page number, query, and access key  | 
 | 19 | +  const url = `https://api.unsplash.com/search/photos?page=${page}&query=${query}&client_id=${accessKey}`;  | 
 | 20 | +  // Send a request to the API and wait for the response  | 
 | 21 | +  const response = await fetch(url);  | 
 | 22 | +  // Parse the response data as JSON  | 
 | 23 | +  const data = await response.json();  | 
 | 24 | +  // Get the results array from the response data  | 
 | 25 | +  const results = data.results;  | 
 | 26 | + | 
 | 27 | +  // If this is the first page of results, clear the search results div  | 
 | 28 | +  if (page === 1) {  | 
 | 29 | +    searchResults.innerHTML = "";  | 
 | 30 | +  }  | 
 | 31 | + | 
 | 32 | +  // Loop through each result and create a div with an image and link for each one  | 
 | 33 | +  results.forEach((result) => {  | 
 | 34 | +    const imageWrapper = document.createElement("div");  | 
 | 35 | +    imageWrapper.classList.add("search-result");  | 
 | 36 | + | 
 | 37 | +    const image = document.createElement("img");  | 
 | 38 | +    image.src = result.urls.small;  | 
 | 39 | +    image.alt = result.alt_description;  | 
 | 40 | + | 
 | 41 | +    const imageLink = document.createElement("a");  | 
 | 42 | +    imageLink.href = result.links.html;  | 
 | 43 | +    imageLink.target = "_blank";  | 
 | 44 | +    imageLink.textContent = result.alt_description;  | 
 | 45 | + | 
 | 46 | +    imageWrapper.appendChild(image);  | 
 | 47 | +    imageWrapper.appendChild(imageLink);  | 
 | 48 | +    searchResults.appendChild(imageWrapper);  | 
 | 49 | +  });  | 
 | 50 | + | 
 | 51 | +  // Increment the page number for the next search  | 
 | 52 | +  page++;  | 
 | 53 | + | 
 | 54 | +  // Show the "show more" button if there are more than one page of results  | 
 | 55 | +  if (page > 1) {  | 
 | 56 | +    showMoreButton.style.display = "block";  | 
 | 57 | +  }  | 
 | 58 | +}  | 
 | 59 | + | 
 | 60 | +// Listen for a submit event on the search form, prevent the default action, and search for images  | 
 | 61 | +searchForm.addEventListener("submit", (event) => {  | 
 | 62 | +  event.preventDefault();  | 
 | 63 | +  page = 1;  | 
 | 64 | +  searchImages();  | 
 | 65 | +});  | 
 | 66 | + | 
 | 67 | +// Listen for a click event on the "show more" button and search for more images  | 
 | 68 | +showMoreButton.addEventListener("click", () => {  | 
 | 69 | +  searchImages();  | 
 | 70 | +});  | 
0 commit comments