Recommended
@@ -24,8 +31,8 @@
const dialog = document.querySelector("dialog");
// Fetch guilds
let guilds = null;
-let is_in_participating_server = null;
const getGuilds = async () => {
+ let is_in_participating_server = false;
if (guilds === null) {
// Create the guilds dictionary object
guilds = {};
@@ -39,12 +46,13 @@ const getGuilds = async () => {
const [id, is_member] = event.data.split(',');
// Add the key-value pair to the guilds dictionary
- guilds[id] = is_member === 'true';
+ const joined = is_member === 'true';
+ guilds[id] = joined;
- if (guilds[id]) {
- setJoined(id);
+ if (joined) {
is_in_participating_server = true;
}
+ setJoined(id, joined);
});
// Function to block execution until SSE events are done
@@ -62,22 +70,17 @@ const getGuilds = async () => {
// Call the function to block execution until SSE events are done
await waitForSSE()
}
- return guilds;
+ return is_in_participating_server;
}
// Show server modal, both for welcome and server list
const showServers = async () => {
dialog.showModal();
// If the user is logged in, load the guild list
// and check if they have joined the available servers
- const guilds = await getGuilds();
- Object.keys(guilds).forEach(id => {
- if (guilds[id]) {
- setJoined(id);
- }
- })
+ await getGuilds();
};
// Switch Join button to Joined for server
-const setJoined = id => {
+const setJoined = (id, joined) => {
element = document.getElementById(id);
if (element === null) {
return;
@@ -87,12 +90,17 @@ const setJoined = id => {
if (a === null) {
return;
}
- const button = document.createElement("button");
- button.tagName = "button";
- button.className = "joinButton";
- button.disabled = true;
- button.innerHTML = "Joined"
- element.querySelector("a").replaceWith(button);
+ if (joined) {
+ const button = document.createElement("button");
+ button.tagName = "button";
+ button.className = "joinButton";
+ button.disabled = true;
+ button.innerHTML = "Joined"
+ a.replaceWith(button);
+ } else {
+ a.innerHTML = "Join";
+ a.classList.remove("disabled");
+ }
}
// Check for welcomed cookie
@@ -104,8 +112,7 @@ const loadServers = async () => {
// in case user pressed Join servers button
document.querySelector("dialog .welcome").style.display = "none";
} else {
- const guilds = await getGuilds();
- if (!is_in_participating_server) {
+ if (!(await getGuilds())) {
showServers();
}
// Even if already in participating server,
@@ -117,6 +124,7 @@ if (welcomed()) {
// TODO: Remove welcome text via cookie check in template?
document.querySelector("dialog .welcome").style.display = "none";
} else {
+ document.querySelector("dialog .participating").style.display = "none";
// If not welcomed, see if welcome is necessary
loadServers();
}