diff --git a/src/components/RandomMsg.jsx b/src/components/RandomMsg.jsx index 9863c39..c5d2b64 100644 --- a/src/components/RandomMsg.jsx +++ b/src/components/RandomMsg.jsx @@ -1,39 +1,51 @@ +import { useState, useEffect } from "react"; import { API_URL } from "../config"; -import { default as $ } from "jquery"; -import ReplayIcon from '@mui/icons-material/Replay'; - - -var randomMsg; - -const getRandomMsg = () => { - $.ajax({ - url: API_URL+'/randmsg', - method: "POST", - contentType: "application/json; charset=utf-8" - }).done(function (data) { - if (! data.msg) { - return; - } else { - randomMsg = data.msg.replace("
", "\n"); - $('.random-msg').html(`${randomMsg}`); - } - }); -} +import ReplayIcon from "@mui/icons-material/Replay"; export default function RandomMsg() { - getRandomMsg(); - return ( -
-
-
- -
- ); -} + const [randomMsg, setRandomMsg] = useState(""); + const getRandomMsg = async () => { + try { + const response = await fetch(`${API_URL}/randmsg`, { + method: "POST", + headers: { + "Content-Type": "application/json; charset=utf-8", + }, + }); + + const data = await response.json(); + if (data?.msg) { + const formattedMsg = data.msg.replace(//gi, "\n"); + setRandomMsg(formattedMsg); + } + } catch (err) { + console.error("Failed to fetch random message:", err); + } + }; + + useEffect(() => { + getRandomMsg(); + }, []); + + return ( +
+
+ {randomMsg && ( + + {randomMsg} + + )} +
+ +
+ ); +}