1
0
mirror of https://github.com/bigchaindb/site.git synced 2024-11-02 00:05:37 +01:00
site/_src/_assets/javascripts/page-getstarted.js

90 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-06-15 19:16:45 +02:00
//=include bigchain/smoothscroll.js
//=include bigchain/newsletter.js
2016-02-10 01:04:57 +01:00
jQuery(function($) {
2017-06-15 19:16:45 +02:00
//
// init modules
//
Newsletter.init()
2017-06-15 19:16:45 +02:00
})
2017-06-22 17:03:05 +02:00
2017-06-23 12:47:58 +02:00
//=include bigchaindb-driver/dist/browser/bigchaindb-driver.window.min.js
2017-06-22 17:23:48 +02:00
window.addEventListener('DOMContentLoaded', function domload(event){
window.removeEventListener('DOMContentLoaded', domload, false)
2017-06-22 17:03:05 +02:00
const driver = window.BigchainDB
const API_PATH = 'https://test.ipdb.io/api/v1/'
const APP_ID = 'b563bf22'
const APP_KEY = 'fd639614dcf8ee90a8c51a013ac11fb0'
2017-06-22 17:23:48 +02:00
const form = document.getElementById('form-transaction')
2017-06-22 17:03:05 +02:00
const postButton = document.getElementById('post')
2017-06-23 12:47:58 +02:00
const messageInput = document.getElementById('message')
// quick form validation
messageInput.addEventListener('input', function() {
if (messageInput.value === '') {
postButton.setAttribute('disabled', '')
2017-06-23 12:47:58 +02:00
} else {
postButton.removeAttribute('disabled')
2017-06-23 12:47:58 +02:00
}
})
2017-06-22 17:03:05 +02:00
postButton.addEventListener('click', function(e) {
e.preventDefault()
2017-06-23 12:47:58 +02:00
const message = messageInput.value
2017-06-22 17:03:05 +02:00
2017-06-22 17:23:48 +02:00
const alice = new driver.Ed25519Keypair()
2017-06-22 17:03:05 +02:00
const tx = driver.Transaction.makeCreateTransaction(
{ assetMessage: message },
2017-06-23 12:47:58 +02:00
{ metaDataMessage: message },
2017-06-22 17:03:05 +02:00
[ driver.Transaction.makeOutput(
driver.Transaction.makeEd25519Condition(alice.publicKey))
],
alice.publicKey
)
const txSigned = driver.Transaction.signTransaction(tx, alice.privateKey)
const conn = new driver.Connection(API_PATH, {
app_id: APP_ID,
app_key: APP_KEY
})
2017-06-22 18:12:56 +02:00
const waiting = document.getElementsByClassName('waiting')[0]
2017-06-23 12:47:58 +02:00
const responseArea = document.getElementsByClassName('response')[0]
2017-06-22 18:12:56 +02:00
const output = document.getElementsByClassName('output')[0]
2017-06-22 18:23:47 +02:00
const messageSuccess = document.getElementsByClassName('message--success')[0]
const messageFail = document.getElementsByClassName('message--fail')[0]
const transactionLink = document.getElementsByClassName('transaction-link')[0]
2017-06-22 18:12:56 +02:00
2017-06-22 17:03:05 +02:00
conn.postTransaction(txSigned)
2017-06-23 12:47:58 +02:00
.then((response) => {
2017-06-22 18:12:56 +02:00
waiting.classList.add('hide')
2017-06-23 12:47:58 +02:00
responseArea.classList.remove('hide')
messageSuccess.classList.remove('hide')
2017-06-22 18:12:56 +02:00
2017-06-23 12:47:58 +02:00
console.log(response)
const outputContent = JSON.stringify(response, null, 4) // indented with 4 spaces
2017-06-22 18:12:56 +02:00
output.textContent = outputContent
2017-06-23 12:47:58 +02:00
transactionLink.href = 'https://test.ipdb.io/api/v1/transactions/' + response.id
2017-06-22 18:12:56 +02:00
}, reason => { // Error!
console.log(reason)
waiting.classList.add('hide')
2017-06-23 12:47:58 +02:00
responseArea.classList.remove('hide')
2017-06-22 18:23:47 +02:00
messageFail.classList.remove('hide')
2017-06-22 17:03:05 +02:00
2017-06-22 18:12:56 +02:00
const outputContent = reason.status + ' ' + reason.statusText
2017-06-22 17:03:05 +02:00
output.textContent = outputContent
})
.then((res) => console.log('Transaction status:', res.status))
}, false)
}, false)