Skip to content

Commit

Permalink
chore: handle errors better
Browse files Browse the repository at this point in the history
  • Loading branch information
cdransf committed May 28, 2024
1 parent d79ee5c commit b8c45d3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
30 changes: 23 additions & 7 deletions api-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,37 @@ class ApiText extends HTMLElement {
const content = this.querySelector('.content')
const cacheKey = this.url || 'api-text-cache'
const cache = sessionStorage?.getItem(cacheKey)

const hideElement = () => { this.style.display = 'none' }

const loadText = (string) => {
loading.style.display = string ? 'none' : ''
content.style.display = string ? 'block' : 'none'
if (string) content.innerHTML = string
if (!string) {
hideElement()
return
}
loading.style.display = 'none'
content.style.display = 'block'
content.innerHTML = string
}

if (cache) loadText(JSON.parse(cache))
if (cache) {
loadText(JSON.parse(cache))
} else {
loading.style.display = 'block'
content.style.display = 'none'
}

try {
const data = await this.data
const value = data.content
loadText(value)
sessionStorage?.setItem(cacheKey, JSON.stringify(value))
if (value) {
loadText(value)
sessionStorage?.setItem(cacheKey, JSON.stringify(value))
} else {
hideElement()
}
} catch (error) {
loadText()
hideElement()
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cdransf/api-text",
"version": "1.3.1",
"version": "1.4.0",
"description": "A web component to load text from an API and display it.",
"main": "api-text.js",
"repository": {
Expand Down

0 comments on commit b8c45d3

Please sign in to comment.