Don't remove the prefilled entry when loading the instances

This commit is contained in:
Andy Balaam 2020-09-24 08:39:45 +01:00
parent 06f7c372b5
commit e27cb3f123

View file

@ -38,6 +38,19 @@ function add_instance(text, disabled, selected) {
choose_instance.appendChild(opt); choose_instance.appendChild(opt);
} }
function add_loading_instance() {
add_instance("... loading ...", true);
}
function remove_loading_instance() {
const last_index = choose_instance.children.length - 1;
const last_entry = choose_instance.children[last_index];
if (last_entry.innerText === "... loading ...") {
last_entry.remove();
}
}
function parseUrl(url) { function parseUrl(url) {
const parser = document.createElement('a'); const parser = document.createElement('a');
parser.href = url; parser.href = url;
@ -52,10 +65,8 @@ if (prefillInstance != null) {
choose_instance.addEventListener('focus', function (e) { choose_instance.addEventListener('focus', function (e) {
if (choose_instance.children.length < 3) { if (choose_instance.children.length < 3) {
if (choose_instance.children.length > 1) { remove_loading_instance();
choose_instance.children[1].remove(); add_loading_instance();
}
add_instance("... loading ...", true);
fetch('https://api.joinmastodon.org/servers') fetch('https://api.joinmastodon.org/servers')
.then(response => { .then(response => {
if (!response.ok) { if (!response.ok) {
@ -64,7 +75,7 @@ choose_instance.addEventListener('focus', function (e) {
return response.json(); return response.json();
}) })
.then(servers => { .then(servers => {
choose_instance.children[1].remove(); remove_loading_instance();
const domains = servers.map(obj => obj.domain); const domains = servers.map(obj => obj.domain);
domains.sort(); domains.sort();
for (const domain of domains) { for (const domain of domains) {
@ -72,7 +83,7 @@ choose_instance.addEventListener('focus', function (e) {
} }
}) })
.catch(error => { .catch(error => {
choose_instance.children[1].remove(); remove_loading_instance();
add_instance("--LOADING FAILED--", true); add_instance("--LOADING FAILED--", true);
console.error( console.error(
'Failed to fetch servers list from joinmastodon.', error); 'Failed to fetch servers list from joinmastodon.', error);