From 6f8f8ac03db359ac9a1ed000975331cc95023103 Mon Sep 17 00:00:00 2001 From: Syco21 Date: Fri, 27 Mar 2026 20:54:28 +0100 Subject: [PATCH] api call cleanup --- src/components/PrintingRow/PrintingRow.jsx | 24 ++++------------------ src/hooks/useCardData.js | 0 src/services/api.jsx | 18 +++++++++++++++- src/store/Provider.jsx | 0 src/utils/formatter.js | 0 5 files changed, 21 insertions(+), 21 deletions(-) delete mode 100644 src/hooks/useCardData.js delete mode 100644 src/store/Provider.jsx delete mode 100644 src/utils/formatter.js diff --git a/src/components/PrintingRow/PrintingRow.jsx b/src/components/PrintingRow/PrintingRow.jsx index 0a4fa74..c425bdc 100644 --- a/src/components/PrintingRow/PrintingRow.jsx +++ b/src/components/PrintingRow/PrintingRow.jsx @@ -1,11 +1,11 @@ import React, { useContext, useState } from 'react'; import { CardContext } from '../../store/CardContext'; +import { updateCardAmount } from '../../services/api'; function PrintingRow({ card_id, printing }) { const { ownedAmounts, updateAmount } = useContext(CardContext); const [loading, setLoading] = useState(false); - // Combined key for uniqueness const key = `${printing.set_id}-${printing.rarity_id}`; const currentAmount = ownedAmounts[card_id]?.[key] ?? printing.amount_owned ?? 0; @@ -15,23 +15,10 @@ function PrintingRow({ card_id, printing }) { setLoading(true); try { - const response = await fetch('http://localhost:3000/collection/amount', { - method: 'PUT', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ - card_id, - set_id, - rarity_id, - amount_owned: newAmount - }) - }); - - if (response.ok) { - // Update context using the combined key - updateAmount(card_id, key, newAmount); - } + await updateCardAmount(card_id, set_id, rarity_id, newAmount); + updateAmount(card_id, key, newAmount); } catch (err) { - // silently fail + console.error('Failed to update amount', err); } finally { setLoading(false); } @@ -61,12 +48,9 @@ function PrintingRow({ card_id, printing }) { ); } -// ✅ Memoize PrintingRow for performance export default React.memo( PrintingRow, (prevProps, nextProps) => { - // Re-render only if card_id changes or printing reference changes - // or if the current amount changed const prevKey = `${prevProps.printing.set_id}-${prevProps.printing.rarity_id}`; const nextKey = `${nextProps.printing.set_id}-${nextProps.printing.rarity_id}`; const prevAmount = prevProps.printing.amount_owned; diff --git a/src/hooks/useCardData.js b/src/hooks/useCardData.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/services/api.jsx b/src/services/api.jsx index 661c333..e485400 100644 --- a/src/services/api.jsx +++ b/src/services/api.jsx @@ -1,4 +1,4 @@ -const API_BASE = 'http://192.168.178.41:3000'; // Backend URL +const API_BASE = 'http://192.168.178.41:3000'; export async function fetchCards() { const response = await fetch(`${API_BASE}/exportCards`); @@ -11,4 +11,20 @@ export async function fetchCardImage(cardId) { if (!response.ok) throw new Error('Failed to fetch card image'); const data = await response.json(); return data.image; +} + +export async function updateCardAmount(cardId, setId, rarityId, amount) { + const response = await fetch(`${API_BASE}/collection/amount`, { + method: 'PUT', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + card_id: cardId, + set_id: setId, + rarity_id: rarityId, + amount_owned: amount + }) + }); + + if (!response.ok) throw new Error('Failed to update amount'); + return await response.json(); } \ No newline at end of file diff --git a/src/store/Provider.jsx b/src/store/Provider.jsx deleted file mode 100644 index e69de29..0000000 diff --git a/src/utils/formatter.js b/src/utils/formatter.js deleted file mode 100644 index e69de29..0000000