Skip to content

Commit

Permalink
cache counts
Browse files Browse the repository at this point in the history
  • Loading branch information
alidevjimmy committed Sep 11, 2024
1 parent 760028d commit 9b5e45e
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions layouts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -718,13 +718,16 @@ <h6 class="font-medium text-sm mt-2">
'Access-Control-Allow-Headers': 'Content-Type',
},
success: function (data) {
const currentTime = new Date().getTime();
const storeData = {
data: data,
timestamp: currentTime
};
localStorage.setItem('apiData', JSON.stringify(storeData));

$('#totalBlocks').text(data.data.totalBlocks || 0);
$('#totalTransactions').text(data.data.totalTransactions || 0);
$('#totalAccounts').text(data.data.totalAccounts || 0);
$('#totalValidators').text(data.data.totalValidators || 0);
$('.counter').countUp();
updateUI(data);

$('.counter').countUp();
},
error: function (xhr, status, error) {
console.error('Error fetching data:', error);
Expand All @@ -733,8 +736,34 @@ <h6 class="font-medium text-sm mt-2">
});
}

updateCountsFromAPI();
function updateUI(data) {
$('#totalBlocks').text(data.data.totalBlocks || 0);
$('#totalTransactions').text(data.data.totalTransactions || 0);
$('#totalAccounts').text(data.data.totalAccounts || 0);
$('#totalValidators').text(data.data.totalValidators || 0);
}

function checkAndUpdateCounts() {
const storedData = localStorage.getItem('apiData');

if (storedData) {
const parsedData = JSON.parse(storedData);
const currentTime = new Date().getTime();
const oneDay = 24 * 60 * 60 * 1000; // 1 day in milliseconds

if (currentTime - parsedData.timestamp < oneDay) {
updateUI(parsedData.data);
$('.counter').countUp();
return;
}
}

updateCountsFromAPI();
}

checkAndUpdateCounts();
});

</script>

<script>
Expand Down

0 comments on commit 9b5e45e

Please sign in to comment.