paging + dev generate sub data

This commit is contained in:
Corban-Lee Jones 2024-06-06 11:02:42 +01:00
parent 1e9b9e905d
commit 1ee4215fbb

View File

@ -4,7 +4,7 @@ var subTable = null;
function initSubscriptionTable() {
subTable = $("#subTable").DataTable({
info: false,
paging: false,
paging: true,
searching: false,
autoWidth: false,
order: [],
@ -74,6 +74,10 @@ function initSubscriptionTable() {
}
]
});
subTable.on('page.dt length.dt', function() {
loadSubscriptions(getCurrentlyActiveServer().guild_id);
});
}
$("#subTable").on("change", ".sub-toggle-active", async function () {
@ -99,8 +103,11 @@ $("#subTable").on("change", ".sub-toggle-active", async function () {
sub.guild_id,
sub.extra_notes,
sub.filters,
active
active,
handleErrorMsg=false
);
if (!subPrimaryKey)
throw Error("This subscription no longer exists.");
showToast(
active ? "success" : "danger",
@ -108,6 +115,14 @@ $("#subTable").on("change", ".sub-toggle-active", async function () {
"Subscription ID: " + subPrimaryKey
);
}
catch (error) {
console.error(error);
showToast(
"danger",
"Error Updating Subscription",
`Tried to toggle activeness, but encountered a problem. <br><code>${error}</code>`
);
}
finally {
// Re-enable toggles after 500ms
setTimeout(() => { $(".sub-toggle-active").prop("disabled", false); }, 500)
@ -183,7 +198,7 @@ $("#subForm").on("submit", async function(event) {
$("#subFormModal").modal("hide");
});
async function saveSubscription(id, name, url, guildId, extraNotes, filters, active) {
async function saveSubscription(id, name, url, guildId, extraNotes, filters, active, handleErrorMsg=true) {
var formData = new FormData();
formData.append("name", name);
formData.append("url", url);
@ -199,7 +214,9 @@ async function saveSubscription(id, name, url, guildId, extraNotes, filters, act
else response = await editSubscription(id, formData);
}
catch (err) {
showToast("danger", "Subscription Error", err.responseText, 18000);
if (handleErrorMsg)
showToast("danger", "Subscription Error", err.responseText, 18000);
return false;
}
@ -230,18 +247,23 @@ function clearExistingSubRows() {
subTable.clear().draw(false);
}
async function loadSubscriptions(guildId) {
async function loadSubscriptions(guildId, page=1, pageSize=10) {
if (!guildId)
return;
const pageInfo = subTable.page();
alert(pageInfo);
alert(JSON.stringify(pageInfo, null, 4));
$("#deleteSelectedSubscriptionsBtn").prop("disabled", true);
clearExistingSubRows();
try {
const subscriptions = await getSubscriptions(guildId);
const subscriptions = await getSubscriptions(guildId, (pageInfo.page + 1) | 1, pageInfo.pages | 10);
subTable.rows.add(subscriptions.results).draw(false);
$("#subTable thead .table-select-all").prop("disabled", subscriptions.results.length === 0);
console.debug("loading subs, " + subscriptions.results.length + " found");
}
catch (err) {
console.error(JSON.stringify(err, null, 4));
@ -260,6 +282,45 @@ $(document).on("selectedServerChange", async function() {
await loadFilterOptions(activeServer.guild_id);
})
// Dev button (to be removed)
// auto fills dummy data into form fields to quickly create test subs
$("#devGenerateSub").on("click", function() {
const items = [
["BBC News · Top Stories", "http://feeds.bbci.co.uk/news/rss.xml"],
["BBC News · World", "http://feeds.bbci.co.uk/news/world/rss.xml"],
["BBC News · Entertainment", "http://feeds.bbci.co.uk/news/entertainment_and_arts/rss.xml"],
["BBC News · UK", "http://feeds.bbci.co.uk/news/uk/rss.xml"],
["BBC News · Business", "http://feeds.bbci.co.uk/news/business/rss.xml"],
["BBC News · Politics", "http://feeds.bbci.co.uk/news/politics/rss.xml"],
["BBC News · Health", "http://feeds.bbci.co.uk/news/health/rss.xml"],
["BBC News · Science", "http://feeds.bbci.co.uk/news/science_and_environment/rss.xml"],
["BBC News · Technology", "http://feeds.bbci.co.uk/news/technology/rss.xml"],
["BBC News · Education", "http://feeds.bbci.co.uk/news/education/rss.xml"],
["BBC News · England", "http://feeds.bbci.co.uk/news/england/rss.xml"],
["BBC News · Scotland", "http://feeds.bbci.co.uk/news/scotland/rss.xml"],
["BBC News · Wales", "http://feeds.bbci.co.uk/news/wales/rss.xml"],
["BBC News · Northern Ireland", "http://feeds.bbci.co.uk/news/northern_ireland/rss.xml"],
["BBC News · Europe", "http://feeds.bbci.co.uk/news/europe/rss.xml"],
["BBC News · Latin America", "http://feeds.bbci.co.uk/news/latin_america/rss.xml"],
["BBC News · US & Canada", "http://feeds.bbci.co.uk/news/us_and_canada/rss.xml"],
["BBC News · Middle East", "http://feeds.bbci.co.uk/news/middle_east/rss.xml"],
["BBC News · Asia", "http://feeds.bbci.co.uk/news/asia/rss.xml"],
["BBC News · Africa", "http://feeds.bbci.co.uk/news/africa/rss.xml"],
["BBC Sports", "https://news.bbc.co.uk/sport1/hi/help/rss/default.stm"],
["This Week in Self-Hosted", "https://selfh.st/rss/"],
["The Babylon Bee", "https://babylonbee.com/feed"],
["Reddit · r/memes", "https://reddit.com/r/memes/.rss"],
["Reddit · r/selfhosted", "https://reddit.com/r/selfhosted/.rss"]
];
const selected = items[Math.floor(Math.random()*items.length)]
$("#subName").val(selected[0]);
$("#subUrl").val(selected[1]);
showToast("info", "Generated", "Picked random sub data");
});
// Delete button on the 'edit subscription' modal
$("#deleteEditSub").on("click", async function() {
const subId = $("#subId").val();
@ -399,10 +460,4 @@ $("#subImage").on("change", function () {
$("#subImagePreview small").hide();
$("#subImagePreview img").attr("src", URL.createObjectURL(file)).show();
}
});
imgInp.onchange = evt => {
const [file] = imgInp.files
if (file) {
blah.src = URL.createObjectURL(file)
}
}
});