2022-01-28 22:00:11 +01:00
|
|
|
import $ from 'jquery';
|
2022-08-09 14:37:34 +02:00
|
|
|
|
2021-10-21 09:37:43 +02:00
|
|
|
const {csrfToken} = window.config;
|
2021-10-16 19:28:04 +02:00
|
|
|
|
2023-05-28 03:34:18 +02:00
|
|
|
export function initCompReactionSelector($parent) {
|
|
|
|
$parent.find(`.select-reaction .item.reaction, .comment-reaction-button`).on('click', async function (e) {
|
2021-10-16 19:28:04 +02:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if ($(this).hasClass('disabled')) return;
|
|
|
|
|
2023-05-28 03:34:18 +02:00
|
|
|
const actionUrl = $(this).closest('[data-action-url]').attr('data-action-url');
|
|
|
|
const reactionContent = $(this).attr('data-reaction-content');
|
|
|
|
const hasReacted = $(this).closest('.ui.segment.reactions').find(`a[data-reaction-content="${reactionContent}"]`).attr('data-has-reacted') === 'true';
|
|
|
|
|
|
|
|
const res = await fetch(`${actionUrl}/${hasReacted ? 'unreact' : 'react'}`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/x-www-form-urlencoded',
|
|
|
|
},
|
|
|
|
body: new URLSearchParams({
|
2021-10-21 09:37:43 +02:00
|
|
|
_csrf: csrfToken,
|
2023-05-28 03:34:18 +02:00
|
|
|
content: reactionContent,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = await res.json();
|
|
|
|
if (data && (data.html || data.empty)) {
|
|
|
|
const content = $(this).closest('.content');
|
|
|
|
let react = content.find('.segment.reactions');
|
|
|
|
if ((!data.empty || data.html === '') && react.length > 0) {
|
|
|
|
react.remove();
|
2021-10-16 19:28:04 +02:00
|
|
|
}
|
2023-05-28 03:34:18 +02:00
|
|
|
if (!data.empty) {
|
|
|
|
const attachments = content.find('.segment.bottom:first');
|
|
|
|
react = $(data.html);
|
|
|
|
if (attachments.length > 0) {
|
|
|
|
react.insertBefore(attachments);
|
|
|
|
} else {
|
|
|
|
react.appendTo(content);
|
2021-10-16 19:28:04 +02:00
|
|
|
}
|
2023-05-28 03:34:18 +02:00
|
|
|
react.find('.dropdown').dropdown();
|
|
|
|
initCompReactionSelector(react);
|
2021-10-16 19:28:04 +02:00
|
|
|
}
|
2023-05-28 03:34:18 +02:00
|
|
|
}
|
2021-10-16 19:28:04 +02:00
|
|
|
});
|
|
|
|
}
|