22function sendMessage ( ) {
33 const userInput = document . getElementById ( 'user-input' ) . value . trim ( ) ;
44 const chatBox = document . getElementById ( 'chat-box' ) ;
5+ const loadingIndicator = document . getElementById ( 'loading-indicator' ) ;
56
67 if ( userInput === "" ) {
78 alert ( "Please enter a message." ) ;
@@ -11,6 +12,10 @@ function sendMessage() {
1112 // Display user message in chat box
1213 chatBox . innerHTML += `<div>User: ${ userInput } </div>` ;
1314 document . getElementById ( 'user-input' ) . value = '' ; // Clear input field
15+ chatBox . scrollTop = chatBox . scrollHeight ; // Scroll to the bottom
16+
17+ // Show loading indicator
18+ loadingIndicator . style . display = 'block' ;
1419
1520 // Send the message to the server
1621 fetch ( '/chat' , {
@@ -29,11 +34,14 @@ function sendMessage() {
2934 . then ( data => {
3035 // Display AI response in chat box
3136 chatBox . innerHTML += `<div>AI: ${ data . response } </div>` ;
32- chatBox . scrollTop = chatBox . scrollHeight ; // Scroll to the bottom
3337 } )
3438 . catch ( error => {
3539 console . error ( 'Error:' , error ) ;
36- chatBox . innerHTML += `<div>AI: Sorry, there was an error processing your request.</div>` ;
40+ chatBox . innerHTML += `<div>AI: Sorry, there was an error processing your request. Please try again later.</div>` ;
41+ } )
42+ . finally ( ( ) => {
43+ // Hide loading indicator
44+ loadingIndicator . style . display = 'none' ;
3745 chatBox . scrollTop = chatBox . scrollHeight ; // Scroll to the bottom
3846 } ) ;
3947}
@@ -42,12 +50,16 @@ function sendMessage() {
4250function runSimulation ( ) {
4351 const userDecision = document . getElementById ( 'user-decision' ) . value . trim ( ) ;
4452 const simulationResult = document . getElementById ( 'simulation-result' ) ;
53+ const loadingIndicator = document . getElementById ( 'loading-indicator-sim' ) ;
4554
4655 if ( userDecision === "" ) {
4756 alert ( "Please enter a decision." ) ;
4857 return ;
4958 }
5059
60+ // Show loading indicator
61+ loadingIndicator . style . display = 'block' ;
62+
5163 // Send the decision to the server for simulation
5264 fetch ( '/simulate' , {
5365 method : 'POST' ,
@@ -69,6 +81,10 @@ function runSimulation() {
6981 } )
7082 . catch ( error => {
7183 console . error ( 'Error:' , error ) ;
72- simulationResult . innerHTML = "Sorry, there was an error processing your simulation." ;
84+ simulationResult . innerHTML = "Sorry, there was an error processing your simulation. Please try again later." ;
85+ } )
86+ . finally ( ( ) => {
87+ // Hide loading indicator
88+ loadingIndicator . style . display = 'none' ;
7389 } ) ;
74- }
90+ }
0 commit comments