From 07d5b4d9eb063d181a2a7f3b9cf4e58487e1d40d Mon Sep 17 00:00:00 2001 From: malcolmm20 Date: Thu, 21 Mar 2024 15:59:08 -0700 Subject: [PATCH] Malcolm/current upcoming auctions (#58) --- backend/auction/urls.py | 6 ++ backend/auction/views.py | 35 +++++++- frontend/src/pages/HomePage.jsx | 138 ++++++++++++++++------------- frontend/src/utils/auctionUtils.js | 22 +++++ 4 files changed, 137 insertions(+), 64 deletions(-) create mode 100644 frontend/src/utils/auctionUtils.js diff --git a/backend/auction/urls.py b/backend/auction/urls.py index db023c4..f3ca8dc 100644 --- a/backend/auction/urls.py +++ b/backend/auction/urls.py @@ -7,6 +7,7 @@ AuctionListApiView, GetSavedUnitApiView, SaveUnitApiView, + AuctionVehiclesApiView, ) urlpatterns = [ @@ -27,4 +28,9 @@ AddToAuctionApiView.as_view(), name="add_to_auction", ), + path( + "/vehicles/", + AuctionVehiclesApiView.as_view(), + name="auction_vehicles", + ), ] diff --git a/backend/auction/views.py b/backend/auction/views.py index 27f8314..b1530b6 100644 --- a/backend/auction/views.py +++ b/backend/auction/views.py @@ -7,7 +7,7 @@ from core.permissions import IsAdminUser, IsAuthenticated from services.AWSCognitoService import AWSCognitoService -from vehicle.models import SavedUnits, Vehicle +from vehicle.models import SavedUnits, Vehicle, Equipment, Trailer from .models import Auction, AuctionItem from .serializers import AuctionSerializer @@ -212,3 +212,36 @@ def post(self, request, *args, **kwargs): {"message": "Vehicle added to auction successfully"}, status=status.HTTP_201_CREATED, ) + + +class AuctionVehiclesApiView(APIView): + """ + An endpoint to retrieve an auction's associated vehicles + """ + + cognitoService = AWSCognitoService() + + def get(self, request, **kwargs): + auction_id = kwargs.get("auction_id") + auction = get_object_or_404(Auction, id=auction_id) + + auction_items = AuctionItem.objects.filter(auction_id=auction) + + vehicle_list = [] + equipment_list = [] + trailer_list = [] + + for auction_item in auction_items: + if isinstance(auction_item.content_object, Vehicle): + vehicle_list.append(auction_item.content_object) + elif isinstance(auction_item.content_object, Equipment): + equipment_list.append(auction_item.content_object) + elif isinstance(auction_item.content_object, Trailer): + trailer_list.append(auction_item.content_object) + + vehicle_data = [{"id": vehicle.id} for vehicle in vehicle_list] + equipment_data = [{"id": equipment.id} for equipment in equipment_list] + trailer_data = [{"id": trailer.id} for trailer in trailer_list] + + return Response({"vehicles": vehicle_data, "equipment": equipment_data, + "trailers": trailer_data}, status=status.HTTP_200_OK) diff --git a/frontend/src/pages/HomePage.jsx b/frontend/src/pages/HomePage.jsx index 0142bb4..5d0d61c 100644 --- a/frontend/src/pages/HomePage.jsx +++ b/frontend/src/pages/HomePage.jsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useEffect, useState } from 'react'; import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; import NavBar from '../components/navBars/NavBar'; import CoverImage from '../assets/cover-image.png'; @@ -13,9 +13,15 @@ import LogInJoinAuctionButton from '../components/buttons/LogInJoinAuctionButton import RegisterEarlyButton from '../components/buttons/RegisterEarlyButton'; import RegisterForAuctionButton from '../components/buttons/RegisterForAuctionButton'; import useAuth from '../hooks/useAuth'; +import useAxios from '../hooks/useAxios'; +import sortAuctions from '../utils/auctionUtils'; export default function HomePage() { const [searchedAuctions, setSearchedAuctions] = useState([]); + const [currentAuctionList, setCurrentAuctionList] = useState([]); + const [upcomingAuctionList, setUpcomingAuctionList] = useState([]); + const [pastAuctionList, setPastAuctionList] = useState([]); + const { fetchData } = useAxios(); const { isAuthenticated } = useAuth(); // eslint-disable-next-line no-console @@ -28,6 +34,40 @@ export default function HomePage() { currentAuctionButton = ; } + const getAuctions = async () => { + try { + // get auction list + const response = await fetchData({ + endpoint: 'auctions/', + method: 'GET', + }); + + // get items associated with each auction + const itemsPromises = response.data.map(async (auction) => { + const itemResponse = await fetchData({ + endpoint: `auctions/${auction.id}/vehicles/`, + method: 'GET', + }); + return { ...auction, items: itemResponse.data }; + }); + + const auctionsWithItems = await Promise.all(itemsPromises); + + const auctionList = sortAuctions(auctionsWithItems); + setUpcomingAuctionList(auctionList.upcoming); + setCurrentAuctionList(auctionList.current); + setPastAuctionList(auctionList.past); + } catch (err) { + // eslint-disable-next-line no-console + console.error(err); + } + }; + + useEffect(() => { + getAuctions(); + }, []); + + return (
@@ -98,90 +138,62 @@ export default function HomePage() {
-
+ {currentAuctionList.length > 0 &&

Current Auction

-
-
+
} + {upcomingAuctionList.length > 0 &&

Upcoming Auctions

- - - + {upcomingAuctionList.map(auction => ( + + ))} +
-
-
+
} + {pastAuctionList.length > 0 &&

Past Auctions

- - - + {pastAuctionList.map(auction => ( + + ))} +
-
+
}