{"size": 1511, "ext": "js", "max_stars_repo_path": "src/index.js", "max_stars_repo_name": "RomanSoroka85/goit-js-hw-13-image-finder", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/index.js", "max_issues_repo_name": "RomanSoroka85/goit-js-hw-13-image-finder", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/index.js", "max_forks_repo_name": "RomanSoroka85/goit-js-hw-13-image-finder", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.1833333333, "max_line_length": 74, "alphanum_fraction": 0.6637988087}
import './styles.css';
import apiService from './js/apiService';
import refs from './js/refs';
import debounce from 'lodash.debounce';
import templateGallery from './templates/templateGallery.hbs';

// apiService.toGetFeatch();
const btn = document.querySelector('.load-more');

refs.search.addEventListener(
  'input',
  debounce(e => {
    apiService.resetPage();
    if (e.target.value === '') {
      refs.gallery.innerHTML = '';
      return;
    }
    apiService.query = e.target.value;
    apiService.toGetFeatch().then(data => {
      (refs.gallery.innerHTML = templateGallery(data)), removeClass(data);
    });
  }, 500),
);

refs.loadMoreBtn.addEventListener('click', () => {
  apiService.setPage();
  autoScroll();
  apiService.toGetFeatch().then(data => {
    refs.gallery.insertAdjacentHTML('beforeend', templateGallery(data)),
      removeClass(data);
  });
});

function removeClass(data) {
  console.log(data.length);
  if (data.length > 11) {
    btn.classList.remove('is-hidden');
  } else {
    btn.classList.add('is-hidden');
  }
}

function autoScroll() {
  let scrollHeight =
    Math.max(
      document.body.scrollHeight,
      document.documentElement.scrollHeight,
      document.body.offsetHeight,
      document.documentElement.offsetHeight,
      document.body.clientHeight,
      document.documentElement.clientHeight,
    ) - document.documentElement.clientHeight;
  setTimeout(() => {
    window.scrollTo({
      top: scrollHeight,
      behavior: 'smooth',
    });
  }, 500);
}
