Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Irina-anat committed May 2, 2023
1 parent e03faf4 commit 03a3395
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import './css/styles.css';
import { Notify } from 'notiflix/build/notiflix-notify-aio';
import SimpleLightbox from "simplelightbox";
import "simplelightbox/dist/simple-lightbox.min.css";
import { getImages, page} from "./js/getImages";
import { getImages } from "./js/getImages";
import {createMarcup} from "./js/createMarcup"

const form = document.querySelector('.search-form');
Expand All @@ -23,18 +23,17 @@ const options = {
threshold: 0,
};

const observer = new IntersectionObserver(onPagination, options);
const observer = new IntersectionObserver(onLoadMore, options);

let searchQuery = ``;
let currentPage = 1;
let perPage = 40;


form.addEventListener(`submit`, onSearch);

async function onSearch(evn) {
evn.preventDefault()
searchQuery = evn.currentTarget.elements.searchQuery.value.trim() //searchQuery.value.trim() можна буде отримати значення, коли ф-я в ін файлі
searchQuery = evn.currentTarget.elements.searchQuery.value.trim();
if (!searchQuery) {
clear()
Notify.failure("Please fill in the search field.")
Expand All @@ -59,28 +58,21 @@ async function onSearch(evn) {
lightbox.refresh();
}
}

catch (err) {
console.log('ERROR: ' + `error`)

};
}
}


function clear() {
gallery.innerHTML = ''
};

//async
function onPagination(entries, observer) {
function onLoadMore(entries, observer) {

try {
entries.forEach(async (entry) => {

if (entry.isIntersecting) {
currentPage += 1;
const data = await getImages(searchQuery, currentPage);
console.log(data)
// console.log(data)
let totalPages = Math.ceil(data.totalHits/perPage)
gallery.insertAdjacentHTML('beforeend', createMarcup(data.hits));

Expand All @@ -89,33 +81,23 @@ function onPagination(entries, observer) {
if (totalPages === currentPage) {
Notify.failure("We're sorry, but you've reached the end of search results.");
}
//scroll();
lightbox.refresh();
}
})
}
catch {
// clear()
console.log('ERROR: ' + `error`)
console.log('ERROR: ' + `error`)
}
}

function resetCurretPage() {
currentPage = 1;
}

function scroll() {
const { height: cardHeight } = document
.querySelector('.gallery')
.firstElementChild.getBoundingClientRect();

window.scrollBy({
top: cardHeight * 2,
behavior: 'smooth',
});
function clear() {
gallery.innerHTML = ''
}





Expand All @@ -126,6 +108,20 @@ function scroll() {



/*function scroll() {
const { height: cardHeight } = document
.querySelector('.gallery')
.firstElementChild.getBoundingClientRect();
window.scrollBy({
top: cardHeight * 2,
behavior: 'smooth',
});
}*/




/*const loadMoreBtn = document.querySelector(`.load-more`);
loadMoreBtn.addEventListener(`click`, onLoadMore);
btnHidden();
Expand Down

0 comments on commit 03a3395

Please sign in to comment.