-
-
Save sukima/25dd434cda8a197f1e1f3ac1f217a6d1 to your computer and use it in GitHub Desktop.
JS does not need to be over complicated
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @typedef {object} PoemData | |
| * @prop {string} title | |
| * @prop {string} author | |
| * @prop {string[]} lines | |
| * @prop {string} linecount | |
| */ | |
| const container = document.getElementById('poem-cont'); | |
| container.innerHTML = 'Loading poem…'; | |
| const response = await fetch('https://poems.4-walls.net/random'); | |
| if (!response.ok) { | |
| throw new Error( | |
| `Unable to fetch poem data: ${response.statusText}`, | |
| { cause: response }, | |
| ); | |
| } | |
| /** @type {PoemData[]} */ | |
| const [poemData] = await response.json(); | |
| container.innerHTML = `<h2></h2><cite></cite><p></p>`; | |
| container.querySelector('h2').textContent = poemData.title; | |
| container.querySelector('cite').textContent = poemData.author; | |
| const poemContainer = cont.querySelector('p'); | |
| for (const line of poemData.lines) { | |
| poemContainer.append(line, document.createElement('br')); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment