-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (72 loc) · 2.13 KB
/
index.js
File metadata and controls
83 lines (72 loc) · 2.13 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
const apikey="563492ad6f917000010000019b983f3b62fe43daa92e746d4553dd35";
const input=document.querySelector("input");
const search_btn=document.querySelector(".search_btn");
const showmore_btn=document.querySelector(".showmore");
let page_num=1;
let search_text="";
let search=false;
input.addEventListener("input",(event)=>{
event.preventDefault();
search_text=event.target.value;
})
search_btn.addEventListener("click",()=>{
if(input.value==="")
{
alert("Please enter the some text")
return;
}
cleargallery();
search=true;
SearchPhotos(search_text,page_num);
})
function cleargallery(){
document.querySelector(".display_images").innerHTML="";
page_num=1;
}
async function CuratedPhotos(page_num){
const data=await fetch(`https://api.pexels.com/v1/curated?page=${page_num}`,
{
method: "GET",
headers: {
Accept: "application/json",
Authorization: apikey,
},
});
const response=await data.json();
console.log(response);
display_images(response);
}
function display_images(response){
response.photos.forEach((image) => {
const photo=document.createElement("div");
photo.innerHTML=`<img src=${image.src.large}>
<figcaption> Photo By: ${image.photographer}📸</figcaption>`;
document.querySelector(".display_images").appendChild(photo);
});
}
async function SearchPhotos(query, page_num){
const data=await fetch(`https://api.pexels.com/v1/search?query=${query}&page=${page_num}`,
{
method: "GET",
headers: {
Accept: "application/json",
Authorization: apikey,
},
});
const response=await data.json();
console.log(response);
display_images(response);
}
showmore_btn.addEventListener("click", () => {
if(!search){
page_num++;
CuratedPhotos(page_num);
}
else{
if(search_text.value==="")
return;
page_num++;
SearchPhotos(search_text,page_num);
}
})
CuratedPhotos(page_num);