2022-01-29 02:30:11 +05:30
|
|
|
import $ from 'jquery';
|
2023-09-19 06:20:30 +05:30
|
|
|
import {POST} from '../../modules/fetch.js';
|
2021-10-16 22:58:04 +05:30
|
|
|
|
2023-05-28 07:04:18 +05:30
|
|
|
export function initCompReactionSelector($parent) {
|
|
|
|
$parent.find(`.select-reaction .item.reaction, .comment-reaction-button`).on('click', async function (e) {
|
2021-10-16 22:58:04 +05:30
|
|
|
e.preventDefault();
|
|
|
|
|
2024-03-27 21:39:34 +05:30
|
|
|
if (this.classList.contains('disabled')) return;
|
2021-10-16 22:58:04 +05:30
|
|
|
|
2024-03-25 10:00:38 +05:30
|
|
|
const actionUrl = this.closest('[data-action-url]')?.getAttribute('data-action-url');
|
|
|
|
const reactionContent = this.getAttribute('data-reaction-content');
|
|
|
|
const hasReacted = this.closest('.ui.segment.reactions')?.querySelector(`a[data-reaction-content="${reactionContent}"]`)?.getAttribute('data-has-reacted') === 'true';
|
2023-05-28 07:04:18 +05:30
|
|
|
|
2023-09-19 06:20:30 +05:30
|
|
|
const res = await POST(`${actionUrl}/${hasReacted ? 'unreact' : 'react'}`, {
|
|
|
|
data: new URLSearchParams({content: reactionContent}),
|
2023-05-28 07:04:18 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
const data = await res.json();
|
|
|
|
if (data && (data.html || data.empty)) {
|
2024-03-16 17:52:16 +05:30
|
|
|
const $content = $(this).closest('.content');
|
|
|
|
let $react = $content.find('.segment.reactions');
|
|
|
|
if ((!data.empty || data.html === '') && $react.length > 0) {
|
|
|
|
$react.remove();
|
2021-10-16 22:58:04 +05:30
|
|
|
}
|
2023-05-28 07:04:18 +05:30
|
|
|
if (!data.empty) {
|
2024-03-16 17:52:16 +05:30
|
|
|
const $attachments = $content.find('.segment.bottom:first');
|
|
|
|
$react = $(data.html);
|
|
|
|
if ($attachments.length > 0) {
|
|
|
|
$react.insertBefore($attachments);
|
2023-05-28 07:04:18 +05:30
|
|
|
} else {
|
2024-03-16 17:52:16 +05:30
|
|
|
$react.appendTo($content);
|
2021-10-16 22:58:04 +05:30
|
|
|
}
|
2024-03-16 17:52:16 +05:30
|
|
|
$react.find('.dropdown').dropdown();
|
|
|
|
initCompReactionSelector($react);
|
2021-10-16 22:58:04 +05:30
|
|
|
}
|
2023-05-28 07:04:18 +05:30
|
|
|
}
|
2021-10-16 22:58:04 +05:30
|
|
|
});
|
|
|
|
}
|