2019-12-17 18:46:54 -05:00
|
|
|
function add_tag(event) {
|
2019-12-21 16:42:08 -05:00
|
|
|
input_tag = event.target.closest('.tag');
|
|
|
|
post_id = input_tag.closest('.post_container').id;
|
|
|
|
tag_name = input_tag.children[0].value;
|
2019-12-11 19:47:19 -05:00
|
|
|
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.') }
|
2019-12-21 16:42:08 -05:00
|
|
|
event.target.parentElement.firstElementChild.value = '';
|
2019-12-11 19:47:19 -05:00
|
|
|
let tag = document.createElement('span');
|
|
|
|
tag.innerText = tag_name;
|
|
|
|
tag.className = 'tag';
|
2019-12-17 18:46:54 -05:00
|
|
|
let span = document.createElement('span');
|
|
|
|
span.className = 'tag_button';
|
|
|
|
span.addEventListener('click', remove_tag);
|
|
|
|
span.innerText = '-';
|
|
|
|
tag.appendChild(span);
|
2019-12-21 16:42:08 -05:00
|
|
|
input_tag.closest('.tags').insertBefore(tag, input_tag);
|
2019-12-11 19:47:19 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-17 18:46:54 -05:00
|
|
|
function remove_tag(event) {
|
|
|
|
tag = event.target.closest('.tag');
|
2019-12-11 19:47:19 -05:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|