functional modal for settings

This commit is contained in:
Corban-Lee Jones 2024-08-16 18:45:46 +01:00
parent e30f431dc3
commit 1d1f519216
2 changed files with 65 additions and 18 deletions

View File

@ -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;
}

View File

@ -8,7 +8,25 @@
</h5>
</div>
<div class="modal-body p-4">
test
<input type="hidden" id="guildSettingsId" name="guildSettingsId">
<input type="hidden" id="guildSettingsGuildId" name="guildSettingsGuildId">
<div class="mb-4">
<div class="colour-input"
data-id="guildSettingsDefaultEmbedColour"
data-label="Default Embed Colour"
data-helptext="Default colour of each embed in Discord."
data-defaultcolour="#3498db">
</div>
</div>
<div>
<div class="form-switch ps-0">
<label for="guildSettingsActive" class="form-check-label mb-2">Server Active?</label>
<br>
<input type="checkbox" name="guildSettingsActive" id="guildSettingsActive" class="form-check-input ms-0 mt-0">
<br>
<div class="form-text">Is this server active?</div>
</div>
</div>
</div>
<div class="modal-footer px-4">
<button type="submit" class="btn btn-primary rounded-1 me-3 ms-0">Save Changes</button>