From bc3a88e159501440761d18814ebed47c0d2f49f3 Mon Sep 17 00:00:00 2001 From: Corban-Lee Jones Date: Thu, 26 Sep 2024 16:47:29 +0100 Subject: [PATCH] toggle sub activeness via switch --- apps/home/static/home/js/tabs/subs.js | 16 +++++++++++---- static/js/api.js | 28 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/apps/home/static/home/js/tabs/subs.js b/apps/home/static/home/js/tabs/subs.js index f9bf5f1..325f903 100644 --- a/apps/home/static/home/js/tabs/subs.js +++ b/apps/home/static/home/js/tabs/subs.js @@ -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); +}); diff --git a/static/js/api.js b/static/js/api.js index 4bcca80..07627f2 100644 --- a/static/js/api.js +++ b/static/js/api.js @@ -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");