From 16cfb642db302955bfde4ef79fcb9fd66114c5e5 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Sat, 24 Aug 2024 01:59:43 -0700 Subject: ffxiv-gil-making: WIP Eureka --- src/components/FFXIVItemPrice.tsx | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'src/components') diff --git a/src/components/FFXIVItemPrice.tsx b/src/components/FFXIVItemPrice.tsx index bcf5900..d65b3ca 100644 --- a/src/components/FFXIVItemPrice.tsx +++ b/src/components/FFXIVItemPrice.tsx @@ -12,14 +12,35 @@ const FFXIVItemPrice: React.FC = ({ itemId = 5530, itemName const [averageSalePrice, setAverageSalePrice] = useState(null); const [inputQuantity, setInputQuantity] = useState(0); const [potentialGil, setPotentialGil] = useState(0); + const [dataSource, setDataSource] = useState('world'); const fetchData = (attempt: number = 1) => { fetch(`https://universalis.app/api/v2/aggregated/${selectedWorld}/${itemId}`) .then(response => response.json()) .then(data => { - const result = data.results[0]; - setDailySaleVelocity(Math.round(result.nq.dailySaleVelocity.world.quantity)); - setAverageSalePrice(Math.round(result.nq.averageSalePrice.world.price)); + let result = data.results[0]; + try { + setDailySaleVelocity(Math.round(result.nq.dailySaleVelocity.world.quantity)); + setAverageSalePrice(Math.round(result.nq.averageSalePrice.world.price)); + setDataSource('world'); + } catch (error) { + try { + setDailySaleVelocity(Math.round(result.nq.dailySaleVelocity.dc.quantity)); + setAverageSalePrice(Math.round(result.nq.averageSalePrice.dc.price)); + setDataSource('dc'); + } catch (error) { + try { + setDailySaleVelocity(Math.round(result.nq.dailySaleVelocity.region.quantity)); + setAverageSalePrice(Math.round(result.nq.averageSalePrice.region.price)); + setDataSource('region'); + } catch (error) { + console.error('Error fetching data:', error); + if (attempt < 3) { + setTimeout(() => fetchData(attempt + 1), 5000); + } + } + } + } }) .catch(error => { console.error(`Error fetching data (attempt ${attempt}):`, error); @@ -53,7 +74,9 @@ const FFXIVItemPrice: React.FC = ({ itemId = 5530, itemName return (
-

{itemName}

+ +

{itemName}

+
{itemName}
@@ -85,7 +108,8 @@ const FFXIVItemPrice: React.FC = ({ itemId = 5530, itemName
-- cgit v1.2.3