data-field rewrite
All checks were successful
Build and Push Docker Image / build (push) Successful in 14s

This commit is contained in:
Corban-Lee Jones 2024-09-02 18:37:55 +01:00
parent 85c5b9c1f2
commit 5d893fed37
2 changed files with 78 additions and 60 deletions

View File

@ -233,70 +233,93 @@ async function showEditSubModal(subId) {
$("#subFormModal").modal("show");
}
function getValueFromField(elem) {
const tagName = elem.tagName.toLowerCase();
const $elem = $(elem);
if (tagName) { return $elem.val() }
switch ($elem.attr("type")) {
case "checkbox":
return $elem.prop("checked");
default:
return $elem.val();
}
}
$("#subForm").on("submit", async function(event) {
event.preventDefault();
var id = $("#subId").val();
name = $("#subName").val();
url = $("#subUrl").val();
guildId = getCurrentlyActiveServer().guild_id;
extraNotes = $("#subExtraNotes").val();
subChannels = Object.fromEntries($("#subChannels option:selected").toArray().map(channel => [channel.value, $(channel).data("name")]));
subFilters = $("#subFilters option:selected").toArray().map(filter => parseInt(filter.value));
subMutators = {
title: $("#subTitleMutators option:selected").toArray().map(mutator => parseInt(mutator.value)),
desc: $("#subDescMutators option:selected").toArray().map(mutator => parseInt(mutator.value))
}
subEmbedColour = $("#subEmbedColour .colour-text").val().split("#")[1];
articleFetchImage = $("#subArticleFetchImage").prop("checked")
publishedThreshold = $("#subPubThreshold").val();
active = $("#subActive").prop("checked");
let subId = $("#subId").val();
let guildId = getCurrentlyActiveServer().guild_id;
var subPrimaryKey = await saveSubscription(id, name, url, guildId, extraNotes, subFilters, subMutators, subEmbedColour, articleFetchImage, publishedThreshold, active);
// TODO: move this into a function, so I can fix the active toggle switches which are broken due to this change
if (!subPrimaryKey) {
alert("prevented /subscriptions/false/subchannels");
return
let formData = new FormData();
formData.append("guild_id", guildId);
// Populate formdata with [data-field] control values
$('#subForm [data-field], #subAdvancedModal [data-field]').each(function() {
const value = getValueFromField(this);
formData.append($(this).data("field"), value);
});
// Add title mutators to formdata
$("#subTitleMutators option:selected").toArray().map(mutator => parseInt(mutator.value)).forEach(
mutator => formData.append("article_title_mutators", mutator)
);
// Add description mutator to formdata
$("#subDescMutators option:selected").toArray().map(mutator => parseInt(mutator.value)).forEach(
mutator => formData.append("article_desc_mutators", mutator)
);
// Add Filters to formdata
$("#subFilters option:selected").toArray().forEach(
filter => formData.append("filters", parseInt(filter.value))
);
// This field is constructed differently, so needs to be specifically added
formData.append("embed_colour", getColourInputVal("subEmbedColour", false));
subId = await saveSubscription(subId, formData);
if (subId) {
showToast("success", "Subscription Saved", `Subscription ID ${subId}`);
}
else {
showToast("danger", "Error Saving Subscription", "");
return;
}
await deleteSubChannels(subPrimaryKey);
for (channelId in subChannels) {
await saveSubChannel(channelId, subChannels[channelId], subPrimaryKey);
}
if (subPrimaryKey) {
showToast("success", "Subscription Saved", "Subscription ID: " + subPrimaryKey);
await loadSubscriptions(guildId);
}
await deleteSubChannels(subId);
$("#subChannels option:selected").each(async function() {
let $channel = $(this);
let channelFormData = new FormData();
channelFormData.append("channel_id", $channel.val());
channelFormData.append("channel_name", $channel.data("name"));
channelFormData.append("subscription", subId);
await newSubChannel(channelFormData);
});
await loadSubscriptions(guildId);
$("#subFormModal").modal("hide");
});
async function saveSubscription(id, name, url, guildId, extraNotes, filters, mutators, embedColour, articleFetchImage, publishedTheshold, active, handleErrorMsg=true) {
var formData = new FormData();
formData.append("name", name);
formData.append("url", url);
formData.append("guild_id", guildId);
formData.append("extra_notes", extraNotes);
filters.forEach(filter => formData.append("filters", filter));
mutators.title.forEach(mutator => formData.append("article_title_mutators", mutator));
mutators.desc.forEach(mutator => formData.append("article_desc_mutators", mutator));
formData.append("embed_colour", embedColour);
formData.append("article_fetch_image", articleFetchImage);
formData.append("published_threshold", publishedTheshold);
formData.append("active", active);
var response;
async function saveSubscription(id, formData, handleErrorMsg=true) {
let response
try {
if (id === "-1") response = await newSubscription(formData);
else response = await editSubscription(id, formData);
response = id === "-1" ? await newSubscription(formData) : await editSubscription(id, formData);
}
catch (err) {
console.error(err)
console.error(err);
if (handleErrorMsg)
if (handleErrorMsg) {
showToast("danger", "Subscription Error", err.responseText, 18000);
}
return false;
}
@ -304,12 +327,7 @@ async function saveSubscription(id, name, url, guildId, extraNotes, filters, mut
return response.id;
}
async function saveSubChannel(channelId, channelName, subscriptionId) {
var formData = new FormData();
formData.append("channel_id", channelId);
formData.append("channel_name", channelName);
formData.append("subscription", subscriptionId);
async function saveSubChannel(formData) {
var response
try {

View File

@ -10,19 +10,19 @@
</h5>
</div>
<div class="modal-body p-4">
<input type="hidden" id="subId" name="subId">
<input type="hidden" id="subId" name="subId" data-role="is-id">
<div class="row">
<div class="col-lg-6 pe-lg-4">
<div class="mb-4">
<label for="subName" class="form-label">Name</label>
<input type="text" id="subName" name="subName" class="form-control rounded-1" placeholder="My News Feed" tabindex="1">
<input type="text" id="subName" name="subName" class="form-control rounded-1" placeholder="My News Feed" data-field="name" tabindex="1">
<div class="form-text">Use a unique name to refer to this subscription.</div>
</div>
</div>
<div class="col-lg-6 ps-lg-4">
<div class="mb-4">
<label for="subUrl" class="form-label">URL</label>
<input type="url" id="subUrl" name="subUrl" class="form-control rounded-1" placeholder="http://example.com/rss.xml" tabindex="2">
<input type="url" id="subUrl" name="subUrl" class="form-control rounded-1" placeholder="http://example.com/rss.xml" data-field="url" tabindex="2">
<div class="form-text">Must point to a valid <a href="https://en.wikipedia.org/wiki/RSS" class="text-decoration-none" target="_blank">RSS</a> feed.</div>
</div>
</div>
@ -43,14 +43,14 @@
<div class="col-lg-6 pe-lg-4">
<div class="mb-4 mb-lg-0">
<label for="subExtraNotes" class="form-label">Extra Notes</label>
<textarea id="subExtraNotes" name="subExtraNotes" class="form-control rounded-1" placeholder="" tabindex="5" style="resize: none; height: 7rem"></textarea>
<textarea id="subExtraNotes" name="subExtraNotes" class="form-control rounded-1" placeholder="" data-field="extra_notes" tabindex="5" style="resize: none; height: 7rem"></textarea>
</div>
</div>
<div class="col-lg-6 ps-lg-4">
<div class="form-switch mb-4 ps-0">
<label for="subActive" class="form-check-label mb-2">Active</label>
<br>
<input type="checkbox" id="subActive" name="subActive" class="form-check-input ms-0 mt-0" tabindex="6">
<input type="checkbox" id="subActive" name="subActive" class="form-check-input ms-0 mt-0" data-field="active" tabindex="6">
<br>
<div class="form-text">Inactive subscriptions wont be processed.</div>
</div>
@ -129,7 +129,7 @@
<div class="col-lg-6 ps-lg-4">
<div class="mb-4">
<label for="subPubThreshold" class="form-label">Publish Datetime Threshold</label>
<input type="datetime-local" name="subPubThreshold" id="subPubThreshold" class="form-control" tabindex="9">
<input type="datetime-local" name="subPubThreshold" id="subPubThreshold" class="form-control" data-field="published_threshold" tabindex="9">
<div class="form-text">RSS content older than this datetime will be skipped.</div>
</div>
</div>
@ -137,7 +137,7 @@
<div class="form-switch ps-0">
<label for="subArticleFetchImage" class="form-check-label mb-2">Show Images on Embed?</label>
<br>
<input type="checkbox" id="subArticleFetchImage" name="subArticleFetchImage" class="form-check-input ms-0 mt-0" tabindex="10">
<input type="checkbox" id="subArticleFetchImage" name="subArticleFetchImage" class="form-check-input ms-0 mt-0" data-field="article_fetch_image" tabindex="10">
<br>
<div class="form-text">Show images on the discord embed?</div>
</div>