nieuws: apply tag changes to article links in edit mode
[minimedit.git] / nieuws / edit.js
index 0061b025a315b61608a034178177ead4ea1a1236..947de7b6a3b22001a3a365d9a484a577d2b17e92 100644 (file)
@@ -16,4 +16,33 @@ document.addEventListener('DOMContentLoaded', () => {
                };
                overview.appendChild(editlink);
        }
+
+       var editlink = document.querySelector('a[href="#edit"]');
+       if (!editlink) return;
+       editlink.addEventListener('click', () => {
+               document.querySelectorAll('.tags input').forEach(tagoption => {
+                       tagoption.removeAttribute('disabled');
+                       tagoption.addEventListener('change', () => {
+                               let editpost = '/edit/nieuws/tag' + window.location.pathname;
+                               let params = new URLSearchParams;
+                               params.append('tag', tagoption.value);
+                               params.append('value', tagoption.checked ? 1 : 0);
+                               fetch(editpost, {
+                                       method: 'POST',
+                                       body: params,
+                                       credentials: 'same-origin',
+                               })
+                               .then(res => {
+                                       if (res.status != 200) {
+                                               return res.text().then(body => {
+                                                       throw `foutcode ${res.status}: ${body}`;
+                                               });
+                                       }
+                               })
+                               .catch(error => {
+                                       alert(`Tag aanpassen mislukt: ${error}`);
+                               });
+                       });
+               });
+       });
 });