diff --git a/apps/static/js/home/subscriptions.js b/apps/static/js/home/subscriptions.js index ac25076..9d3def6 100644 --- a/apps/static/js/home/subscriptions.js +++ b/apps/static/js/home/subscriptions.js @@ -217,7 +217,15 @@ $("#addSubscriptionBtn").on("click", async function() { await showEditSubModal(-1); }); +function clearPreviousValidation() { + $("#subFormModal, #subAdvancedModal").removeClass("was-validated"); + $("#subFormModal .invalid-feedback, #subAdvancedModal .invalid-feedback").remove(); + $("#subFormModal .is-invalid, #subAdvancedModal .is-invalid").removeClass("is-invalid"); +} + async function showEditSubModal(subId) { + clearPreviousValidation(); + if (subId === -1) { $("#subFormModal .form-create, #subAdvancedModal .form-create").show(); $("#subFormModal .form-edit, #subAdvancedModal .form-edit").hide(); @@ -303,6 +311,7 @@ $("#subForm").on("submit", async function(event) { // Populate formdata with [data-field] control values $('#subForm [data-field], #subAdvancedModal [data-field]').each(function() { const value = getValueFromField(this); + if (Array.isArray(value) && !value.length) { return }; formData.append($(this).data("field"), value); }); @@ -321,10 +330,18 @@ $("#subForm").on("submit", async function(event) { filter => formData.append("filters", parseInt(filter.value)) ); - // Unique Content Rules - $("#subUniqueRules option:selected").toArray().forEach( - rule => formData.append("unique_content_rules", parseInt(rule.value)) - ); + for (const [key, value] of formData.entries()) { + console.log(`${key}: ${value}`); + } + + // // Unique Content Rules + // $("#subUniqueRules option:selected").toArray().forEach( + // rule => formData.append("unique_content_rules", parseInt(rule.value)) + // ); + + for (const [key, value] of formData.entries()) { + console.log(`${key}: ${value}`); + } // This field is constructed differently, so needs to be specifically added formData.append("embed_colour", getColourInputVal("subEmbedColour", false)); @@ -361,7 +378,21 @@ async function saveSubscription(id, formData, handleErrorMsg=true) { response = id === "-1" ? await newSubscription(formData) : await editSubscription(id, formData); } catch (err) { - console.error(err); + if (typeof err !== "object" && err.responseJSON) { + return false + } + + clearPreviousValidation(); + for (const [fieldKey, message] of Object.entries(err.responseJSON)) { + const $field = $(`#subFormModal [data-field="${fieldKey}"], #subAdvancedModal [data-field="${fieldKey}"]`); + const $helpText = $field.closest("div").find(".form-text"); + const $feedback = $(`
${message}
`); + + $field.addClass("is-invalid").prop("invalid", true); + $feedback.insertAfter($helpText.length ? $helpText : $field); + } + + $("#subFormModal, #subAdvancedModal").addClass("was-validated"); if (handleErrorMsg) { showToast("danger", "Subscription Error", err.responseText, 18000); diff --git a/apps/templates/home/includes/submodal.html b/apps/templates/home/includes/submodal.html index e4dbbc7..01d914a 100644 --- a/apps/templates/home/includes/submodal.html +++ b/apps/templates/home/includes/submodal.html @@ -15,14 +15,14 @@
- +
Use a unique name to refer to this subscription.
- +
Must point to a valid RSS feed.
@@ -129,14 +129,14 @@
- +
RSS content older than this datetime will be skipped.
- +
Prevent duplicate articles, by identifying articles matched by these selected fields.
@@ -144,7 +144,7 @@

- +
Show images on the discord embed?