Skip to content

Commit 5ede340

Browse files
committed
update loadMessage function
1 parent dbd9a96 commit 5ede340

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
"editor.defaultFormatter": "esbenp.prettier-vscode",
44
"workbench.editorAssociations": {
55
"*.md": "vscode.markdown.preview.editor"
6+
},
7+
"[javascriptreact]": {
8+
"editor.defaultFormatter": "vscode.typescript-language-features"
69
}
710
}

src/front/components/BackendURL.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ export const BackendURL = () => (
1212
<li>Make sure you backend is running on port 3001.</li>
1313
<li>Open your API and copy the API host.</li>
1414
<li>Open the .env file (do not open the .env.example)</li>
15-
<li>Add a new variable BACKEND_URL=<Dark>your api host</Dark></li>
15+
<li>Add a new variable VITE_BACKEND_URL=<Dark>your api host</Dark></li>
1616
<li>Replace <Dark>your api host</Dark> with the public API URL of your flask backend sever running at port 3001</li>
1717
</ol>
18-
<img src={envFile} />
18+
<div className="w-100">
19+
<img src={envFile} className="w-100"/>
20+
</div>
1921
<p>Note: If you are publishing your website to Heroku, Render.com or any other hosting you probably need to follow other steps.</p>
2022
</div>
2123
);

src/front/pages/Home.jsx

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,43 @@ import useGlobalReducer from "../hooks/useGlobalReducer.jsx";
44

55
export const Home = () => {
66

7-
const {store, dispatch} =useGlobalReducer()
8-
7+
const { store, dispatch } = useGlobalReducer()
8+
99
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
10+
try {
11+
const backendUrl = import.meta.env.VITE_BACKEND_URL
12+
13+
if (!backendUrl) throw new Error("VITE_BACKEND_URL is not defined in .env file")
14+
15+
const response = await fetch(backendUrl + "/api/hello")
16+
const data = await response.json()
17+
18+
if (response.ok) dispatch({ type: "set_hello", payload: data.message })
19+
20+
return data
21+
22+
} catch (error) {
23+
if (error.message) throw new Error(
24+
`Could not fetch the message from the backend.
25+
Please check if the backend is running and the backend port is public.`
26+
);
27+
}
28+
2029
}
2130

22-
useEffect(()=>{
31+
useEffect(() => {
2332
loadMessage()
24-
},[])
33+
}, [])
2534

2635
return (
2736
<div className="text-center mt-5">
2837
<h1 className="display-4">Hello Rigo!!</h1>
2938
<p className="lead">
3039
<img src={rigoImageUrl} className="img-fluid rounded-circle mb-3" alt="Rigo Baby" />
3140
</p>
32-
<div className="alert alert-info fw-bold">
33-
{store.hello && store.hello.message ? (
34-
<span>{store.hello.message}</span>
41+
<div className="alert alert-info">
42+
{store.message ? (
43+
<span>{store.message}</span>
3544
) : (
3645
<span className="text-danger">
3746
Loading message from the backend (make sure your python 🐍 backend is running)...

src/front/store.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function storeReducer(store, action = {}) {
2121
case 'set_hello':
2222
return {
2323
...store,
24-
hello: action.payload
24+
message: action.payload
2525
};
2626

2727
case 'add_task':

0 commit comments

Comments
 (0)