Last active
April 27, 2018 22:51
-
-
Save brianglass/24df39824f67a4fda7ff3a6c5f665d9b to your computer and use it in GitHub Desktop.
ES6 example of how to use the orthocal.info api
This file contains 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
// Use the client's date since the server is always set to Pacific Timezone | |
const today = new Date(); | |
// JS months are zero-origin, so we have to add 1 to the month | |
const url = `https://orthocal.info/api/oca/${today.getFullYear()}/${today.getMonth()+1}/${today.getDate()}/`; | |
fetch(url) | |
.then(response => response.json()) | |
.then(day => { | |
// Display the title of the day | |
document.getElementById('title').innerText = day.titles[0]; | |
// Display fasting information | |
const fastElement = document.getElementById('fast'); | |
if (day.fast_exception.length > 0) { | |
fastElement.innerText = `${day.fast_level_desc}: ${day.fast_exception}`; | |
} else { | |
fastElement.innerText = day.fast_level_desc; | |
} | |
// Display the readings in the <body> | |
const body = document.querySelector('body'); | |
for (const reading of day.readings) { | |
const readingElement = renderReading(reading); | |
body.appendChild(readingElement); | |
} | |
}); | |
function renderReading(reading) { | |
const element = document.createElement('div'); | |
// The title is the scripture reference | |
const titleElement = document.createElement('h2'); | |
titleElement.innerText = reading.display; | |
element.appendChild(titleElement); | |
const passageElement = document.createElement('div'); | |
passageElement.setAttribute('class', 'passage') | |
// Render the verses | |
for (const verse of reading.passage) { | |
const p = document.createElement('p'); | |
p.innerHTML = `<span class="verse-number">${verse.verse}</span> ${verse.content}`; | |
passageElement.appendChild(p); | |
} | |
element.appendChild(passageElement); | |
return element; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment