-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
97 lines (88 loc) · 3.52 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
document.addEventListener('DOMContentLoaded', function () {
const wordElement = document.getElementById('word');
const imageContainer = document.getElementById('imageContainer');
const searchInput = document.getElementById('searchInput');
const searchButton = document.getElementById('searchButton');
const overlay = document.getElementById('overlay');
const overlayImage = document.getElementById('overlayImage');
const closeOverlayButton = document.getElementById('closeOverlay');
const downloadButton = document.getElementById('downloadButton');
let currentWord = ''; //set default word
let isDownloading = false;
let currentImageUrl = '';
require('dotenv').config();
fetchImages();
searchButton.addEventListener('click', function () {
currentWord = searchInput.value.trim();
fetchImages();
});
searchInput.addEventListener('keydown', function (event) {
if (event.key === 'Enter') {
searchButton.click();
}
});
async function fetchImages() {
try {
const apiKey = process.env.UNSPLASH_API_KEY;
const perPage = 20; // per page imgs
const response = await fetch(`https://api.unsplash.com/search/photos?query=${currentWord}&per_page=${perPage}&client_id=${apiKey}`, {
method: 'GET',
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
wordElement.textContent = currentWord;
displayImages(data.results);
} catch (error) {
console.error('Error fetching images:', error.message);
}
}
function displayImages(images) {
imageContainer.innerHTML = '';
images.forEach((imageData) => {
const imageElement = document.createElement('img');
imageElement.src = imageData.urls.small;
imageElement.alt = 'Unsplash Image';
imageElement.className = 'image';
imageContainer.appendChild(imageElement);
imageElement.addEventListener('click', function () {
openOverlay(imageData.urls.regular);
currentImageUrl = imageData.urls.regular;
});
});
}
function openOverlay(imageUrl) {
overlayImage.src = imageUrl;
overlay.style.display = 'flex';
closeOverlayButton.addEventListener('click', closeOverlay);
downloadButton.addEventListener('click', function downloadHandler() {
if (!isDownloading) {
isDownloading = true;
downloadImage(currentImageUrl);
}
});
}
function closeOverlay() {
overlay.style.display = 'none';
overlayImage.src = '';
isDownloading = false;
downloadButton.removeEventListener('click', downloadHandler);
}
function downloadImage(imageUrl) {
fetch(imageUrl)
.then(response => response.blob())
.then(blob => {
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = 'downloaded_image.jpg'; //change download name
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
})
.catch(error => console.error('Error fetching image:', error))
.finally(() => {
isDownloading = false;
});
}
});