Skip to content

Commit dbd9a96

Browse files
authored
Merge pull request #4 from SamuelCarmona83/main
Add backend message loading to Home component and update environment …
2 parents c22bd3b + 5936b76 commit dbd9a96

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/front/pages/Home.jsx

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
1+
import React, { useEffect } from "react"
12
import rigoImageUrl from "../assets/img/rigo-baby.jpg";
23
import useGlobalReducer from "../hooks/useGlobalReducer.jsx";
34

45
export const Home = () => {
56

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+
},[])
725

826
return (
927
<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" />
1331
</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>
1441
</div>
1542
);
1643
};

src/front/store.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export const initialStore=()=>{
1818

1919
export default function storeReducer(store, action = {}) {
2020
switch(action.type){
21+
case 'set_hello':
22+
return {
23+
...store,
24+
hello: action.payload
25+
};
26+
2127
case 'add_task':
2228

2329
const { id, color } = action.payload

0 commit comments

Comments
 (0)