2024-07-07 17:32:30 +02:00
|
|
|
import {POST} from '../../modules/fetch.ts';
|
2024-12-21 19:59:25 +01:00
|
|
|
import type {DOMEvent} from '../../utils/dom.ts';
|
2025-03-01 10:02:10 +08:00
|
|
|
import {registerGlobalEventFunc} from '../../modules/observer.ts';
|
2021-10-17 01:28:04 +08:00
|
|
|
|
2025-03-01 10:02:10 +08:00
|
|
|
export function initCompReactionSelector() {
|
|
|
|
registerGlobalEventFunc('click', 'onCommentReactionButtonClick', async (target: HTMLElement, e: DOMEvent<MouseEvent>) => {
|
|
|
|
// there are 2 places for the "reaction" buttons, one is the top-right reaction menu, one is the bottom of the comment
|
|
|
|
e.preventDefault();
|
2021-10-17 01:28:04 +08:00
|
|
|
|
2025-03-01 10:02:10 +08:00
|
|
|
if (target.classList.contains('disabled')) return;
|
2021-10-17 01:28:04 +08:00
|
|
|
|
2025-03-01 10:02:10 +08:00
|
|
|
const actionUrl = target.closest('[data-action-url]').getAttribute('data-action-url');
|
|
|
|
const reactionContent = target.getAttribute('data-reaction-content');
|
2023-05-28 03:34:18 +02:00
|
|
|
|
2025-03-01 10:02:10 +08:00
|
|
|
const commentContainer = target.closest('.comment-container');
|
2023-05-28 03:34:18 +02:00
|
|
|
|
2025-03-01 10:02:10 +08:00
|
|
|
const bottomReactions = commentContainer.querySelector('.bottom-reactions'); // may not exist if there is no reaction
|
|
|
|
const bottomReactionBtn = bottomReactions?.querySelector(`a[data-reaction-content="${CSS.escape(reactionContent)}"]`);
|
|
|
|
const hasReacted = bottomReactionBtn?.getAttribute('data-has-reacted') === 'true';
|
2024-04-14 12:44:11 +02:00
|
|
|
|
2025-03-01 10:02:10 +08:00
|
|
|
const res = await POST(`${actionUrl}/${hasReacted ? 'unreact' : 'react'}`, {
|
|
|
|
data: new URLSearchParams({content: reactionContent}),
|
2024-04-14 12:44:11 +02:00
|
|
|
});
|
2025-03-01 10:02:10 +08:00
|
|
|
|
|
|
|
const data = await res.json();
|
|
|
|
bottomReactions?.remove();
|
|
|
|
if (data.html) {
|
|
|
|
commentContainer.insertAdjacentHTML('beforeend', data.html);
|
|
|
|
}
|
|
|
|
});
|
2021-10-17 01:28:04 +08:00
|
|
|
}
|