1
+ import React , { useEffect } from "react"
1
2
import rigoImageUrl from "../assets/img/rigo-baby.jpg" ;
2
3
import useGlobalReducer from "../hooks/useGlobalReducer.jsx" ;
3
4
4
5
export const Home = ( ) => {
5
6
6
- const { store, dispatch} = useGlobalReducer ( )
7
+ const { store, dispatch} = useGlobalReducer ( )
8
+
9
+ const loadMessage = async ( ) => {
10
+ const backendUrl = import . meta. env . VITE_BACKEND_URL
11
+ if ( ! backendUrl ) throw new Error ( "VITE_BACKEND_URL is not defined in .env file" )
12
+ const response = await fetch ( backendUrl + "/api/hello" )
13
+ const data = await response . json ( )
14
+ if ( ! response . ok ) throw new Error (
15
+ `Could not fetch the message from the backend.
16
+ Please check if the backend is running and the backend port is public.`
17
+ ) ;
18
+ dispatch ( { type : "set_hello" , payload : data } )
19
+ return data
20
+ }
21
+
22
+ useEffect ( ( ) => {
23
+ loadMessage ( )
24
+ } , [ ] )
7
25
8
26
return (
9
27
< div className = "text-center mt-5" >
10
- < h1 > Hello Rigo!!</ h1 >
11
- < p >
12
- < img src = { rigoImageUrl } />
28
+ < h1 className = "display-4" > Hello Rigo!!</ h1 >
29
+ < p className = "lead" >
30
+ < img src = { rigoImageUrl } className = "img-fluid rounded-circle mb-3" alt = "Rigo Baby" />
13
31
</ p >
32
+ < div className = "alert alert-info fw-bold" >
33
+ { store . hello && store . hello . message ? (
34
+ < span > { store . hello . message } </ span >
35
+ ) : (
36
+ < span className = "text-danger" >
37
+ Loading message from the backend (make sure your python 🐍 backend is running)...
38
+ </ span >
39
+ ) }
40
+ </ div >
14
41
</ div >
15
42
) ;
16
43
} ;
0 commit comments