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

99 lines
3.9 KiB
JavaScript
Raw Normal View History

2019-02-21 13:28:09 -06:00
"use strict"; /* global document, fetch, history, send, window */
2019-02-04 17:42:52 -06:00
2019-02-11 17:47:01 -06:00
document.getElementById("get-started").onclick = event => {
2019-02-04 17:42:52 -06:00
event.preventDefault();
send({
message: "auth me with github"
});
2019-02-11 17:47:01 -06:00
};
if (window.location.search.includes("?code=")) {
document.querySelector("developer-program").innerHTML = `
2019-02-21 13:28:09 -06:00
<form onsubmit="return false;">
2019-02-11 17:47:01 -06: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 14:31:48 -06:00
2019-02-19 13:17:32 -05:00
<h4>Need An Address?</h4>
2019-02-18 14:31:48 -06: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-20 23:43:42 -04:00
<li>Download <a href="https://github.com/lbryio/lbry-sdk/releases">the LBRY SDK</a>.</li>
2019-02-19 13:17:32 -05:00
<li>Launch the command-line utility (<code>./lbrynet start</code>).</li>
2019-02-25 11:31:59 -05:00
<li>Run <code>./lbrynet address unused</code> and copy the <code>id</code> field.</li>
2019-02-18 14:31:48 -06:00
</ol>
2019-02-11 17:47:01 -06:00
`;
history.replaceState({}, "", window.location.pathname); // clean up URL bar
}
if (document.getElementById("creditsAcquire")) {
2019-02-21 13:28:09 -06:00
document.getElementById("walletAddress").addEventListener("keyup", event => {
const key = event.keyCode ? event.keyCode : event.which;
if (key === 13)
document.getElementById("creditsAcquire").click();
});
2019-02-11 17:47:01 -06:00
document.getElementById("creditsAcquire").onclick = () => {
send({
address: document.getElementById("walletAddress").value,
code: document.getElementById("oauthCode").value,
2019-02-21 13:28:09 -06:00
message: "verify github token"
2019-02-11 17:47:01 -06:00
});
2019-02-19 13:17:32 -05:00
document.querySelector("developer-program").innerHTML = "<p><em>Awaiting response from LBRY server...</em></p>";
2019-02-11 17:47:01 -06:00
};
}
2019-02-21 13:28:09 -06:00
2019-02-22 11:41:50 -06:00
function syncWithApi(data) { // eslint-disable-line no-unused-vars
2019-02-21 13:28:09 -06:00
const address = data.address;
const code = data.code;
2019-07-11 17:09:36 -05:00
if (code === null) {
2019-02-21 13:28:09 -06: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-11 17:09:36 -05:00
}
2019-02-21 13:28:09 -06:00
2019-03-19 17:56:32 -05:00
fetch(`https://api.lbry.com/reward/new?github_token=${code}&reward_type=github_developer&wallet_address=${address}`)
2019-02-22 11:41:50 -06:00
.then(response => response.json())
.then(result => {
switch(true) {
case result.error === "This reward is limited to 1 per person":
2019-02-22 11:41:50 -06: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 15:54:44 -05:00
return;
2019-02-22 11:41:50 -06:00
case Boolean(result.error):
2019-09-09 17:37:58 -05:00
document.querySelector("developer-program").innerHTML =
`<p>${result.error}</p>`;
return;
2019-02-22 11:41:50 -06:00
case result.success:
result = result.data;
document.querySelector("developer-program").innerHTML =
2019-03-19 17:56:32 -05: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 15:54:44 -05:00
return;
2019-02-22 11:41:50 -06:00
default:
2019-09-09 15:54:44 -05:00
console.info(data); // eslint-disable-line no-console
2019-02-22 13:16:50 -06:00
document.querySelector("developer-program").innerHTML =
2019-09-09 17:37:58 -05:00
"<p><strong>No success or error was received so the LBRY API might be down.<br/>Please try again later.</strong></p>";
2019-09-09 15:54:44 -05:00
return;
2019-02-22 11:41:50 -06:00
}
})
2019-09-09 15:54:44 -05:00
.catch(error => {
console.error(error);
2019-02-22 11:41:50 -06:00
// Idk what the error would be (probably a 500) so let's just have this message
2019-02-21 13:28:09 -06:00
document.querySelector("developer-program").innerHTML =
"<p><strong>LBRY API is down. Please try again later.</strong></p>";
2019-02-22 11:41:50 -06:00
});
2019-02-21 13:28:09 -06:00
}