diff --git a/assets/css/style.css b/assets/css/style.css index f397682..3cd9ec4 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -1,7 +1,16 @@ -.input { - max-width: 50%; +.field.is-grouped { + display: flex; + gap: .75rem; + justify-content: center; } +#authorSearchResults { + display: flex; + justify-content: center; + margin: 0 auto; + flex-direction: column; + align-items: center; +} .card { background-color: #a1cfdd; @@ -14,4 +23,4 @@ justify-content: space-evenly; margin: 10px; padding: 10px; -} +} \ No newline at end of file diff --git a/assets/js/script.js b/assets/js/script.js index 6f5b5fa..658f134 100644 --- a/assets/js/script.js +++ b/assets/js/script.js @@ -262,4 +262,75 @@ function getApi() { }); }; -searchButton.addEventListener("click", getApi); \ No newline at end of file +searchButton.addEventListener("click", getApi); + + +async function searchByAuthor(authorName) { + const searchUrl = `https://openlibrary.org/search/authors.json?q=${encodeURIComponent(authorName)}`; + + try { + const response = await fetch(searchUrl); + const data = await response.json(); + + if (data.numFound > 0) { + const authors = data.docs.slice(0, 5).map(author => ({ + key: author.key, + name: author.name, + top_work: author.top_work, + })); + return authors; + } else { + return []; + } + } catch (error) { + return []; + } +} + +async function fetchAuthorWorks(authorKey, limit = 5) { + const worksUrl = `https://openlibrary.org/authors/${authorKey}/works.json?limit=${limit}`; + + try { + const response = await fetch(worksUrl); + const data = await response.json(); + + if (data.entries && data.entries.length > 0) { + const works = data.entries.map(work => ({ + title: work.title, + key: work.key, + })); + return works; + } else { + return []; + } + } catch (error) { + return []; + } +} + +document.getElementById('searchButton').addEventListener('click', async () => { + const searchInput = document.getElementById('searchInput').value; + const searchType = document.getElementById('searchType').value; + const authorResultsContainer = document.getElementById('authorSearchResults'); + + if (searchType.toLowerCase() === 'author') { + const authors = await searchByAuthor(searchInput); + if (authors.length > 0) { + const authorKey = authors[0].key; + const works = await fetchAuthorWorks(authorKey, 5); + + if (works.length > 0) { + authorResultsContainer.textContent = "Top Works" + works.forEach(work => { + const workElement = document.createElement('div'); + workElement.textContent = `${work.title}`; + authorResultsContainer.appendChild(workElement); + }); + } else { + authorResultsContainer.textContent = "No works found"; + } + } else { + authorResultsContainer.textContent = "No authors found"; + } + } +}); diff --git a/index.html b/index.html index 897ddbb..b3463d9 100644 --- a/index.html +++ b/index.html @@ -3,27 +3,35 @@ - Perfect Ambient Generator
-

Perfect Ambient Generator

+

Perfect Ambient Generator

- -
- +
+
+ +
+
+
+ +
+
+
+ +
-
-
+
+