🎨 Rework UI to show queued blocks

This commit is contained in:
Ettore Di Giacinto
2021-11-01 21:07:34 +01:00
parent cb19a365f3
commit e36f3d7cf2
3 changed files with 38 additions and 30 deletions

View File

@@ -84,9 +84,9 @@ func API(l string, ledger *blockchain.Ledger) error {
ec.GET("/*", echo.WrapHandler(http.StripPrefix("/", assetHandler))) ec.GET("/*", echo.WrapHandler(http.StripPrefix("/", assetHandler)))
// ec.GET("/api/blockchain", func(c echo.Context) error { ec.GET("/api/blockchain", func(c echo.Context) error {
// return c.JSON(http.StatusOK, ledger.BlockChain()) return c.JSON(http.StatusOK, ledger.LastBlock())
// }) })
ec.GET("/api/ledger", func(c echo.Context) error { ec.GET("/api/ledger", func(c echo.Context) error {
return c.JSON(http.StatusOK, ledger.CurrentData()) return c.JSON(http.StatusOK, ledger.CurrentData())

View File

@@ -105,28 +105,36 @@
</section> </section>
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
var table = $('#table').DataTable( { var resp
"processing": true, var table = $('#table').DataTable()
"ajax": { sync = function() {
"url": "/api/blockchain", $.ajax({
"type": "GET", url: "/api/blockchain",
"dataSrc": '', type: "GET",
}, cache: false,
'language':{ async: true,
"loadingRecords": "&nbsp;",
"processing": "Loading..."
},
"columns": [
{ "data": "Index" },
{ "data": "Timestamp" },
{ "data": "Hash" },
{ "data": "PrevHash" },
],
} );
setInterval( function () { dataType: "json",
table.ajax.reload(); success: function (response) {
}, 30000 ); // response is JSON
if (resp && response.Index <= resp.Index ) {
return
}
resp = response
table.row.add( [
resp.Index,
resp.Timestamp,
resp.Hash,
resp.PrevHash
] ).draw( false );
}
});
}
sync()
setInterval( function () {
sync()
}, 5000 );
} ); } );
</script> </script>
<footer class="footer"> <footer class="footer">

View File

@@ -192,12 +192,12 @@ func (l *Ledger) CurrentData() map[string]map[string]Data {
return l.blockchain.Last().Storage return l.blockchain.Last().Storage
} }
// // BlockChain returns the current blockchain (locking) // LastBlock returns the last block in the blockchain
// func (l *Ledger) BlockChain() Blockchain { func (l *Ledger) LastBlock() Block {
// l.Lock() l.Lock()
// defer l.Unlock() defer l.Unlock()
// return l.blockchain return l.blockchain.Last()
// } }
// Add data to the blockchain // Add data to the blockchain
func (l *Ledger) Add(b string, s map[string]interface{}) { func (l *Ledger) Add(b string, s map[string]interface{}) {