49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
function add_tag(event) {
|
|
input = event.target.closest('.tag');
|
|
post_id = input.closest('.post_container').id;
|
|
tag_name = input.children[0].value;
|
|
fetch(url_prefix + '/add_tag', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify({
|
|
'post_id': post_id,
|
|
'tag_name': tag_name,
|
|
})
|
|
}).then(function(response) {
|
|
if(!response.ok) { throw new Error('Error adding tag!'); }
|
|
return response.json();
|
|
}).then(function(json) {
|
|
if (!json.ok) { throw new Error('Could not add tag.') }
|
|
let tag = document.createElement('span');
|
|
tag.innerText = tag_name;
|
|
tag.className = 'tag';
|
|
let span = document.createElement('span');
|
|
span.className = 'tag_button';
|
|
span.addEventListener('click', remove_tag);
|
|
span.innerText = '-';
|
|
tag.appendChild(span);
|
|
input.closest('.tags').insertBefore(tag, input);
|
|
});
|
|
}
|
|
|
|
function remove_tag(event) {
|
|
console.log(event);
|
|
tag = event.target.closest('.tag');
|
|
post_id = tag.closest('.post_container').id;
|
|
tag_name = tag.innerText.replace(/ .*/, '');
|
|
fetch(url_prefix + '/remove_tag', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify({
|
|
'post_id': post_id,
|
|
'tag_name': tag_name,
|
|
})
|
|
}).then(function(response) {
|
|
if(!response.ok) { throw new Error('Error removing tag!'); }
|
|
return response.json();
|
|
}).then(function(json) {
|
|
if (!json.ok) { throw new Error('Could not remove tag.') }
|
|
tag.remove();
|
|
});
|
|
}
|