2019-02-21 20:28:09 +01:00
"use strict" ; /* global document, fetch, history, send, window */
2019-02-05 00:42:52 +01:00
2019-02-12 00:47:01 +01:00
document . getElementById ( "get-started" ) . onclick = event => {
2019-02-05 00:42:52 +01:00
event . preventDefault ( ) ;
send ( {
message : "auth me with github"
} ) ;
2019-02-12 00:47:01 +01:00
} ;
if ( window . location . search . includes ( "?code=" ) ) {
document . querySelector ( "developer-program" ) . innerHTML = `
2019-02-21 20:28:09 +01:00
< form onsubmit = "return false;" >
2019-02-12 00:47:01 +01:00
< input - submit >
< input id = "walletAddress" placeholder = "Your LBRY wallet address" type = "text" / >
< input id = "oauthCode" type = "hidden" value = "${window.location.search.split(" ? code = ").pop()}" / >
< button id = "creditsAcquire" title = "Get LBRY credits" type = "button" > Get credits < / b u t t o n >
< / i n p u t - s u b m i t >
< / f o r m >
2019-02-18 21:31:48 +01:00
2019-02-19 19:17:32 +01:00
< h4 > Need An Address ? < / h 4 >
2019-02-18 21:31:48 +01:00
< p > To receive your LBC , you ' ll need a wallet address . While graphical wallets are available , the recommended path for engineers is to : < / p >
< ol >
2019-06-21 05:43:42 +02:00
< li > Download < a href = "https://github.com/lbryio/lbry-sdk/releases" > the LBRY SDK < / a > . < / l i >
2019-02-19 19:17:32 +01:00
< li > Launch the command - line utility ( < code > . / lbrynet start < / c o d e > ) . < / l i >
2019-02-25 17:31:59 +01:00
< li > Run < code > . / lbrynet address unused < / c o d e > a n d c o p y t h e < c o d e > i d < / c o d e > f i e l d . < / l i >
2019-02-18 21:31:48 +01:00
< / o l >
2019-02-12 00:47:01 +01:00
` ;
history . replaceState ( { } , "" , window . location . pathname ) ; // clean up URL bar
}
if ( document . getElementById ( "creditsAcquire" ) ) {
2019-02-21 20:28:09 +01:00
document . getElementById ( "walletAddress" ) . addEventListener ( "keyup" , event => {
const key = event . keyCode ? event . keyCode : event . which ;
if ( key === 13 )
document . getElementById ( "creditsAcquire" ) . click ( ) ;
} ) ;
2019-02-12 00:47:01 +01:00
document . getElementById ( "creditsAcquire" ) . onclick = ( ) => {
send ( {
address : document . getElementById ( "walletAddress" ) . value ,
code : document . getElementById ( "oauthCode" ) . value ,
2019-02-21 20:28:09 +01:00
message : "verify github token"
2019-02-12 00:47:01 +01:00
} ) ;
2019-02-19 19:17:32 +01:00
document . querySelector ( "developer-program" ) . innerHTML = "<p><em>Awaiting response from LBRY server...</em></p>" ;
2019-02-12 00:47:01 +01:00
} ;
}
2019-02-21 20:28:09 +01:00
2019-02-22 18:41:50 +01:00
function syncWithApi ( data ) { // eslint-disable-line no-unused-vars
2019-02-21 20:28:09 +01:00
const address = data . address ;
const code = data . code ;
2019-07-12 00:09:36 +02:00
if ( code === null ) {
2019-02-21 20:28:09 +01:00
document . querySelector ( "developer-program" ) . innerHTML =
"<p><strong>There was an issue with accessing GitHub's API. Please try again later.</strong></p>" ;
2019-07-12 00:09:36 +02:00
}
2019-02-21 20:28:09 +01:00
2019-03-19 23:56:32 +01:00
fetch ( ` https://api.lbry.com/reward/new?github_token= ${ code } &reward_type=github_developer&wallet_address= ${ address } ` )
2019-02-22 18:41:50 +01:00
. then ( response => response . json ( ) )
. then ( result => {
switch ( true ) {
2019-09-12 20:48:54 +02:00
case result . error === "This reward is limited to 1 per person" :
2019-02-22 18:41:50 +01:00
document . querySelector ( "developer-program" ) . innerHTML =
"<p>You have already claimed this reward. This reward is limited to <strong>ONE</strong> per person. Your enthusiasm is appreciated.</p>" ;
2019-09-09 22:54:44 +02:00
return ;
2019-02-22 18:41:50 +01:00
2019-09-12 20:48:54 +02:00
case Boolean ( result . error ) :
2019-09-10 00:37:58 +02:00
document . querySelector ( "developer-program" ) . innerHTML =
` <p> ${ result . error } </p> ` ;
return ;
2019-02-22 18:41:50 +01:00
case result . success :
result = result . data ;
document . querySelector ( "developer-program" ) . innerHTML =
2019-03-19 23:56:32 +01:00
` <p><strong>Success!</strong> Your wallet has been credited with ${ result . reward _amount } LBC.</p><p>We have a great reference for the <a href="/api/sdk">LBRY SDK here</a> to help you get started.</p><p>You can see proof of this transaction on <a href="https://explorer.lbry.com/tx/ ${ result . transaction _id } ">our Blockchain Explorer</a>.</p> ` ;
2019-09-09 22:54:44 +02:00
return ;
2019-02-22 18:41:50 +01:00
default :
2019-09-09 22:54:44 +02:00
console . info ( data ) ; // eslint-disable-line no-console
2019-02-22 20:16:50 +01:00
document . querySelector ( "developer-program" ) . innerHTML =
2019-09-10 00:37:58 +02:00
"<p><strong>No success or error was received so the LBRY API might be down.<br/>Please try again later.</strong></p>" ;
2019-02-22 18:41:50 +01:00
}
} )
2019-09-09 22:54:44 +02:00
. catch ( error => {
console . error ( error ) ;
2019-02-22 18:41:50 +01:00
// Idk what the error would be (probably a 500) so let's just have this message
2019-02-21 20:28:09 +01:00
document . querySelector ( "developer-program" ) . innerHTML =
"<p><strong>LBRY API is down. Please try again later.</strong></p>" ;
2019-02-22 18:41:50 +01:00
} ) ;
2019-02-21 20:28:09 +01:00
}