Skip to content

Commit

Permalink
Implement Firestore database on exchange-rates collection
Browse files Browse the repository at this point in the history
  • Loading branch information
cetinajero committed Apr 27, 2018
1 parent ccd64d0 commit bce885c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
40 changes: 40 additions & 0 deletions _includes/body/header/exchange-rates/usd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<span class="glyphicon glyphicon-globe"></span>
<span id="sell-usd-exchange-rates"></span>
<span class="glyphicon glyphicon-info-sign"></span>
<span id="updated-at-usd-exchange-rates"></span>

<script>
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}

function monthName(date, locale, type){
name = date.toLocaleString(locale, { month: type });
return capitalize(name);
}

function parseDate(date){
return date.getDate() + " / " +
monthName(date, 'es-MX', "short") + " / " +
date.getFullYear() + " " +
((date.getHours()%12)?(date.getHours()%12):12) + ":" +
(date.getMinutes()<10?'0':'') + date.getMinutes() +
(date.getHours()>=12?' PM':' AM');
}

getRealtimeExchangeRates = function() {
const docRef = firestoredb.doc("exchange-rates/usd");
const usdSell = document.querySelector("#sell-usd-exchange-rates");
const usdUpdatedAt = document.querySelector("#updated-at-usd-exchange-rates");

docRef.onSnapshot(function (doc){
if (doc && doc.exists) {
const data = doc.data();
usdSell.innerText = "USD: " + data.sell.toFixed(2);
usdUpdatedAt.innerText = parseDate(new Date(data.updated_at));
}
});
}

getRealtimeExchangeRates();
</script>
3 changes: 3 additions & 0 deletions _includes/head/firebase/sdk.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script src="https://www.gstatic.com/firebasejs/4.11.0/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.11.0/firebase-firestore.js"></script>
<script src="/js/firebase/firebase-sdk.js"></script>

1 comment on commit bce885c

@cetinajero
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.