Skip to content

Add backend message loading to Home component and update environment … #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ FLASK_DEBUG=1
DEBUG=TRUE

# Front-End Variables
BASENAME=/
#BACKEND_URL=
VITE_BASENAME=/
#VITE_BACKEND_URL=
35 changes: 31 additions & 4 deletions src/front/pages/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
import React, { useEffect } from "react"
import rigoImageUrl from "../assets/img/rigo-baby.jpg";
import useGlobalReducer from "../hooks/useGlobalReducer.jsx";

export const Home = () => {

const {store, dispatch} =useGlobalReducer()
const {store, dispatch} =useGlobalReducer()

const loadMessage = async () => {
const backendUrl = import.meta.env.VITE_BACKEND_URL
if (!backendUrl) throw new Error("VITE_BACKEND_URL is not defined in .env file")
const response = await fetch( backendUrl + "/api/hello")
const data = await response.json()
if (!response.ok) throw new Error(
`Could not fetch the message from the backend.
Please check if the backend is running and the backend port is public.`
);
dispatch({type: "set_hello", payload: data})
return data
}

useEffect(()=>{
loadMessage()
},[])

return (
<div className="text-center mt-5">
<h1>Hello Rigo!!</h1>
<p>
<img src={rigoImageUrl} />
<h1 className="display-4">Hello Rigo!!</h1>
<p className="lead">
<img src={rigoImageUrl} className="img-fluid rounded-circle mb-3" alt="Rigo Baby" />
</p>
<div className="alert alert-info fw-bold">
{store.hello && store.hello.message ? (
<span>{store.hello.message}</span>
) : (
<span className="text-danger">
Loading message from the backend (make sure your python 🐍 backend is running)...
</span>
)}
</div>
</div>
);
};
6 changes: 6 additions & 0 deletions src/front/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export const initialStore=()=>{

export default function storeReducer(store, action = {}) {
switch(action.type){
case 'set_hello':
return {
...store,
hello: action.payload
};

case 'add_task':

const { id, color } = action.payload
Expand Down