nagasurendra commited on
Commit
5309ff5
·
verified ·
1 Parent(s): 912ae9b

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +29 -5
static/script.js CHANGED
@@ -74,22 +74,46 @@ function handleResponse(userInput) {
74
  }
75
 
76
  function displayOptions(options) {
77
- // Function to display options on the UI
78
  const optionsContainer = document.getElementById('options-container');
79
  if (!optionsContainer) {
80
  console.error('Options container not found!');
81
  return;
82
  }
83
 
84
- options.forEach(option => {
 
 
85
  const button = document.createElement('button');
86
- button.classList.add(option.class);
87
- button.textContent = option.text;
88
- button.onclick = () => handleResponse(option.text);
 
 
 
 
89
  optionsContainer.appendChild(button);
90
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  }
92
 
 
93
  document.getElementById('userInput').addEventListener('keypress', function(e) {
94
  if (e.key === 'Enter') {
95
  sendMessage();
 
74
  }
75
 
76
  function displayOptions(options) {
 
77
  const optionsContainer = document.getElementById('options-container');
78
  if (!optionsContainer) {
79
  console.error('Options container not found!');
80
  return;
81
  }
82
 
83
+ optionsContainer.innerHTML = ''; // Clear previous options if any
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
+ optionsContainer.appendChild(document.createElement('br')); // Line break after options
98
+
99
+ // Optional 'Go Back' button functionality
100
+ if (conversation.length > 1) {
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
+
117
  document.getElementById('userInput').addEventListener('keypress', function(e) {
118
  if (e.key === 'Enter') {
119
  sendMessage();