|
| 1 | +// modules |
| 2 | +const { ipcRenderer } = require('electron') |
| 3 | +const axios = require('axios') |
| 4 | + |
| 5 | +const toast = require('../scripts/toast') |
| 6 | + |
| 7 | +// DOM elements |
| 8 | +const keywordSearch = document.querySelector('#keyword-search') |
| 9 | +const startSearch = document.querySelector('#potter-search') |
| 10 | +const characterList = document.querySelector('#character-list') |
| 11 | + |
| 12 | +// potter character search info |
| 13 | +const getCharacters = async () => { |
| 14 | + try { |
| 15 | + const { data } = await axios.get("https://potterapi-fedeperin.vercel.app/en/characters", { |
| 16 | + params: { search: keywordSearch.value } |
| 17 | + }) |
| 18 | + |
| 19 | + data.forEach((character) => { |
| 20 | + const container = document.createElement('article') |
| 21 | + container.classList.add('app-glass', 'info') |
| 22 | + |
| 23 | + const image = document.createElement('img') |
| 24 | + image.classList.add('info-img') |
| 25 | + image.src = character.image |
| 26 | + image.alt = character.nickname |
| 27 | + |
| 28 | + /* const characterName = document.createElement('strong') |
| 29 | + characterName.classList.add('info-title') |
| 30 | + characterName.textContent = `${character.fullName} - "${character.nickname}"` */ |
| 31 | + |
| 32 | + const desc = document.createElement('p') |
| 33 | + desc.classList.add('info-desc') |
| 34 | + desc.innerHTML = ` |
| 35 | + <strong>${character.fullName} - "${character.nickname}"</strong> |
| 36 | + hogwarts house: ${ character.hogwartsHouse }<br> |
| 37 | + interpreted by: ${ character.interpretedBy } |
| 38 | + ` |
| 39 | + |
| 40 | + const birthdate = document.createElement('time') |
| 41 | + birthdate.classList.add('info-birthdate') |
| 42 | + birthdate.textContent = character.birthdate |
| 43 | + birthdate.dateTime = character.birthdate |
| 44 | + |
| 45 | + |
| 46 | + container.append(image, desc, birthdate) |
| 47 | + |
| 48 | + characterList.append(container) |
| 49 | + }) |
| 50 | + } catch(err) { |
| 51 | + toast(err.message) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// events |
| 56 | +startSearch.addEventListener('submit', (e) => { |
| 57 | + characterList.innerHTML = '' |
| 58 | + |
| 59 | + getCharacters() |
| 60 | + |
| 61 | + e.preventDefault() |
| 62 | + startSearch.reset() |
| 63 | +}) |
| 64 | + |
| 65 | + |
| 66 | +ipcRenderer.on('clear-stack', () => (characterList.innerHTML = '')) |
0 commit comments