From 0924c01bbbe0fe4f0f0a1726190e822b9b086aa4 Mon Sep 17 00:00:00 2001 From: ArtOfCode- Date: Wed, 16 Oct 2024 04:31:26 +0100 Subject: [PATCH] Test closures and update --- app/assets/javascripts/application.js | 2 +- app/assets/javascripts/closure.js | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 5892349e5..3173a0f2d 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -65,7 +65,7 @@ document.addEventListener('DOMContentLoaded', async () => { }); if (document.cookie.indexOf('dismiss_fvn') === -1) { - document.querySelector('#fvn-dismiss').addEventListener('click', _ev => { + QPixel.DOM.addSelectorListener('click', '#fvn-dismiss', _ev => { document.cookie = 'dismiss_fvn=true; path=/; expires=Fri, 31 Dec 9999 23:59:59 GMT'; }); } diff --git a/app/assets/javascripts/closure.js b/app/assets/javascripts/closure.js index 151569423..a11a32d15 100644 --- a/app/assets/javascripts/closure.js +++ b/app/assets/javascripts/closure.js @@ -4,11 +4,17 @@ document.addEventListener('DOMContentLoaded', async () => { const self = ev.target; const activeRadio = self.closest('.js-close-box').querySelector("input[type='radio'][name='close-reason']:checked"); + + if (!activeRadio) { + QPixel.createNotification('danger', 'You must select a close reason.'); + return; + } + const otherPostInput = activeRadio.closest('.widget--body').querySelector('.js-close-other-post'); - const otherPostRequired = !!activeRadio.dataset.rop; + const otherPostRequired = activeRadio.dataset.rop === 'true'; const data = { - 'reason_id': activeRadio.value, - 'other_post': otherPostInput.value + 'reason_id': parseInt(activeRadio.value, 10), + 'other_post': otherPostInput?.value // option will be silently discarded if no input element }; @@ -16,11 +22,7 @@ document.addEventListener('DOMContentLoaded', async () => { data['other_post'] = data['other_post'].replace(/.*\/([0-9]+)$/, "$1"); } - if (!activeRadio.value) { - QPixel.createNotification('danger', 'You must select a close reason.'); - return; - } - if (!otherPostInput.value && otherPostRequired) { + if (!otherPostInput?.value && otherPostRequired) { QPixel.createNotification('danger', 'You must enter an ID or URL to another post.'); return; } @@ -30,6 +32,7 @@ document.addEventListener('DOMContentLoaded', async () => { body: JSON.stringify(data), credentials: 'include', headers: { + 'Content-Type': 'application/json', 'X-CSRF-Token': QPixel.csrfToken() } });