Skip to content

Instantly share code, notes, and snippets.

@dosandk
Created October 7, 2022 12:19
Show Gist options
  • Select an option

  • Save dosandk/880d23eb1d1aefb9020f527c7b593529 to your computer and use it in GitHub Desktop.

Select an option

Save dosandk/880d23eb1d1aefb9020f527c7b593529 to your computer and use it in GitHub Desktop.
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