hamza82 commited on
Commit
f431280
·
verified ·
1 Parent(s): ac5daf9

Update static/index.html

Browse files
Files changed (1) hide show
  1. static/index.html +49 -34
static/index.html CHANGED
@@ -2,53 +2,68 @@
2
  <html lang="en">
3
  <head>
4
  <meta charset="UTF-8">
5
- <title>Chat with FastAPI</title>
6
  <script>
7
- // Function to handle the sending of the message when the enter key is pressed
8
- async function sendMessage(event) {
9
- // Check if the enter key was pressed
10
- if (event.key === "Enter") {
11
- const inputElement = document.getElementById('messageInput');
12
- const message = inputElement.value;
13
- const messagesContainer = document.getElementById('messages');
 
 
14
 
15
- // Prevent sending empty messages
16
- if (!message.trim()) return;
 
 
17
 
18
- // Add the user's message to the messages container
19
- const userMessageElement = document.createElement('p');
20
- userMessageElement.textContent = `You: ${message}`;
21
- messagesContainer.appendChild(userMessageElement);
22
 
23
- // Clear the input after sending
24
- inputElement.value = '';
 
 
 
25
 
26
- try {
27
- // Assuming /custom-auth will handle the message in some way
28
- const response = await fetch('/custom-auth', {
29
- method: 'GET', // Since it's a GET request, consider how to send data if needed
30
- });
31
- const data = await response.json();
32
 
33
- // Display the response - adjust according to the actual response structure
34
- const responseMessageElement = document.createElement('p');
35
- responseMessageElement.textContent = `Server: ${data.token}`; // Assuming the token is the response
36
- messagesContainer.appendChild(responseMessageElement);
37
- } catch (error) {
38
- console.error('Error:', error);
39
- }
40
- }
41
  }
 
 
 
 
 
 
 
 
 
42
  </script>
 
 
 
 
43
  </head>
44
- <body>
45
- <h1>Chat</h1>
46
- <div id="messages" style="margin-bottom: 20px; border: 1px solid #ccc; padding: 10px; height: 300px; overflow-y: auto;"></div>
47
- <input type="text" id="messageInput" placeholder="Type your message here and press Enter" onkeydown="sendMessage(event)">
 
48
  </body>
49
  </html>
50
 
51
 
 
52
  <!-- ################################################################################################## -->
53
  <!-- <!DOCTYPE html>
54
  <html lang="en">
 
2
  <html lang="en">
3
  <head>
4
  <meta charset="UTF-8">
5
+ <title>Chat Simulation</title>
6
  <script>
7
+ async function fetchAuthToken() {
8
+ try {
9
+ const response = await fetch('/custom-auth');
10
+ const data = await response.json();
11
+ document.getElementById('authToken').textContent = `Auth Token: ${data.token}`;
12
+ } catch (error) {
13
+ console.error('Error:', error);
14
+ }
15
+ }
16
 
17
+ async function sendMessage() {
18
+ const inputElement = document.getElementById('messageInput');
19
+ const message = inputElement.value;
20
+ if (!message.trim()) return; // Ignore empty messages
21
 
22
+ // Display the user's message
23
+ displayMessage(`You: ${message}`, 'user');
 
 
24
 
25
+ // Simulate sending the message and receiving a response
26
+ // Here, you'd replace this with a call to your actual message processing endpoint
27
+ // Since the provided code does not specify an endpoint for sending messages, this is a placeholder
28
+ const simulatedResponse = "This is a simulated response based on your message.";
29
+ displayMessage(`Server: ${simulatedResponse}`, 'server');
30
 
31
+ // Clear the input field
32
+ inputElement.value = '';
33
+ }
 
 
 
34
 
35
+ function displayMessage(text, sender) {
36
+ const messagesContainer = document.getElementById('messages');
37
+ const messageElement = document.createElement('p');
38
+ messageElement.textContent = text;
39
+ messageElement.className = sender; // Apply different styling based on the sender
40
+ messagesContainer.appendChild(messageElement);
 
 
41
  }
42
+
43
+ // Event listener for the Enter key in the message input
44
+ document.addEventListener('DOMContentLoaded', function() {
45
+ document.getElementById('messageInput').addEventListener('keydown', function(event) {
46
+ if (event.key === 'Enter') {
47
+ sendMessage();
48
+ }
49
+ });
50
+ });
51
  </script>
52
+ <style>
53
+ .user { color: blue; }
54
+ .server { color: green; }
55
+ </style>
56
  </head>
57
+ <body onload="fetchAuthToken()">
58
+ <h1>Simple Chat Simulation</h1>
59
+ <div id="authToken"></div>
60
+ <div id="messages" style="height: 200px; overflow-y: auto; border: 1px solid #ccc; padding: 10px; margin-bottom: 10px;"></div>
61
+ <input type="text" id="messageInput" placeholder="Type your message here and press Enter">
62
  </body>
63
  </html>
64
 
65
 
66
+
67
  <!-- ################################################################################################## -->
68
  <!-- <!DOCTYPE html>
69
  <html lang="en">