From f3c4b3b33e5ba97d9cfa679c5f933518667eefaa Mon Sep 17 00:00:00 2001 From: Giovani Rodriguez Date: Tue, 15 Jul 2025 06:12:58 +0000 Subject: [PATCH] feat: load flag with loading text --- typewriter/src/App.tsx | 18 ++++++++++++++++-- typewriter/src/api/fetchBodyText.ts | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 typewriter/src/api/fetchBodyText.ts diff --git a/typewriter/src/App.tsx b/typewriter/src/App.tsx index fe3491a..1ec4cdb 100644 --- a/typewriter/src/App.tsx +++ b/typewriter/src/App.tsx @@ -1,9 +1,23 @@ -import React from 'react'; +import { useEffect, useState } from "react"; +import { fetchBodyText } from "./api/fetchBodyText"; function App() { + const [bodyText, setBodyText] = useState(""); + const [loading, setLoading] = useState(true); + + useEffect(() => { + async function load() { + setLoading(true); + const text = await fetchBodyText(); + setBodyText(text); + setLoading(false); + } + load(); + }, []); + return (
- Hello World! + {loading ?

Loading...

:

{bodyText}

}
); } diff --git a/typewriter/src/api/fetchBodyText.ts b/typewriter/src/api/fetchBodyText.ts new file mode 100644 index 0000000..8e8ab15 --- /dev/null +++ b/typewriter/src/api/fetchBodyText.ts @@ -0,0 +1,19 @@ +const url = "https://wgg522pwivhvi5gqsn675gth3q0otdja.lambda-url.us-east-1.on.aws/6f7665"; + +export async function fetchBodyText(): Promise { + try { + const response = await fetch(url); + const html = await response.text(); + + const parser = new DOMParser(); + const doc = parser.parseFromString(html, "text/html"); + + const body = doc.body; + const text = body?.textContent?.trim(); + + return text ?? ""; + } catch (error) { + console.error("Error fetching or parsing HTML:", error); + return ""; + } +} \ No newline at end of file