| Davit Tabidze | aec6d8a | 2024-07-29 18:30:09 +0400 | [diff] [blame] | 1 | async function loadPublicData() { |
| 2 | let networkSelect = document.querySelector("select#network"); |
| 3 | if (networkSelect === undefined) { |
| 4 | return; |
| 5 | } |
| 6 | let appTypeSelect = document.querySelector("select#app-type"); |
| 7 | if (appTypeSelect === undefined) { |
| 8 | return; |
| 9 | } |
| 10 | networkSelect.innerHTML = `<option value="" disabled selected>domain</option>`; |
| 11 | appTypeSelect.innerHTML = `<option value="" disabled selected>application type</option>`; |
| 12 | let resp = await fetch(`${apiBaseURL}/api/public-data`); |
| 13 | if (!resp.ok) { |
| 14 | return; |
| 15 | } |
| 16 | let data = await resp.json(); |
| 17 | data.networks.forEach((network) => { |
| 18 | let opt = document.createElement("option"); |
| 19 | opt.setAttribute("value", network.domain); |
| 20 | opt.innerHTML = network.domain; |
| 21 | networkSelect.appendChild(opt); |
| 22 | }); |
| 23 | data.types.forEach((t) => { |
| 24 | let opt = document.createElement("option"); |
| 25 | opt.setAttribute("value", t); |
| 26 | opt.innerHTML = t; |
| 27 | appTypeSelect.appendChild(opt); |
| 28 | }); |
| 29 | } |
| 30 | |
| 31 | function errorRender(error) { |
| 32 | const errorMsg = document.getElementById("error-message"); |
| 33 | errorMsg.innerHTML = error; |
| 34 | errorMsg.style.display = "block"; |
| 35 | } |
| 36 | |
| 37 | function triggerForm(status, errDisplay, buttonTxt, spinnerStatus) { |
| 38 | const form = document.getElementById('register-form'); |
| 39 | const elements = form.querySelectorAll('input, select, textarea, button'); |
| 40 | elements.forEach(element => { |
| 41 | element.disabled = status; |
| 42 | }); |
| 43 | const errorMsg = document.getElementById("error-message"); |
| 44 | errorMsg.style.display = errDisplay; |
| 45 | const button = document.getElementById("create-app-button"); |
| 46 | button.removeChild(button.lastChild); |
| 47 | button.appendChild(document.createTextNode(buttonTxt)); |
| 48 | const spinner = document.getElementById("spinner"); |
| 49 | spinner.style.display = spinnerStatus; |
| 50 | } |
| 51 | |
| 52 | async function register(event) { |
| 53 | event.preventDefault(); |
| 54 | const data = { |
| 55 | type: document.getElementById("app-type").value, |
| 56 | adminPublicKey: document.getElementById("public-key").value, |
| 57 | network: document.getElementById("network").value, |
| 58 | subdomain: document.getElementById("subdomain").value, |
| 59 | }; |
| 60 | triggerForm(true, "none", "\u00A0\u00A0\creating first app", "inline-block"); |
| 61 | fetch(`${apiBaseURL}/api/apps`, { |
| 62 | method: "POST", |
| 63 | body: JSON.stringify(data) |
| 64 | }) |
| 65 | .then(response => { |
| 66 | if (!response.ok) { |
| 67 | errorRender("Internal error, try again"); |
| 68 | triggerForm(false, "block", "create first app", "none"); |
| 69 | } |
| 70 | return response.json(); |
| 71 | }) |
| 72 | .then(result => { |
| 73 | const domain = document.getElementById("network").value; |
| 74 | const subdomain = document.getElementById("subdomain").value; |
| 75 | const appLink = `https://${subdomain}.${domain}`; |
| 76 | const appStatusLink = `https://status.${subdomain}.${domain}`; |
| 77 | |
| 78 | const successHTML = ` |
| 79 | <div class="registration-outcome"> |
| 80 | <h3>Application has been successfully deployed, use information below to access it:</h3> |
| 81 | <button onclick="window.open('${appLink}', '_blank')">Application address: ${appLink}</button> |
| 82 | <br> |
| 83 | <button onclick="window.open('${appStatusLink}', '_blank')">Status page address: ${appStatusLink}</button> |
| 84 | <br> |
| 85 | <button class="pass" onclick="copyPassword('${result.password}')" id="copy-button"> |
| 86 | Status page password: <strong>${result.password}</strong> |
| 87 | <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 256 256"> |
| 88 | <path fill="currentColor" d="M216 32H88a8 8 0 0 0-8 8v40H40a8 8 0 0 0-8 8v128a8 8 0 0 0 8 8h128a8 8 0 0 0 8-8v-40h40a8 8 0 0 0 8-8V40a8 8 0 0 0-8-8m-56 176H48V96h112Zm48-48h-32V88a8 8 0 0 0-8-8H96V48h112Z" /> |
| 89 | </svg> |
| 90 | </button> |
| 91 | <div id="tooltip" class="tooltip">Password copied to clipboard</div> |
| 92 | </div>`; |
| 93 | document.getElementById("form-container").innerHTML = successHTML; |
| 94 | }) |
| 95 | .catch(error => { |
| 96 | errorRender(`Failed to deploy application. Error: '${error.message}'`); |
| 97 | triggerForm(false, "block", "create first app", "none"); |
| 98 | }) |
| 99 | .finally(() => { |
| 100 | document.getElementById("spinner").style.display = "none"; |
| 101 | }); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | function copyPassword(password) { |
| 106 | navigator.clipboard.writeText(password).then(() => { |
| 107 | const button = document.getElementById("copy-button"); |
| 108 | const tooltip = document.getElementById("tooltip"); |
| 109 | const rect = button.getBoundingClientRect(); |
| 110 | const tooltipWidth = tooltip.offsetWidth; |
| 111 | tooltip.style.top = `${rect.top - 30 + window.scrollY}px`; |
| 112 | tooltip.style.left = `${rect.left + (rect.width / 2) - (tooltipWidth / 2) + window.scrollX}px`; |
| 113 | tooltip.style.opacity = "1"; |
| 114 | tooltip.style.visibility = "visible"; |
| 115 | setTimeout(() => { |
| 116 | tooltip.style.opacity = "0"; |
| 117 | tooltip.style.visibility = "hidden"; |
| 118 | }, 1000); |
| 119 | }); |
| 120 | } |
| 121 | |
| 122 | document.addEventListener("DOMContentLoaded", () => { |
| 123 | loadPublicData(); |
| 124 | const registerForm = document.getElementById("register-form"); |
| 125 | if (registerForm) { |
| 126 | registerForm.onsubmit = register; |
| 127 | } |
| 128 | }); |