Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +13 -23
static/script.js
CHANGED
@@ -74,43 +74,33 @@ function handleResponse(userInput) {
|
|
74 |
}
|
75 |
|
76 |
function displayOptions(options) {
|
77 |
-
const
|
78 |
-
if (!
|
79 |
-
console.error('
|
80 |
return;
|
81 |
}
|
82 |
|
83 |
-
|
84 |
-
|
85 |
options.forEach(opt => {
|
|
|
|
|
86 |
const button = document.createElement('button');
|
87 |
button.textContent = opt.text;
|
88 |
button.className = `option-button ${opt.class}`;
|
|
|
|
|
89 |
button.onclick = () => {
|
90 |
addMessage('user', opt.text);
|
91 |
conversation.push({ role: 'user', message: opt.text });
|
92 |
setTimeout(() => handleResponse(opt.text), 500);
|
93 |
};
|
94 |
-
optionsContainer.appendChild(button);
|
95 |
-
});
|
96 |
|
97 |
-
|
|
|
|
|
98 |
|
99 |
-
//
|
100 |
-
|
101 |
-
const backButton = document.createElement('button');
|
102 |
-
backButton.textContent = 'Go Back';
|
103 |
-
backButton.className = 'option-button';
|
104 |
-
backButton.onclick = () => {
|
105 |
-
if (conversation.length > 1) {
|
106 |
-
conversation.pop(); // Remove last message
|
107 |
-
chatMessages.innerHTML = ''; // Clear current messages
|
108 |
-
conversation.forEach(msg => addMessage(msg.role, msg.message)); // Re-render
|
109 |
-
setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
|
110 |
-
}
|
111 |
-
};
|
112 |
-
optionsContainer.appendChild(backButton);
|
113 |
-
}
|
114 |
}
|
115 |
|
116 |
|
|
|
74 |
}
|
75 |
|
76 |
function displayOptions(options) {
|
77 |
+
const chatMessages = document.getElementById('chatMessages');
|
78 |
+
if (!chatMessages) {
|
79 |
+
console.error('Chat messages container not found for options!');
|
80 |
return;
|
81 |
}
|
82 |
|
83 |
+
// Display each option as a chat message
|
|
|
84 |
options.forEach(opt => {
|
85 |
+
const messageDiv = document.createElement('div');
|
86 |
+
messageDiv.className = 'bot-message'; // This ensures the options look like bot messages
|
87 |
const button = document.createElement('button');
|
88 |
button.textContent = opt.text;
|
89 |
button.className = `option-button ${opt.class}`;
|
90 |
+
|
91 |
+
// When an option is clicked, treat it as a user response and trigger the appropriate handler
|
92 |
button.onclick = () => {
|
93 |
addMessage('user', opt.text);
|
94 |
conversation.push({ role: 'user', message: opt.text });
|
95 |
setTimeout(() => handleResponse(opt.text), 500);
|
96 |
};
|
|
|
|
|
97 |
|
98 |
+
messageDiv.appendChild(button);
|
99 |
+
chatMessages.appendChild(messageDiv); // Append the button inside a bot message container
|
100 |
+
});
|
101 |
|
102 |
+
// Scroll to the bottom of the chat container
|
103 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
}
|
105 |
|
106 |
|