@@ -20,11 +20,40 @@ function App() {
20
20
const [ scale , setScale ] = useState (
21
21
Math . min ( window . innerWidth / 1400 , window . innerHeight / 900 )
22
22
) ;
23
+ const [ counterValue , setCounterValue ] = useState ( 0 ) ;
23
24
24
25
useEffect ( ( ) => {
26
+ fetchCounterValue ( ) ;
25
27
egg ( ) ;
26
28
} , [ ] ) ;
27
29
30
+ const fetchCounterValue = async ( ) => {
31
+ try {
32
+ const response = await fetch ( "/counter" ) ;
33
+ if ( ! response . ok ) {
34
+ throw new Error ( "Failed to fetch counter value" ) ;
35
+ }
36
+ const data = await response . json ( ) ;
37
+ setCounterValue ( data . value ) ;
38
+ } catch ( error ) {
39
+ console . error ( "Failed to fetch counter value:" , error ) ;
40
+ }
41
+ } ;
42
+
43
+ const increaseCounter = async ( amount ) => {
44
+ try {
45
+ const response = await fetch ( `/counter?amount=${ amount } ` , {
46
+ method : "POST" ,
47
+ } ) ;
48
+ if ( ! response . ok ) {
49
+ throw new Error ( "Failed to increase counter value" ) ;
50
+ }
51
+ setCounterValue ( ( prevValue ) => prevValue + amount ) ;
52
+ } catch ( error ) {
53
+ console . error ( "Failed to increase counter value:" , error ) ;
54
+ }
55
+ } ;
56
+
28
57
const handleResize = ( ) => {
29
58
setScale ( Math . min ( window . innerWidth / 1400 , window . innerHeight / 900 ) ) ;
30
59
} ;
@@ -46,6 +75,25 @@ function App() {
46
75
47
76
return (
48
77
< Provider store = { store } >
78
+ < div
79
+ style = { {
80
+ margin : "0" ,
81
+ fontSize : "20px" ,
82
+ position : "absolute" ,
83
+ left : "calc(100vw/2 - 150px)" ,
84
+ top : "calc(100vh/2 - 100px)" ,
85
+ display : "flex" ,
86
+ justifyContent : "space-around" ,
87
+ alignItems : "center" ,
88
+ height : "70px" ,
89
+ width : "300px" ,
90
+ backgroundColor : "#f0f0f0" ,
91
+ borderRadius : "10px" ,
92
+ } }
93
+ >
94
+ < h1 style = { { position : "static" } } > Counter Value: { counterValue } </ h1 >
95
+ < button onClick = { ( ) => increaseCounter ( 1 ) } > Increase by 1</ button >
96
+ </ div >
49
97
< div className = "scale-wrapper" style = { { transform : `scale(${ scale } )` } } >
50
98
< Draggable onDrag = { ( e , data ) => trackPos ( data ) } >
51
99
< div className = "form no-drag" >
0 commit comments