Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {authorizeFunc} from './authorize.js';
- import {getToken} from './gettoken.js';
- import {getRandomPhotos} from './getrandomphotos.js';
- import {searchPhotos} from './search.js';
- import {searchTest} from './searchTest.js';
- // Get images container div.
- let imgContainer = document.getElementById('imgContainer')
- // Redirect user to authorization link.
- let authorizeBtn = document.getElementById('authorizeBtn');
- authorizeBtn.addEventListener('click', authorizeFunc);
- // Get user token by sending POST to API
- let tokenBtn = document.getElementById('tokenBtn');
- tokenBtn.addEventListener('click', getToken);
- // Create current page container.
- let getPage = 1;
- // Get search input value.
- let searchField = document.getElementById('searchField');
- // Get search button and add onClick listener.
- let searchButton = document.getElementById('searchButton');
- searchButton.addEventListener('click', () => {
- // Reset pagination.
- getPage = 1;
- // Request api for pictures.
- searchPhotos(searchField.value, getPage);
- });
- // Create flag to recognize lazy loading.
- let obj = { isFetching: false };
- // Lazy loading.
- window.onscroll = (event) => {
- let currentScrollPosition = window.innerHeight + window.scrollY;
- let currentDocumentHeight = document.body.offsetHeight;
- let currentScrollPercent = (currentScrollPosition / currentDocumentHeight) * 100;
- if (currentScrollPercent >= 80 && obj.isFetching == false) {
- console.log('call next page');
- obj.isFetching = true;
- console.log(obj.isFetching);
- // Set pagination to another page.
- getPage = getPage + 1;
- searchPhotos(searchField.value, getPage, obj.searchTest);
- }
- };
- // Display random photos on main page if user is logged in.
- window.onload = () => {
- // Check if token is empty in cookies.
- if (document.cookie.split("token=")[1] === null) {
- console.log('im here');
- // Hide login form
- document.getElementById('login').style.display = 'none';
- // Get random photos from API.
- getRandomPhotos()
- }
- }
- // Trash
- console.log(document.cookie);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement