nieuws: append create link to navigation block
[minimedit.git] / nieuws / edit.js
index 0061b025a315b61608a034178177ead4ea1a1236..2bb0c2004e50d12afe7063e7d9634eb137ff1fa1 100644 (file)
@@ -1,8 +1,7 @@
 document.addEventListener('DOMContentLoaded', () => {
-       var overview = document.getElementById('news');
-       if (overview) {
+       var nav = document.querySelector('#news .nav');
+       if (nav) {
                var editlink = document.createElement('a');
-               editlink.className = 'nav';
                editlink.appendChild(document.createTextNode('Nieuw artikel'));
                editlink.onclick = function () {
                        var today = new Date().toJSON().slice(0, 10).split('-');
@@ -14,6 +13,36 @@ document.addEventListener('DOMContentLoaded', () => {
                        window.location = window.location.pathname+'/'+url+'?edit='+title+'#edit';
                        return false;
                };
-               overview.appendChild(editlink);
+               nav.appendChild(document.createTextNode(' '));
+               nav.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}`);
+                               });
+                       });
+               });
+       });
 });