@@ -26,6 +26,7 @@ <h1>ChatABC</h1>
2626 < form id ="input ">
2727 < input type ="text " />
2828 < input type ="submit " />
29+ < button id ="cancel " type ="button "> Cancel</ button >
2930 < span id ="spinner "> </ span >
3031 </ form >
3132 < div id ="messages "> </ div >
@@ -35,6 +36,7 @@ <h1>ChatABC</h1>
3536 let msgContainer = document . getElementById ( "messages" ) ;
3637 let textEl = document . querySelector ( "form input[type=text]" ) ;
3738 let spinnerEl = document . getElementById ( "spinner" ) ;
39+ let cancelEl = document . getElementById ( "cancel" ) ;
3840
3941 async function fetchChat ( chat ) {
4042 spinnerEl . classList . add ( "active" ) ;
@@ -46,7 +48,13 @@ <h1>ChatABC</h1>
4648 body : JSON . stringify ( chat )
4749 } ) ;
4850 if ( ! response . ok ) throw new Error ( response . statusText ) ;
49- return await response . json ( ) ;
51+ let json = await response . json ( ) ;
52+ if ( json . type === "Success" ) {
53+ return json ;
54+ } else {
55+ alert ( "Cancelled" ) ;
56+ throw new Error ( "Cancelled" ) ;
57+ }
5058 } finally {
5159 spinnerEl . classList . remove ( "active" ) ;
5260 textEl . removeAttribute ( "disabled" ) ;
@@ -65,11 +73,13 @@ <h1>ChatABC</h1>
6573 event . preventDefault ( ) ;
6674 chat . messages . push ( textEl . value ) ;
6775 textEl . value = "" ;
76+ updateChat ( chat ) ;
6877 fetchChat ( chat ) . then ( updateChat ) ;
6978 }
7079
7180 function main ( ) {
7281 document . getElementById ( "input" ) . addEventListener ( "submit" , onSubmit ) ;
82+ cancelEl . addEventListener ( "click" , ( ) => fetch ( "/cancel" , { method : "post" } ) ) ;
7383 }
7484
7585 main ( ) ;
0 commit comments