2021-03-18 23:25:35 -03:00
|
|
|
@extends('layout.base')
|
|
|
|
|
|
|
|
@section('page_content')
|
|
|
|
<h2 class="ui header">
|
2021-03-19 22:48:24 -03:00
|
|
|
Monedas
|
2021-03-18 23:25:35 -03:00
|
|
|
</h2>
|
2021-03-19 22:48:24 -03:00
|
|
|
<div class="ui cards" id="cards">
|
|
|
|
</div>
|
2021-03-18 23:25:35 -03:00
|
|
|
@endsection
|
2021-03-19 22:48:24 -03:00
|
|
|
|
|
|
|
@push('scripts')
|
|
|
|
<script type="text/javascript">
|
|
|
|
let cards = {
|
|
|
|
id: '#cards',
|
|
|
|
setup: function() {
|
2021-03-30 16:21:51 -03:00
|
|
|
socket.url = '{{$urls->ws}}'
|
|
|
|
socket.connect(this.socketReady, this.getMessage)
|
2021-03-19 22:48:24 -03:00
|
|
|
},
|
|
|
|
data: [],
|
2021-03-30 16:21:51 -03:00
|
|
|
socketReady: function() {
|
|
|
|
socket.sendMessage('currencies')
|
|
|
|
},
|
|
|
|
getMessage: function(e) {
|
|
|
|
response = JSON.parse(e.data)
|
|
|
|
if (response.request.action == 'currencies') {
|
|
|
|
cards.getCurrencies(response.body)
|
|
|
|
}
|
|
|
|
if (response.request.action == 'currency.values.latest') {
|
|
|
|
cards.getValues(response.body)
|
|
|
|
cards.buildCards()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getCurrencies: function(data) {
|
|
|
|
let promises = []
|
|
|
|
$.each(data.currencies, (i, el) => {
|
|
|
|
this.data[el.id] = {'currency': el, 'value': null}
|
|
|
|
socket.sendMessage('currency.values.latest', {currency_id: el.id})
|
2021-03-19 22:48:24 -03:00
|
|
|
})
|
|
|
|
},
|
2021-03-30 16:21:51 -03:00
|
|
|
getMaxIdx: function() {
|
|
|
|
keys = Object.keys(this.data)
|
|
|
|
max = 0
|
|
|
|
$.each(keys, (i, el) => {
|
|
|
|
if (max < parseInt(el)) {
|
|
|
|
max = parseInt(el)
|
2021-03-19 22:48:24 -03:00
|
|
|
}
|
2021-03-30 16:21:51 -03:00
|
|
|
})
|
|
|
|
return max
|
|
|
|
},
|
|
|
|
getValues: function(data) {
|
|
|
|
if (data.value == null) {
|
2021-03-19 22:48:24 -03:00
|
|
|
this.data = this.data.filter(function(item) {
|
2021-03-30 16:21:51 -03:00
|
|
|
return item.currency.id != data.currency.id
|
2021-03-19 22:48:24 -03:00
|
|
|
})
|
2021-03-30 16:21:51 -03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
idx = this.data.findIndex((item, i, arr) => {
|
|
|
|
if (typeof item == 'undefined') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return item.currency.id == data.currency.id
|
2021-03-19 22:48:24 -03:00
|
|
|
})
|
2021-03-30 16:21:51 -03:00
|
|
|
if (idx < 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.data[idx].value = data.value
|
2021-03-19 22:48:24 -03:00
|
|
|
},
|
|
|
|
buildCards: function() {
|
2021-03-30 16:21:51 -03:00
|
|
|
$(this.id).html('')
|
2021-03-19 22:48:24 -03:00
|
|
|
$.each(this.data, (i, el) => {
|
2021-03-30 16:21:51 -03:00
|
|
|
if (typeof el == 'undefined') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (el.value == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (el.currency == null) {
|
|
|
|
return
|
|
|
|
}
|
2021-03-19 22:48:24 -03:00
|
|
|
$(this.id).append(
|
|
|
|
this.buildCard(el)
|
|
|
|
)
|
2021-03-30 16:21:51 -03:00
|
|
|
if (i == this.getMaxIdx()) {
|
|
|
|
socket.conn.close()
|
|
|
|
}
|
2021-03-19 22:48:24 -03:00
|
|
|
})
|
|
|
|
},
|
|
|
|
buildCard: function(currency) {
|
|
|
|
let card = $('<div></div>').attr('class', 'card').append(
|
|
|
|
$('<div></div>').attr('class', 'content').append(
|
|
|
|
$('<a></a>').attr('class', 'center aligned header').attr('href', '{{$urls->base}}/value/' + currency.currency.id + '/' + currency.value.base.id + '/' + encodeURI(readyDate(new Date(currency.value.date_time))))
|
|
|
|
.html(formatValue(currency.value.value, currency.value.base.code))
|
|
|
|
).append(
|
|
|
|
$('<div></div>').attr('class', 'center aligned description').append(
|
|
|
|
$('<a></a>').attr('href', '{{$urls->base}}/value/' + currency.currency.id + '/' + currency.value.base.id + '/' + encodeURI(readyDate(new Date(currency.value.date_time))))
|
|
|
|
.html(formatDate(new Date(currency.value.date_time)))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
).append(
|
|
|
|
$('<a></a>').attr('class', 'center aligned extra content').attr('href', '{{$urls->base}}/currency/' + currency.currency.id).html(currency.currency.name)
|
|
|
|
)
|
|
|
|
return card
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$(document).ready(() => {
|
|
|
|
cards.setup()
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
@endpush
|