Created
July 9, 2019 19:02
-
-
Save ennjoy/73a22f6adf537795a41922ff9059f8ae to your computer and use it in GitHub Desktop.
xhr.js
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
// Get Transactions | |
function getTransactions() { | |
var url = api_transaction + '?access_token=' + accessToken; | |
var last_id = $('.message_class:first'), url = api_transaction + '?access_token=' + accessToken + '&last_id=' + (last_id.lenght ? last_id.attr('message-id') : 0); | |
var xhr = new XMLHttpRequest(); | |
xhr.onreadystatechange = function() { | |
if (this.readyState != 4) return; | |
if (this.status == 200) { | |
if (this.responseText) { | |
var data = JSON.parse(this.response); | |
if (data['status'] == 200) { | |
if (data['items']) { | |
$(document).ready(function () { | |
var result = data['items']; | |
var oE = $("#page-transaction"); | |
if(oE.length == 0) oE = null; | |
$.each(data['items'], function(index, value) { | |
if (value.type == 0) { | |
var type = '+'; | |
var type_color = 'gren'; | |
} else { | |
var type = '-'; | |
var type_color = 'red'; | |
} | |
if (value.sex == 0) { | |
var sex = 'img/person/man.png'; | |
} else { | |
var sex = 'img/person/woman.png'; | |
} | |
function timeConverter(UNIX_timestamp){ | |
var a = new Date(UNIX_timestamp * 1000); | |
var hour = a.getHours(); | |
var min = a.getMinutes(); | |
var mid = 'AM'; | |
if (hour == 0) { | |
hour = 12; | |
} else if (hour > 12) { | |
hour= hour%12; | |
mid = 'PM'; | |
} | |
var time = hour + ':' + min + ' ' + mid; | |
return time; | |
} | |
var time = timeConverter(value.time); | |
if (last_id.lenght) { | |
oE.prepend($('<li class="swipeout" message-id="' + value.id + '"><div class="item-content swipeout-content transaction"><div class="item-media"><img src="' + sex +'"></div><div class="item-inner"><div class="item-title"><div class="name">' + value.name + '</div><div class="time">' + time + '</div></div><div class="item-title amount"><span class="' + type_color + '">' + type + '$' + value.amount + '</span></div></div></div><div class="swipeout-actions-right"><a href="#" class="swipeout-delete transaction-delete" data-id="' + value.id + '"><i class="f7-icons">trash</i></a></div></li>')); | |
} else { | |
oE.append($('<li class="swipeout"><div class="item-content swipeout-content transaction"><div class="item-media"><img src="' + sex +'"></div><div class="item-inner"><div class="item-title"><div class="name">' + value.name + '</div><div class="time">' + time + '</div></div><div class="item-title amount"><span class="' + type_color + '">' + type + '$' + value.amount + '</span></div></div></div><div class="swipeout-actions-right"><a href="#" class="swipeout-delete transaction-delete" data-id="' + value.id + '"><i class="f7-icons">trash</i></a></div></li>')); | |
} | |
}); | |
}); | |
} else { | |
document.getElementById('page-transaction').innerHTML = '<li><div class="item-content referal">No transactions</div></li>'; | |
} | |
} else if (data.status == 400) { | |
app.dialog.alert(data.error_msg); | |
} | |
} | |
getTransactions(); | |
return; | |
} | |
if (this.status != 502) { | |
console.log(this.statusText); | |
} | |
setTimeout(getTransactions, 1000); | |
} | |
xhr.open("GET", url, true); | |
xhr.send(); | |
} | |
getTransactions(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment