You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. toastr.options = {
  2. "positionClass": "toast-bottom-right"
  3. };
  4. var priceData = "";
  5. //get the dataset
  6. $(document).ready(function() {
  7. $.ajax({
  8. type: "GET",
  9. url: "https://api.fairplayground.info/rawdata/faircoin_prices.csv",
  10. dataType: "text",
  11. success: processData
  12. });
  13. $('#inputFaircoin').on('input', calculateEuros);
  14. $('#inputEuro').on('input', calculateFaircoins);
  15. $('#inputDate').on('input', calculateEuros);
  16. var strDate = new Date().toISOString().slice(0,10);
  17. $('#inputDate').val(strDate);
  18. });
  19. function csvToArray(csv) {
  20. var a1 = csv.split("\n");
  21. var r = [];
  22. for(var i=0; i<a1.length; i++) {
  23. var a2 = a1[i].split(",");
  24. r.push(a2);
  25. }
  26. return(r);
  27. }
  28. function processData(data) {
  29. priceData = csvToArray(data);
  30. };
  31. function calculateEuros() {
  32. document.getElementById("result").innerHTML="";
  33. var faircoins = document.getElementById("inputFaircoin").value;
  34. var date = document.getElementById("inputDate").value;
  35. if(!faircoins){
  36. toastr.warning("please, fill the faircoin input box");
  37. return;
  38. }
  39. if(!date){
  40. toastr.warning("no date");
  41. return;
  42. }
  43. //let's find the date in the priceData
  44. var selectedPriceData = []
  45. for(var i=0; i<priceData.length; i++) {
  46. if(priceData[i][0]==date) {
  47. selectedPriceData = priceData[i];
  48. }
  49. }
  50. if(selectedPriceData.length<1) {
  51. toastr.warning("selected date not found in the dataset");
  52. return;
  53. }
  54. console.log(selectedPriceData);
  55. var euros = faircoins * selectedPriceData[3];
  56. document.getElementById("inputEuro").value= euros;
  57. document.getElementById("result").innerHTML = "The official rate was " + parseFloat(selectedPriceData[3]).toString() + " <p>" + faircoins + " FAIR were worth " + euros + " €";
  58. toastr.success(faircoins + " FAIR were " + euros + " €");
  59. }
  60. function calculateFaircoins() {
  61. document.getElementById("result").innerHTML="";
  62. var euros = document.getElementById("inputEuro").value;
  63. var date = document.getElementById("inputDate").value;
  64. if(!euros){
  65. toastr.warning("please, fill the euro input box");
  66. return;
  67. }
  68. if(!date){
  69. toastr.warning("no date");
  70. return;
  71. }
  72. //let's find the date in the priceData
  73. var selectedPriceData = []
  74. for(var i=0; i<priceData.length; i++) {
  75. if(priceData[i][0]==date) {
  76. selectedPriceData = priceData[i];
  77. }
  78. }
  79. if(selectedPriceData.length<1) {
  80. toastr.warning("selected date not found in the dataset");
  81. return;
  82. }
  83. console.log(selectedPriceData);
  84. var faircoins = euros / selectedPriceData[3];
  85. document.getElementById("inputFaircoin").value= faircoins;
  86. document.getElementById("result").innerHTML = "The official rate was " + parseFloat(selectedPriceData[3]).toString() + " <p>" + euros + " € were worth " + faircoins + " FAIR";
  87. toastr.success(euros + "€ were " + faircoins + " FAIR");
  88. }