From c057fefd1daf1152752556bb88d71e1c2e392796 Mon Sep 17 00:00:00 2001 From: Maciej Gierada Date: Fri, 14 Jun 2024 08:35:01 +0200 Subject: [PATCH] fix: bug page not refreshed --- ui/src/app/page.js | 24 ++++++++++++++++++++-- ui/src/components/CounterDisplay/index.jsx | 23 +-------------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/ui/src/app/page.js b/ui/src/app/page.js index f057107..cd00480 100644 --- a/ui/src/app/page.js +++ b/ui/src/app/page.js @@ -1,12 +1,32 @@ +import axios from "axios"; import CounterDisplay from "@/components/CounterDisplay"; -const Home = () => { +const fetchCounter = async () => { + try { + const rootUrl = process.env.NEXT_PUBLIC_ROOT_API_URL; + const endpoint = `${rootUrl}/counter`; + + const response = await axios.get(endpoint); + if (response.data && typeof response.data.CurrentValue === "number") { + return { currentValue: response.data.CurrentValue, error: null }; + } else { + throw new Error("Invalid response data"); + } + } catch (error) { + console.error("Error fetching the current value:", error.message); // Debug log + return { currentValue: null, error: "Failed to fetch current value" }; + } +}; + +const Home = async () => { + const { currentValue, error } = await fetchCounter(); + return (
- +
diff --git a/ui/src/components/CounterDisplay/index.jsx b/ui/src/components/CounterDisplay/index.jsx index 1ba755f..0cc3897 100644 --- a/ui/src/components/CounterDisplay/index.jsx +++ b/ui/src/components/CounterDisplay/index.jsx @@ -1,25 +1,4 @@ -import axios from "axios"; - -const fetchCurrentValue = async () => { - try { - const rootUrl = process.env.NEXT_PUBLIC_ROOT_API_URL; - const endpoint = `${rootUrl}/counter`; - - const response = await axios.get(endpoint); - if (response.data && typeof response.data.CurrentValue === "number") { - return { currentValue: response.data.CurrentValue, error: null }; - } else { - throw new Error("Invalid response data"); - } - } catch (error) { - console.error("Error fetching the current value:", error.message); // Debug log - return { currentValue: null, error: "Failed to fetch current value" }; - } -}; - -const CounterDisplay = async () => { - const { currentValue, error } = await fetchCurrentValue(); - +const CounterDisplay = ({ currentValue, error }) => { return (
{error ? (