forked from surajondev/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (81 loc) · 2.63 KB
/
script.js
File metadata and controls
90 lines (81 loc) · 2.63 KB
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
const form = document.querySelector('.js-form');
form.addEventListener('submit', handleSubmit);
const nextBtn = document.querySelector('.js-next');
const prevBtn = document.querySelector('.js-prev');
let resultStats = document.querySelector('.js-result-stats');
const spinner = document.querySelector('.js-spinner');
let totalResults;
let currentPage = 1;
let searchQuery;
const apiKey = "Glk2p7yYemtbRnuA1y-RhCsIWopUQ6CwmTP3IreZ_4E";
nextBtn.addEventListener('click', () => {
currentPage += 1;
fetchResults(searchQuery);
});
prevBtn.addEventListener('click', () => {
currentPage -= 1;
fetchResults(searchQuery);
});
function pagination(totalPages) {
nextBtn.classList.remove('hidden');
if (currentPage >= totalPages) {
nextBtn.classList.add('hidden');
}
prevBtn.classList.add('hidden');
if (currentPage !== 1) {
prevBtn.classList.remove('hidden');
}
}
async function fetchResults(searchQuery) {
spinner.classList.remove('hidden');
try {
const results = await searchUnsplash(searchQuery);
pagination(results.total_pages);
console.log(results);
displayResults(results);
} catch(err) {
console.log(err);
alert('Failed to search Unsplash');
}
spinner.classList.add('hidden');
}
function handleSubmit(event) {
event.preventDefault();
currentPage = 1;
const inputValue = document.querySelector('.js-search-input').value;
searchQuery = inputValue.trim();
console.log(searchQuery);
fetchResults(searchQuery);
}
async function searchUnsplash(searchQuery) {
const endpoint = `https://api.unsplash.com/search/photos?query=${searchQuery}&per_page=30&page=${currentPage}&client_id=${apiKey}`;
const response = await fetch(endpoint);
if (!response.ok) {
throw Error(response.statusText);
}
const json = await response.json();
return json;
}
function displayResults(json) {
const searchResults = document.querySelector('.search-results');
searchResults.textContent = '';
json.results.forEach(result => {
const url = result.urls.small;
const unsplashLink = result.links.html;
const photographer = result.user.name;
const photographerPage = result.user.links.html;
searchResults.insertAdjacentHTML(
'beforeend',
`<div>
<a href="${unsplashLink}" target="_blank">
<div class="result-item" style="background-image: url(${url});"></div>
</a>
<p class="photographer-name">
<a href="${photographerPage}" target="_blank" style="color: black; text-decoration: none;">Photo by ${photographer}</a>
</p>
</div>`
);
});
totalResults = json.total;
resultStats.textContent = `About ${totalResults} results found`;
};