Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export function searchPhotos(value, page) {
  2.     // Get token from cookies.
  3.     let token = document.cookie.split('=')[1];
  4.     // Get image container from DOM.
  5.     let imgContainer = document.getElementById('imgContainer');
  6.  
  7.     // Reset imgContainer content to avoid mix of search results.
  8.     // if (document.querySelectorAll('img') != 0) {
  9.     //     imgContainer.innerHTML = "";        
  10.     // }
  11.  
  12.     fetch(`https://api.unsplash.com/search/photos?page=${page}&per_page=30&query=${value}`, {
  13.         method: 'get',
  14.         headers: {
  15.             'Authorization': `Bearer ${token}`,
  16.         }
  17.     })
  18.     .then(response => response.json())
  19.     .then(res => {
  20.         // Display random images to page
  21.         res.results.forEach(item => {
  22.             imgContainer.innerHTML += `<img class="card-img mb-3" src=${item.urls.small} large="${item.urls.small}" data-toggle="modal" data-target=".bd-example-modal-lg" alt="Card image">`;
  23.  
  24.             // Get all displayed images.
  25.             let getImages = document.querySelectorAll('img');
  26.             // Get modal.
  27.             let getModal = document.getElementById('imgModal');
  28.  
  29.             // Add click event for each img and set inner html of modal.
  30.             getImages.forEach(img => {
  31.                 img.addEventListener('click', (event) => {
  32.                     getModal.innerHTML = `<img src="${event.target.getAttribute('large')}">`;
  33.                 })
  34.             })
  35.         });
  36.         window.isFetching = false;
  37.     });
  38. }
  39.  
  40.  
  41.  
  42. // export async function searchPhotos(value) {
  43.  
  44. //     // Get token from cookies.
  45. //     let token = document.cookie.split('=')[1];
  46. //     // Get image container from DOM.
  47. //     let imgContainer = document.getElementById('imgContainer');
  48.  
  49. //     // Reset imgContainer content to avoid mix of search results.
  50. //     if (document.querySelectorAll('img') != 0) {
  51. //         imgContainer.innerHTML = "";        
  52. //     }
  53.  
  54. //     await fetch(`https://api.unsplash.com/search/photos?page=1&query=${value}`, {
  55. //         method: 'get',
  56. //         headers: {
  57. //             'Authorization': `Bearer ${token}`,
  58. //         }
  59. //     })
  60. //     .then(response => response.json())
  61. //     .then(res => {
  62. //         // Display random images to page
  63. //         res.results.forEach(item => {
  64. //             imgContainer.innerHTML += `<img class="card-img mb-3" src=${item.urls.small} large="${item.urls.small}" data-toggle="modal" data-target=".bd-example-modal-lg" alt="Card image">`;
  65. //         });
  66. //     });
  67.  
  68. //     // Get all displayed images.
  69. //     let getImages = document.querySelectorAll('img');
  70. //     // Get modal.
  71. //     let getModal = document.getElementById('imgModal');
  72.  
  73. //     // Add click event for each img and set inner html of modal.
  74. //     getImages.forEach(img => {
  75. //         img.addEventListener('click', (event) => {
  76. //             getModal.innerHTML = `<img src="${event.target.getAttribute('large')}">`;
  77. //         })
  78. //     })
  79. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement