// components/comment-component.js - Comments functionality import { StateManager } from '../services/state-manager.js'; import { ApiService } from '../services/api-service.js'; import { TranslationService } from '../services/translation-service.js'; export const CommentComponent = { elements: { leaveCommentText: null, commentOverlay: null, closeCommentBtn: null, cancelCommentBtn: null, sendCommentBtn: null, commentInput: null, statusComment: null }, /** * Initialize the comment component */ init() { this.elements.leaveCommentText = document.getElementById('leave-comment'); this.elements.commentOverlay = document.getElementById('comment-overlay'); this.elements.closeCommentBtn = document.getElementById('closeCommentBtn'); this.elements.cancelCommentBtn = document.getElementById('cancelCommentBtn'); this.elements.sendCommentBtn = document.getElementById('sendCommentBtn'); this.elements.commentInput = document.getElementById('commentInput'); this.elements.statusComment = document.getElementById('commentStatus'); this.attachOutsideClickListener(); this.attachEventListeners(); this.initializeStatus(); }, attachOutsideClickListener() { this.elements.commentOverlay.addEventListener('click', (e) => { // Check if click is on the overlay itself (not its children) if (e.target === this.elements.commentOverlay) { this.closeOverlay(); } }); }, /** * Attach event listeners */ attachEventListeners() { this.elements.leaveCommentText.addEventListener('click', (e) => this.openOverlay(e)); this.elements.closeCommentBtn.addEventListener('click', () => this.closeOverlay()); this.elements.cancelCommentBtn.addEventListener('click', () => this.closeOverlay()); this.elements.sendCommentBtn.addEventListener('click', () => this.sendComment()); // Enter to send, Shift+Enter = newline this.elements.commentInput.addEventListener('keydown', (e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); this.sendComment(); } }); }, /** * Initialize status display */ initializeStatus() { this.elements.statusComment.dataset.i18n = "ready"; this.elements.statusComment.className = 'status-ok'; TranslationService.applyTranslation(); }, /** * Open the comment overlay */ openOverlay(e) { e.preventDefault(); this.elements.commentOverlay.style.display = ''; }, /** * Close the comment overlay */ closeOverlay() { this.elements.commentOverlay.style.display = 'none'; }, /** * Send a comment to the server */ async sendComment() { const comment = this.elements.commentInput.value; if (!comment) return; this.setStatus('sending', 'info'); const result = await ApiService.sendComment(comment); if (!result.success) { if (result.status === 400) { this.setStatus('empty_message_error', 'error'); } else { this.setStatus('server_error', 'error'); } return; } this.elements.commentInput.value = ''; this.setStatus('comment_sent', 'ok'); }, /** * Set status message * @param {string} messageKey - Translation key for the message * @param {string} type - Status type ('ok', 'info', 'error') */ setStatus(messageKey, type) { this.elements.statusComment.dataset.i18n = messageKey; this.elements.statusComment.className = `status-${type}`; TranslationService.applyTranslation(); } };