-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
108 lines (86 loc) · 2.77 KB
/
app.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
98
99
100
101
102
103
104
105
106
107
108
const auth = "563492ad6f917000010000011b0450e2ec8347eb9e414e77dece0c2a";
const searchInput = document.querySelector(".search-input");
const form = document.querySelector(".search-form");
const gallery = document.querySelector(".gallery");
const moreBtn = document.querySelector(".more-btn");
const burger = document.querySelector(".burger");
// Variables
let searchValue;
let page = 1;
let fetchLink;
// Event listeners
searchInput.addEventListener("input", updateInput);
form.addEventListener("submit", (e) => {
e.preventDefault();
searchPhotos(searchValue);
});
moreBtn.addEventListener("click", loadMore);
burger.addEventListener("click", navToggle);
function updateInput(e) {
e.preventDefault();
searchValue = e.target.value;
}
async function fetchApi(url) {
const fetchPhotos = await fetch(url, {
method: "GET",
headers: {
Accept: "application/json",
Authorization: auth,
},
});
const data = await fetchPhotos.json();
return data;
}
function generateGallery(data) {
data.photos.forEach((photo) => {
const generateGallery = document.createElement("div");
generateGallery.classList.add("photo");
generateGallery.innerHTML = `
<p>${photo.photographer}</p>
<a href="${photo.src.large}"><img src="${photo.src.large}"> </img></a>
`;
gallery.appendChild(generateGallery);
});
}
async function getPhotos() {
const fetchLink = "https://api.pexels.com/v1/curated?per_page=15&page=1";
const data = await fetchApi(fetchLink);
generateGallery(data);
}
async function searchPhotos(query) {
clear();
const fetchLink = `https://api.pexels.com/v1/search?query=${query}&per_page=15&page=1`;
const data = await fetchApi(fetchLink);
generateGallery(data);
}
async function loadMore() {
page++;
if (searchValue) {
fetchLink = `https://api.pexels.com/v1/search?query=${searchValue}&per_page=15&page=${page}`;
} else {
fetchLink = `https://api.pexels.com/v1/curated?per_page=15&page=${page}`;
}
const data = await fetchApi(fetchLink);
generateGallery(data);
}
function clear() {
gallery.innerHTML = "";
searchInput.value = "";
}
function navToggle(e) {
console.log(e);
if (!e.target.classList.contains("active")) {
e.target.classList.add("active");
gsap.to(".line1", 0.5, { rotate: "45", y: 5, background: "black" });
gsap.to(".line2", 0.5, { rotate: "-45", y: -2, background: "black" });
gsap.to(".nav-bar", 1, { clipPath: "circle(2500px at 100% -10%)" });
document.body.classList.add("hide");
} else {
e.target.classList.remove("active");
gsap.to(".line1", 0.5, { rotate: "0", y: 0 });
gsap.to(".line2", 0.5, { rotate: "0", y: 0 });
gsap.to(".nav-bar", 1, { clipPath: "circle(50px at 100% -10%)" });
document.body.classList.remove("hide");
}
}
getPhotos();