diff --git a/apps/static/js/home/settings.js b/apps/static/js/home/settings.js index 7503987..3f3b48c 100644 --- a/apps/static/js/home/settings.js +++ b/apps/static/js/home/settings.js @@ -1,29 +1,58 @@ -/** Here is how this should work - * - * Currently SavedGuild is created and destroyed on demand, it's not viable to store settings here - * instead lets create a new database obj `GuildSettings` to store these, an automated job can erase - * a `GuildSettings` instance if no matching `SavedGuilds` are found within x number of days. - * - * The tables will not be linked directly, but will share a common `guild_id` field to find - * each other, or the saved guild will have a foreign key for the related `GuildSettings`. -*/ - -$(document).on("selectedServerChange", async function() { - server = getCurrentlyActiveServer(); - updateColourInput("defaultEmbedColour", "#" + server.default_embed_colour); -}); - $("#serverSettingsBtn").on("click", async function() { await showServerSettingsModal(); }); async function showServerSettingsModal() { const server = getCurrentlyActiveServer(); + var guildSettings; + + try { guildSettings = (await getGuildSettings(server.guild_id))[0] } + catch (error) { + console.error(error) + return; + } + + $("#guildSettingsId").val(guildSettings.id); + $("#guildSettingsGuildId").val(guildSettings.guild_id); + $("#guildSettingsActive").prop("checked", guildSettings.active); + updateColourInput("guildSettingsDefaultEmbedColour", guildSettings.default_embed_colour); + $("#serverSettingsModal").modal("show"); } -$("#serverSettingsForm").on("submit", function(e) { +$("#serverSettingsForm").on("submit", async function(e) { e.preventDefault(); - alert("not implemented"); + + var id = $("#guildSettingsId").val(); + guildId = $("#guildSettingsGuildId").val(); + active = $("#guildSettingsActive").prop("checked"); + defaultEmbedColour = getColourInputVal("guildSettingsDefaultEmbedColour", false); + + const pk = await saveGuildSettings(id, guildId, defaultEmbedColour, active); + + if (pk) { + showToast("success", "Server Settings Saved", "Primary Key: " + pk); + } + + $("#serverSettingsModal").modal("hide"); + }) + +async function saveGuildSettings(id, guildId, defaultEmbedColour, active) { + var formData = new FormData(); + formData.append("guild_id", guildId); + formData.append("default_embed_colour", defaultEmbedColour); + formData.append("active", active); + + var response; + try { + response = await editGuildSettings(id, formData); + } + catch (err) { + console.error(err); + return false; + } + + return response.id; +} diff --git a/apps/templates/home/includes/settingsmodal.html b/apps/templates/home/includes/settingsmodal.html index b123974..e673180 100644 --- a/apps/templates/home/includes/settingsmodal.html +++ b/apps/templates/home/includes/settingsmodal.html @@ -8,7 +8,25 @@