improved admin page behaviour

Changed admin.js so that instead of deleting all invites and then
generating them, new ones are created, pre-existing ones have their
expiry updated, and newly invalid ones are deleted. Accidentally
improved their layout, as well.
This commit is contained in:
Harvey Tindall 2020-04-13 20:02:00 +01:00
parent c00311bd15
commit 0ab93990e8

View File

@ -1,8 +1,25 @@
function parseInvite(invite, empty = false) {
if (empty === true) {
return ["None", "", "1"]
} else {
var i = ["", "", "0"];
i[0] = invite['code'];
if (invite['hours'] == 0) {
i[1] = invite['minutes'] + 'm';
} else if (invite['minutes'] == 0) {
i[1] = invite['hours'] + 'h';
} else {
i[1] = invite['hours'] + 'h ' + invite['minutes'] + 'm';
}
i[1] = "Expires in " + i[1] + " ";
return i
}
}
function addItem(invite) { function addItem(invite) {
var links = document.getElementById('invites'); var links = document.getElementById('invites');
var listItem = document.createElement('li'); var listItem = document.createElement('li');
listItem.classList.add('list-group-item'); listItem.id = invite[0]
listItem.classList.add('align-middle'); listItem.classList.add('list-group-item', 'align-middle');
var listCode = document.createElement('div'); var listCode = document.createElement('div');
listCode.classList.add('float-left'); listCode.classList.add('float-left');
var codeLink = document.createElement('a'); var codeLink = document.createElement('a');
@ -11,28 +28,37 @@ function addItem(invite) {
listItem.appendChild(listCode); listItem.appendChild(listCode);
var listRight = document.createElement('div'); var listRight = document.createElement('div');
listRight.classList.add('float-right'); listRight.classList.add('float-right');
listRight.appendChild(document.createTextNode(invite[1])); listText = document.createElement('div');
listText.id = invite[0] + '_expiry'
listText.appendChild(document.createTextNode(invite[1]));
listRight.appendChild(listText);
if (invite[2] == 0) { if (invite[2] == 0) {
var inviteCode = window.location.href + 'invite/' + invite[0]; var inviteCode = window.location.href + 'invite/' + invite[0];
codeLink.href = inviteCode; codeLink.href = inviteCode;
listCode.appendChild(document.createTextNode(" ")); listCode.appendChild(document.createTextNode(" "));
var codeCopy = document.createElement('i'); var codeCopy = document.createElement('i');
codeCopy.onclick = function(){toClipboard(inviteCode)}; codeCopy.onclick = function(){toClipboard(inviteCode)};
codeCopy.classList.add('fa'); codeCopy.classList.add('fa', 'fa-clipboard');
codeCopy.classList.add('fa-clipboard');
listCode.appendChild(codeCopy); listCode.appendChild(codeCopy);
var listDelete = document.createElement('button'); var listDelete = document.createElement('button');
listDelete.onclick = function(){deleteInvite(invite[0])}; listDelete.onclick = function(){deleteInvite(invite[0])};
listDelete.classList.add('btn'); listDelete.classList.add('btn', 'btn-outline-danger', 'float-right');
listDelete.classList.add('btn-outline-danger');
listDelete.appendChild(document.createTextNode('Delete')); listDelete.appendChild(document.createTextNode('Delete'));
listRight.appendChild(listDelete); listRight.appendChild(listDelete);
}; };
listItem.appendChild(listRight); listItem.appendChild(listRight);
links.appendChild(listItem); links.appendChild(listItem);
}; };
function updateInvite(invite) {
var expiry = document.getElementById(invite[0] + '_expiry');
expiry.textContent = invite[1];
}
function removeInvite(code) {
var item = document.getElementById(code);
item.parentNode.removeChild(item);
}
function generateInvites(empty = false) { function generateInvites(empty = false) {
document.getElementById('invites').textContent = ''; // document.getElementById('invites').textContent = '';
if (empty === false) { if (empty === false) {
$.ajax('/getInvites', { $.ajax('/getInvites', {
type : 'GET', type : 'GET',
@ -48,26 +74,40 @@ function generateInvites(empty = false) {
complete: function(response) { complete: function(response) {
var data = JSON.parse(response['responseText']); var data = JSON.parse(response['responseText']);
if (data['invites'].length == 0) { if (data['invites'].length == 0) {
addItem(["None", "", "1"]); document.getElementById('invites').textContent = '';
addItem(parseInvite([], true));
} else { } else {
data['invites'].forEach(function(item) { data['invites'].forEach(function(invite) {
var i = ["", "", "0"]; var match = false;
i[0] = item['code']; var items = document.getElementById('invites').children;
if (item['hours'] == 0) { for (var i = 0; i < items.length; i++) {
i[1] = item['minutes'] + 'm'; if (items[i].id == invite['code']) {
} else if (item['minutes'] == 0) { match = true;
i[1] = item['hours'] + 'h'; updateInvite(parseInvite(invite));
} else { };
i[1] = item['hours'] + 'h ' + item['minutes'] + 'm'; };
} if (match == false) {
i[1] = "Expires in " + i[1] + " "; addItem(parseInvite(invite));
addItem(i) };
}); });
} var items = document.getElementById('invites').children;
for (var i = 0; i < items.length; i++) {
var exists = false;
data['invites'].forEach(function(invite) {
if (items[i].id == invite['code']) {
exists = true;
}
});
if (exists == false) {
removeInvite(items[i].id);
}
};
};
} }
}); });
} else if (empty === true) { } else if (empty === true) {
addItem(["None", "", "1"]); document.getElementById('invites').textContent = '';
addItem(parseInvite([], true));
}; };
}; };
function deleteInvite(code) { function deleteInvite(code) {
@ -147,8 +187,7 @@ $("form#loginForm").submit(function() {
if (data['status'] == 401) { if (data['status'] == 401) {
var formBody = document.getElementById('formBody'); var formBody = document.getElementById('formBody');
var wrongPassword = document.createElement('div'); var wrongPassword = document.createElement('div');
wrongPassword.classList.add('alert'); wrongPassword.classList.add('alert', 'alert-danger');
wrongPassword.classList.add('alert-danger');
wrongPassword.setAttribute('role', 'alert'); wrongPassword.setAttribute('role', 'alert');
wrongPassword.appendChild(document.createTextNode('Incorrect username or password.')); wrongPassword.appendChild(document.createTextNode('Incorrect username or password.'));
formBody.appendChild(wrongPassword); formBody.appendChild(wrongPassword);