11import InputGroup from "react-bootstrap/InputGroup" ;
22import Form from "react-bootstrap/Form" ;
33import Table from "react-bootstrap/Table" ;
4- import { useEffect , useRef , useState } from "react" ;
4+ import { useRef , useState } from "react" ;
5+ import { useQuery , useMutation } from '@tanstack/react-query' ;
56
67const MISSON_COMMAND_PREFIX = "MCC_" ;
78
89function MissionCommands ( ) {
9- const [ commands , setCommands ] = useState ( "" ) ;
10- const [ commandResponse , setCommandResponse ] = useState ( "" ) ;
1110 const inputRef = useRef ( null ) ;
1211
13- useEffect ( ( ) => {
14- try {
15- const command = localStorage . getItem ( MISSON_COMMAND_PREFIX + "command" ) ;
16- const response = localStorage . getItem ( MISSON_COMMAND_PREFIX + "response" ) ;
17- if ( ! command ) return ;
18- if ( ! response ) return ;
19- setCommands ( command ) ;
20- setCommandResponse ( response ) ;
21- } catch ( error ) {
22- console . error ( error ) ;
23- }
24- } , [ ] ) ;
12+ const { data : savedData } = useQuery ( {
13+ queryKey : [ "missionCommand" ] ,
14+ queryFn : ( ) => {
15+ const command = localStorage . getItem ( MISSON_COMMAND_PREFIX + "command" ) || "" ;
16+ const response = localStorage . getItem ( MISSON_COMMAND_PREFIX + "response" ) || "" ;
17+ return { command, response } ;
18+ } ,
19+ initialData : {
20+ command : localStorage . getItem ( MISSON_COMMAND_PREFIX + "command" ) || "" ,
21+ response : localStorage . getItem ( MISSON_COMMAND_PREFIX + "response" ) || "" ,
22+ } ,
23+ } ) ;
24+
25+ const [ commands , setCommands ] = useState ( savedData . command ) ;
2526
2627 // const handleChange = (event) => {
2728 // setCommands(event.target.value);
2829 // localStorage.setItem(MISSON_COMMAND_PREFIX + "command", event.target.value);
2930 // }
30-
31- const handleCommand = ( ) => {
32- try {
33- const requestOptions = {
31+ const mutation = useMutation ( {
32+ mutationFn : async ( command : string ) => {
33+ const response = await fetch ( "http://localhost:5000/mission-control" , {
3434 method : "POST" ,
3535 headers : { "Content-Type" : "application/json" } ,
36- body : JSON . stringify ( { command : commands } ) ,
37- } ;
38- fetch ( "http://localhost:5000/mission-control" , requestOptions )
39- . then ( ( response ) => response . json ( ) )
40- . then ( ( data ) => {
41- setCommandResponse ( data . response ) ;
42- localStorage . setItem (
43- MISSON_COMMAND_PREFIX + "response" ,
44- data . response ,
45- ) ;
46- } ) ;
47- } catch ( error ) {
48- console . error ( error ) ;
49- }
36+ body : JSON . stringify ( { command } ) ,
37+ } ) ;
38+ if ( ! response . ok ) {
39+ throw new Error ( `Server error: ${ response . status } ` ) ;
40+ }
41+ return response . json ( ) ;
42+ } ,
43+ onSuccess : ( data ) => {
44+ localStorage . setItem (
45+ MISSON_COMMAND_PREFIX + "response" ,
46+ data . response
47+ ) ;
48+ } ,
49+ } ) ;
50+
51+ const handleCommand = ( ) => {
52+ mutation . mutate ( commands ) ;
5053 } ;
5154
5255 const clear = ( ) => {
@@ -56,8 +59,7 @@ function MissionCommands() {
5659 console . error ( error ) ;
5760 }
5861 setCommands ( "" ) ;
59- setCommandResponse ( "" ) ;
60- // inputRef.current.value = ''
62+ mutation . reset ( ) ;
6163 } ;
6264
6365 return (
@@ -80,7 +82,13 @@ function MissionCommands() {
8082 </ tr >
8183 </ thead >
8284 < tbody >
83- < tr > { commandResponse && < td > { commandResponse } </ td > } </ tr >
85+ < tr >
86+ < td >
87+ { mutation . status === "pending" && "Loading..." }
88+ { mutation . status === "error" && `Error: ${ ( mutation . error as Error ) . message } ` }
89+ { mutation . status === "success" && mutation . data . response }
90+ </ td >
91+ </ tr >
8492 </ tbody >
8593 </ Table >
8694 < button onClick = { clear } > Clear</ button >
0 commit comments