File size: 3,675 Bytes
4edc3e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eebe76e
4edc3e5
 
 
 
eebe76e
 
 
 
 
 
 
 
 
4edc3e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// 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();
  }
};