diff --git a/components/Auction/index.js b/components/Auction/index.js index e6feb3c..391307f 100644 --- a/components/Auction/index.js +++ b/components/Auction/index.js @@ -11,35 +11,17 @@ const WALLET_URL = `${BASE_API_URL}/wallet`; const HandleAuctions = () => { const [auctionsData, setAuctionsData] = useState([]); - const [userBid, setUserBid] = useState(); + const [userBid, setUserBid] = useState(0); const [isUserLoggedIn, setIsUserLoggedIn] = useState(false); const [isLoading, setIsLoading] = useState(true); - const [userMoney, setUserMoney] = useState(); - - useEffect(() => { - fetchAndSetAuctions(); - }, []); - - useEffect(() => { - getUserWallet(); - }, []); - - useEffect(() => { - fetchSelfDetails() - .then((res) => { - if (res.status === 200) { - setIsUserLoggedIn(true); - } - }) - .catch((err) => { - console.log('User is not logged in', err); - }); - }, []); + const [userMoney, setUserMoney] = useState(0); const fetchAndSetAuctions = async () => { const response = await fetchData(AUCTIONS_URL); - const json = await response.json(); - setAuctionsData(json.auctions); + if (!response) return null; + setAuctionsData( + JSON.parse(response && response.auctions ? response.auctions : 0) || 0 + ); setIsLoading(false); }; @@ -47,7 +29,7 @@ const HandleAuctions = () => { const response = await fetchData(WALLET_URL, 'GET', { credentials: 'include', }); - const { status } = await response; + const { status } = response; if (status === 200) { const { wallet } = await response.json(); if (Object.keys(wallet).length === 0) return setUserMoney(0); @@ -59,9 +41,26 @@ const HandleAuctions = () => { } } else { setUserMoney(0); + return null; } }; + useEffect(() => { + (async () => { + await fetchAndSetAuctions(); + await getUserWallet(); + await fetchSelfDetails() + .then((res) => { + if (res.status === 200) { + setIsUserLoggedIn(true); + } + }) + .catch((err) => { + console.error('User is not logged in', err); + }); + })(); + }, []); + const handleNewBid = async (e, auctionId) => { e.preventDefault(); if (!isUserLoggedIn) { @@ -86,7 +85,7 @@ const HandleAuctions = () => { ); const { status } = await response; if (status === 201) { - fetchAndSetAuctions(); + await fetchAndSetAuctions(); setIsLoading(false); } } @@ -108,78 +107,82 @@ const HandleAuctions = () => { e.target.src = '/assets/default_avatar.jpg'; }; - const auctionHandler = auctionsData.map((auction) => { - const { id, seller, quantity, highest_bid, bidders } = auction; - return ( -
-
brokenImageHandler(e)}
/>
+