Skip to content

Commit

Permalink
HTTP requests is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
KristinaHranovska committed Nov 25, 2023
1 parent 5639eef commit 3c091f4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
"preview": "vite preview"
},
"devDependencies": {
"vite": "^4.0.5",
"glob": "^8.1.0"
"glob": "^8.1.0",
"vite": "^4.0.5"
},
"author": "Alexander Repeta <alexander.repeta@gmail.com>",
"license": "ISC",
"dependencies": {
"izitoast": "^1.4.0",
"vite-plugin-full-reload": "^1.0.5",
"vite-plugin-html-inject": "^1.0.1"
}
Expand Down
4 changes: 4 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<link rel="stylesheet" href="./css/style.css" />
</head>
<body>
<form class="js-search">
<input name="search" type="text" />
<button type="submit">Search</button>
</form>
<script type="module" src="./js/script.js"></script>
</body>
</html>
44 changes: 44 additions & 0 deletions src/js/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import iziToast from "izitoast";
import "izitoast/dist/css/iziToast.min.css";

const formSearch = document.querySelector('.js-search');
const BASE_URL = 'https://pixabay.com/api/';
const KEY = '40891115-11d0b88dd3a60afc830d1d27f';
let picktures;

const searchParams = new URLSearchParams({
key: KEY,
image_type: 'photo',
orientation: 'horizontal',
safesearch: true,
})

formSearch.addEventListener('submit', onSearch);

function onSearch(event) {
event.preventDefault();
const inputValue = event.target.elements.search.value;


getPictures(inputValue)
.then(data => {
if (!data.hits.length) {
iziToast.error({
title: 'Error',
message: 'Sorry, there are no images matching your search query. Please try again!',
});
}
})
.catch((err) => console.log(err));
}

function getPictures(name) {
return fetch(`${BASE_URL}?${searchParams}&q=${name}`)
.then(res => {
console.log(`${BASE_URL}?${searchParams}&q=${name}`);
if (!res.ok) {
throw new Error(res.statusText);
}
return res.json();
})
}

0 comments on commit 3c091f4

Please sign in to comment.