-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Firestore database on exchange-rates collection
- Loading branch information
1 parent
ccd64d0
commit bce885c
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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> |
This file contains 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
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> |
bce885c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
REST API: https://firestore.googleapis.com/v1beta1/projects/grupopv-mx-firebase/databases/(default)/documents/exchange-rates/usd