@@ -6,6 +6,7 @@ export default function App() {
6
6
const [ suggestedPrompts , setSuggestedPrompts ] = useState ( [ ] )
7
7
const [ isDisabled , setIsDisabled ] = useState ( false ) // for the input field and the button
8
8
const [ query , setQuery ] = useState ( "" )
9
+ const [ triggerSubmission , setTriggerSubmission ] = useState ( false )
9
10
10
11
useEffect ( ( ) => {
11
12
// get rid of conversation history and tokens used on page refresh
@@ -19,10 +20,16 @@ export default function App() {
19
20
. then ( ( data ) => setSuggestedPrompts ( data ) )
20
21
} , [ ] )
21
22
23
+ useEffect ( ( ) => {
24
+ if ( triggerSubmission ) {
25
+ document . getElementById ( "submit-input" ) . click ( ) ;
26
+ setTriggerSubmission ( false ) ;
27
+ }
28
+ } , [ triggerSubmission ] )
29
+
22
30
const submitForm = async ( e ) => {
23
31
e . preventDefault ( )
24
32
25
- if ( query . trim ( ) === '' ) return ;
26
33
setSuggestedPrompts ( [ ] ) ;
27
34
28
35
const chatMessages = document . getElementById ( "chat-messages" )
@@ -43,7 +50,6 @@ export default function App() {
43
50
loader . className = "loader"
44
51
chatMessages . append ( loader )
45
52
46
- setQuery ( "" )
47
53
setIsDisabled ( true )
48
54
49
55
let sessionMessageHistory = sessionStorage . getItem ( "messageHistory" ) ;
@@ -67,6 +73,7 @@ export default function App() {
67
73
} )
68
74
. then ( ( response ) => response . json ( ) )
69
75
. then ( ( data ) => {
76
+ setQuery ( "" ) ;
70
77
const { response : botMessage , usedTokens, updatedHistory} = data ;
71
78
72
79
// update client side with number of used tokens(included tokens used for the last user query and bot response)
@@ -105,12 +112,14 @@ export default function App() {
105
112
}
106
113
107
114
return (
108
- < >
109
- < div className = "container" >
115
+ < div className = "container" >
110
116
< div className = "chat-container" >
111
117
< h1 className = "text-center" > BabbleBeaver React</ h1 >
112
118
113
- < div id = "suggested-prompts" onClick = { ( e ) => setQuery ( e . target . textContent ) } >
119
+ < div id = "suggested-prompts" onClick = { ( e ) => {
120
+ setQuery ( e . target . textContent ) ;
121
+ setTriggerSubmission ( true ) ;
122
+ } } >
114
123
{ suggestedPrompts . map ( ( suggestedPrompt , index ) => {
115
124
return (
116
125
< button className = "suggested-prompt-btn" key = { index } > { suggestedPrompt } </ button >
@@ -119,14 +128,20 @@ export default function App() {
119
128
</ div >
120
129
< div id = "chat-messages" > </ div >
121
130
122
- < form id = "chat-form" className = "mt-4" onSubmit = { ( e ) => submitForm ( e ) } >
131
+ < form id = "chat-form" className = "mt-4" onSubmit = { ( e ) => submitForm ( e ) } >
123
132
< div className = "form-group" >
124
133
< input type = "text" id = "user-input" className = "form-control" value = { query } placeholder = "Type your message..." onChange = { ( e ) => setQuery ( e . target . value ) } disabled = { isDisabled } />
125
134
</ div >
126
- < button id = "submit-input" type = "submit" className = "btn btn-primary btn-block" disabled = { isDisabled } > Send</ button >
135
+ < button
136
+ id = "submit-input"
137
+ type = "submit"
138
+ className = "btn btn-primary btn-block"
139
+ disabled = { query . trim ( ) === "" || isDisabled }
140
+ >
141
+ Send
142
+ </ button >
127
143
</ form >
128
144
</ div >
129
145
</ div >
130
- </ >
131
146
)
132
147
}
0 commit comments