templates/index.html.twig line 1

  1. {# templates/chat/index.html.twig #}
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5.   <meta name="viewport" content="width=device-width, initial-scale=1">
  6.   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  7.   <title>Poll</title>
  8.   <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  9.   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  10.   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
  11.   <script>
  12.     var sessionId = 1;
  13.     $(document).ready(function() {
  14.       var lastMessageId = -1;
  15.       var pollSelector = document.getElementById('poll-selector');
  16.       var getSessionUrl = '{{ path('api_get_session',{'pollId': 1}) }}';
  17.       var getQuestionUrl = '{{ path('api_get_question',{'pollId': 1}) }}';
  18.       var postMessageUrl = '{{ path('api_post_message',{'pollId': 1}) }}';
  19.       var initialPollId = 1;
  20.       getSessionUrl = getSessionUrl.replace('initialPollId', initialPollId);
  21.       pollSelector.addEventListener('change', function() {
  22.         var selectedPollId = pollSelector.value;
  23.         getSessionUrl = getSessionUrl.replace(initialPollId, selectedPollId);
  24.         getQuestionUrl = getQuestionUrl.replace(initialPollId, selectedPollId);
  25.         postMessageUrl = postMessageUrl.replace(initialPollId, selectedPollId);
  26.         initialPollId = selectedPollId;
  27.         getSession();
  28.         loadFirstQuestion();
  29.         $('#chat-messages').html('');
  30.       });
  31.       function getSession() {
  32.         $.ajax({
  33.           url: getSessionUrl,
  34.           type: 'GET',
  35.           dataType: 'json',
  36.           success: function(data) {
  37.             sessionId = data.session_id;
  38.             loadFirstQuestion();
  39.             // Load messages initially
  40.             loadMessages();
  41.           },
  42.         });
  43.       }
  44.       function loadFirstQuestion() {
  45.         $.ajax({
  46.           url: getQuestionUrl,
  47.           type: 'GET',
  48.           dataType: 'json',
  49.           headers: {
  50.             'x-session-id': sessionId,
  51.           },
  52.           success: function(data) {
  53.             console.log(data);
  54.           },
  55.         });
  56.       }
  57.       // Function to load messages via AJAX
  58.       function loadMessages() {
  59.         $.ajax({
  60.           url: '{{ path('chat_messages')}}',
  61.           type: 'GET',
  62.           dataType: 'json',
  63.           headers: {
  64.             'x-session-id': sessionId
  65.           },
  66.           beforeSend: function(xhr) {
  67.             // Display loading icon3
  68.             $('#loading-icon').show();
  69.           },
  70.           success: function(data) {
  71.             // Process the JSON response
  72.             var chatMessages = '';
  73.             var isNewMessage = false;
  74.             for (var i = 0; i < data.length; i++) {
  75.               var message = data[i];
  76.               var createdAt = new Date(message.createdAt).toLocaleTimeString();
  77.               if (message.id > lastMessageId) {
  78.                 css = (message.user == 'user') ? 'userMessage' : 'assitantMessage';
  79.                 chatMessages += '<div class="message '+css+' new">';
  80.                 chatMessages += '<p class="content">' + message.content + '</p>';
  81.                 chatMessages += '<span class="timestamp">' + createdAt + '</span>';
  82.                 chatMessages += '</div>';
  83.                 isNewMessage = true;
  84.               }
  85.             }
  86.             if (isNewMessage) {
  87.               $('#chat-messages').append(chatMessages);
  88.               // Scroll to the bottom of the chat window if there is a new message
  89.               var chatWindow = $('#chat-window');
  90.               chatWindow.scrollTop(chatWindow[0].scrollHeight);
  91.               // Update the last message ID
  92.               lastMessageId = data[data.length - 1].id;
  93.             }
  94.             $('#loading-icon').hide();
  95.           }
  96.         });
  97.       }
  98.       getSession();
  99.       // Function to handle form submission
  100.       $('#message-form').submit(function(event) {
  101.         event.preventDefault();
  102.         var formData = $(this).serialize();
  103.         $.ajax({
  104.           url: postMessageUrl,
  105.           type: 'POST',
  106.           data: formData,
  107.           headers: {
  108.             'x-session-id': sessionId,
  109.           },
  110.           beforeSend: function() {
  111.             // Display loading icon
  112.             $('#loading-icon').show();
  113.           },
  114.           success: function() {
  115.             $('#message-content').val('');
  116.             loadMessages();
  117.           },
  118.           complete: function() {
  119.             // Hide loading icon
  120.             $('#loading-icon').hide();
  121.           }
  122.         });
  123.       });
  124.       // Function to periodically load messages
  125.       setInterval(loadMessages, 3000);
  126.     });
  127.   </script>
  128.   <style>
  129.     body, html {
  130.       height: 90%;
  131.     }
  132.     .message {
  133.       background-color: #f1f1f1;
  134.       padding: 10px;
  135.       margin-bottom: 10px;
  136.       border-radius: 5px;
  137.     }
  138.     .message.userMessage {
  139.       background-color: #6388b1;
  140.       padding: 10px;
  141.       margin-bottom: 10px;
  142.       border-radius: 5px;
  143.     }
  144.     .content {
  145.       font-weight: bold;
  146.       margin-bottom: 5px;
  147.     }
  148.     .timestamp {
  149.       font-size: 0.8rem;
  150.       color: #999;
  151.     }
  152.     .loading {
  153.     height: 32px;
  154.     }
  155.     .message-form {
  156.       margin-bottom: 30px;
  157.     }
  158.   </style>
  159. </head>
  160. <body>
  161.   <div class="container">
  162.     <div style="display: flex; justify-content: space-between;">
  163.       <h1>Poll Chat <span style="font-size: 10px;"> v0.2</span></h1>
  164.       <select id="poll-selector" style="padding: 5px; margin: 5px; margin-right: 0px; width: 200px; border-radius: 5px; border: 1px solid #ccc;">
  165.         {% for poll in polls %}
  166.           <option value="{{ poll.id }}">{{ poll.title }}</option>
  167.         {% endfor %}
  168.       </select>
  169.     </div>
  170.     <div id="chat-window" style="height: 80vh; overflow-y: scroll;">
  171.       <div id="chat-messages"></div>
  172.     </div>
  173.     <div class="loading">
  174.       <div id="loading-icon" style="display: none;">
  175.         <img src="images/Loading_icon.gif" alt="Loading" width="32" height="32">
  176.       </div>
  177.     </div>
  178.   </div>
  179.   <form autocomplete="off" id="message-form" method="post" class="fixed-bottom bg-light p-3 message-form">
  180.     <div class="container">
  181.       <div class="form-row">
  182.         <div class="col">
  183.           <input autocomplete="off" type="text" name="content" id="message-content" class="form-control" placeholder="Type your message" required>
  184.         </div>
  185.         <div class="col-auto">
  186.           <button type="submit" class="btn btn-primary">Send</button>
  187.         </div>
  188.       </div>
  189.     </div>
  190.   </form>
  191. </body>
  192. </html>