lbry.tech/app/components/client/devprogram-scripts.js

91 lines
3.6 KiB
JavaScript
Raw Normal View History

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</button>
</input-submit>
</form>
2019-02-18 21:31:48 +01:00
2019-02-19 19:17:32 +01:00
<h4>Need An Address?</h4>
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>.</li>
2019-02-19 19:17:32 +01:00
<li>Launch the command-line utility (<code>./lbrynet start</code>).</li>
2019-02-25 17:31:59 +01:00
<li>Run <code>./lbrynet address unused</code> and copy the <code>id</code> field.</li>
2019-02-18 21:31:48 +01:00
</ol>
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;
if (code === null)
document.querySelector("developer-program").innerHTML =
"<p><strong>There was an issue with accessing GitHub's API. Please try again later.</strong></p>";
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) {
case result.error === "this reward is limited to 1 per person":
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>";
break;
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-02-22 18:41:50 +01:00
break;
default:
console.log(data); // eslint-disable-line no-console
2019-02-22 20:16:50 +01:00
document.querySelector("developer-program").innerHTML =
"<p><strong>The LBRY API might be down. Please try again later.</strong></p>";
2019-02-22 18:41:50 +01:00
break;
}
})
.catch(() => {
// 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
}