Created
October 7, 2022 12:19
-
-
Save dosandk/880d23eb1d1aefb9020f527c7b593529 to your computer and use it in GitHub Desktop.
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
| class Card { | |
| total = 0; | |
| constructor ({ | |
| title = '', | |
| formatTotal = data => data | |
| } = {}) { | |
| this.title = title; | |
| this.formatTotal = formatTotal; | |
| this.loadData(); | |
| this.render(); | |
| } | |
| get template () { | |
| return ` | |
| <div> | |
| <h1>${this.title}</h1> | |
| <div> | |
| Total: <span id="totalContainer">${this.total}</span> | |
| </div> | |
| </div> | |
| `; | |
| } | |
| async loadData () { | |
| try { | |
| const response = await fetch('https:/api.some-endpoint/total'); | |
| const data = await response.json(); | |
| this.saveToBrowserCache(data); | |
| const totalContainer = document.getElementById('totalContainer'); | |
| totalContainer.innerHTML = this.formatTotal(data); | |
| } catch (error) { | |
| console.error('Error: something went wrong'); | |
| } | |
| } | |
| saveToBrowserCache (data) { | |
| localStorage.setItem('cardData', data); | |
| } | |
| render () { | |
| this.element = document.createElement('div'); | |
| this.element.innerHTML = this.template; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment