From 8ad03ac14da1cf999cf1074f7d9a7b8f3fb36a17 Mon Sep 17 00:00:00 2001 From: macbook Date: Tue, 17 Sep 2024 10:13:10 +0100 Subject: [PATCH] handled optional render of actual donations from api if present --- .../DonationStats/DonationStats.tsx | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/pages/Projects/components/DonationStats/DonationStats.tsx b/src/pages/Projects/components/DonationStats/DonationStats.tsx index 39e7d0cf..b88d0632 100644 --- a/src/pages/Projects/components/DonationStats/DonationStats.tsx +++ b/src/pages/Projects/components/DonationStats/DonationStats.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from "alem"; +import { asyncFetch, useEffect, useState } from "alem"; import DonateSDK from "@app/SDK/donate"; import yoctosToUsdWithFallback from "@app/utils/yoctosToUsdWithFallback"; import { Stats, StatsSubTitle, StatsTitle } from "./styles"; @@ -10,6 +10,33 @@ const DonationStats = () => { const [donations, setDonations] = useState(null); const data = DonateSDK.getConfig(); + + const getDonations = () => { + asyncFetch("https://dev.potlock.io/api/v1/stats", { + method: "GET", + headers: { + "Access-Control-Allow-Origin": "*", + "Content-Type": "application/json", + "Allow-Origin": "*", + }, + }) + .then((statesResponse) => { + console.log("States response:", statesResponse); + if (statesResponse.ok) { + return statesResponse.json().then((states) => { + const lastDonationAmount = states.total_payouts_usd; + + setDonations(states?.total_donors_count); + setDonated(yoctosToUsdWithFallback(lastDonationAmount, true)); + }); + } + // throw new Error("Failed to fetch states"); + }) + .catch((error) => { + console.error("Error fetching states:", error); + }); + }; + useEffect(() => { if (!donated) { const lastDonationAmount = data.net_donations_amount @@ -20,6 +47,7 @@ const DonationStats = () => { setDonated(lastDonationAmount); setDonations(totalDonations); } + getDonations(); }, [data, donated]); // const { donated, donations } = useDonationsInfo();