toggle sub activeness via switch

This commit is contained in:
Corban-Lee Jones 2024-09-26 16:47:29 +01:00
parent 20e713d8df
commit bc3a88e159
2 changed files with 40 additions and 4 deletions

View File

@ -1,7 +1,7 @@
const subTableId = "#subTable";
// #region Init Module
// region Init Module
function initSubscriptionsModule() {
initializeDataTable(
@ -87,9 +87,8 @@ function initSubscriptionsModule() {
);
}
// #endregion
// #region Load Data
// region Load Data
$(document).on("selectedServerChange", async function() {
await loadSubscriptionData();
@ -108,5 +107,14 @@ async function loadSubscriptionData() {
await loadTableData(subTableId, `/api/subscriptions/`, "GET");
}
// #endregion
// region Table Switches
$(subTableId).on("change", ".sub-toggle-active", async function() {
let active = $(this).prop("checked");
let id = $(subTableId).DataTable().row($(this).closest("tr")).data().id;
let sub = await ajaxRequest(`/api/subscriptions/${id}/`, "GET");
sub.active = active;
let formData = objectToFormData(sub);
await ajaxRequest(`/api/subscriptions/${id}/`, "PUT", formData);
});

View File

@ -32,6 +32,34 @@ function makeQuerystring(filters, sort) {
return sort ? querystring += `ordering=${sort}` : querystring;
}
function objectToFormData(object) {
let formData = new FormData();
Object.keys(object).forEach(key => {
let value = object[key];
if (value === null || value === undefined) {
return;
}
if (Array.isArray(value)) {
value.forEach(part => {
formData.append(key, part);
})
}
else if (typeof value === "object" && !(value instanceof File)) {
Object.keys(value).forEach(nestedKey => {
formData.append(`${key}[${nestedKey}]`, value[nestedKey]);
});
}
else {
formData.append(key, value);
}
});
return formData;
}
// Loading Channels
async function loadChannels(guildId) {
return await ajaxRequest(`/channels?guild=${guildId}`, "GET");