Skip to content

Commit

Permalink
Malcolm/current upcoming auctions (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolmm20 authored Mar 21, 2024
1 parent c1b8521 commit 07d5b4d
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 64 deletions.
6 changes: 6 additions & 0 deletions backend/auction/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
AuctionListApiView,
GetSavedUnitApiView,
SaveUnitApiView,
AuctionVehiclesApiView,
)

urlpatterns = [
Expand All @@ -27,4 +28,9 @@
AddToAuctionApiView.as_view(),
name="add_to_auction",
),
path(
"<uuid:auction_id>/vehicles/",
AuctionVehiclesApiView.as_view(),
name="auction_vehicles",
),
]
35 changes: 34 additions & 1 deletion backend/auction/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
138 changes: 75 additions & 63 deletions frontend/src/pages/HomePage.jsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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
Expand All @@ -28,6 +34,40 @@ export default function HomePage() {
currentAuctionButton = <RegisterForAuctionButton />;
}

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 (
<div className="min-w-screen max-w-screen">
<div className="relative min-h-screen">
Expand Down Expand Up @@ -98,90 +138,62 @@ export default function HomePage() {
</h2>
<AuctionsSearchBar setResults={setSearchedAuctions} />
</div>
<div className="flex flex-col gap-y-[18px] w-[80%] items-start">
{currentAuctionList.length > 0 && <div className="flex flex-col gap-y-[18px] w-[80%] items-start">
<h2 className="text-mv-black text-4xl font-semibold">
Current Auction
</h2>
<CurrentAuctionCard
imageUrls={[image, image, image, image]}
startDate={new Date('2024-02-22')}
endDate={new Date('2024-02-28')}
numberOfEquipment={10}
numberOfTrailers={15}
numberOfTrucks={5}
startDate={new Date(currentAuctionList[0].start_date)}
endDate={new Date(currentAuctionList[0].end_date)}
numberOfEquipment={currentAuctionList[0].items.equipment.length}
numberOfTrailers={currentAuctionList[0].items.trailers.length}
numberOfTrucks={currentAuctionList[0].items.vehicles.length}
button={currentAuctionButton}
/>
</div>
<div className="flex flex-col gap-y-[18px] w-[80%] items-start">
</div>}
{upcomingAuctionList.length > 0 && <div className="flex flex-col gap-y-[18px] w-[80%] items-start">
<h2 className="text-mv-black text-4xl font-semibold">
Upcoming Auctions
</h2>
<div className="grid grid-cols-3 grid-rows-1 gap-[4.3rem] w-full">
<UpcomingAuctionCard
imageUrls={[image, image, image, image]}
startDate={new Date('2024-02-29')}
endDate={new Date('2024-03-05')}
numberOfEquipment={3}
numberOfTrailers={18}
numberOfTrucks={20}
button={upcomingAuctionButton}
/>
<UpcomingAuctionCard
imageUrls={[image, image, image, image]}
startDate={new Date('2024-03-07')}
endDate={new Date('2024-03-10')}
numberOfEquipment={3}
numberOfTrailers={18}
numberOfTrucks={20}
button={upcomingAuctionButton}
/>
<UpcomingAuctionCard
imageUrls={[image, image, image, image]}
startDate={new Date('2024-04-25')}
endDate={new Date('2024-04-30')}
numberOfEquipment={3}
numberOfTrailers={18}
numberOfTrucks={20}
button={upcomingAuctionButton}
/>
{upcomingAuctionList.map(auction => (
<UpcomingAuctionCard
imageUrls={[image, image, image, image]}
startDate={new Date(auction.start_date)}
endDate={new Date(auction.end_date)}
numberOfEquipment={auction.items.equipment.length}
numberOfTrailers={auction.items.trailers.length}
numberOfTrucks={auction.items.vehicles.length}
button={upcomingAuctionButton}
/>
))}

</div>
</div>
<div className="flex flex-col gap-y-[18px] w-[80%] items-start">
</div>}
{pastAuctionList.length > 0 && <div className="flex flex-col gap-y-[18px] w-[80%] items-start">
<h2 className="text-mv-black text-4xl font-semibold">
Past Auctions
</h2>
<div className="grid grid-cols-3 grid-rows-1 gap-[4.3rem] w-full">
<PastAuctionCard
imageUrls={[image, image, image, image]}
startDate={new Date('2024-02-29')}
endDate={new Date('2024-03-05')}
numberOfEquipment={3}
numberOfTrailers={18}
numberOfTrucks={20}
/>
<PastAuctionCard
imageUrls={[image, image, image, image]}
startDate={new Date('2024-03-07')}
endDate={new Date('2024-03-10')}
numberOfEquipment={3}
numberOfTrailers={18}
numberOfTrucks={20}
/>
<PastAuctionCard
imageUrls={[image, image, image, image]}
startDate={new Date('2024-04-25')}
endDate={new Date('2024-04-30')}
numberOfEquipment={3}
numberOfTrailers={18}
numberOfTrucks={20}
/>
{pastAuctionList.map(auction => (
<PastAuctionCard
imageUrls={[image, image, image, image]}
startDate={new Date(auction.start_date)}
endDate={new Date(auction.end_date)}
numberOfEquipment={auction.items.equipment.length}
numberOfTrailers={auction.items.trailers.length}
numberOfTrucks={auction.items.vehicles.length}
/>
))}

</div>
<button type="button" className="flex ml-auto items-end">
<p className="text-mv-black text-base font-normal underline">
view more
</p>
</button>
</div>
</div>}
</div>
<div className="w-full items-center">
<Footer />
Expand Down
22 changes: 22 additions & 0 deletions frontend/src/utils/auctionUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const sortAuctions = (auctionList) => {
const upcomingList = []
const currentList = []
const pastList = []

const now = new Date(Date.now());
auctionList.forEach(auction => {
if (new Date(auction.start_date) > now) {
upcomingList.push(auction);
} else if (new Date(auction.end_date) > now) {
currentList.push(auction);
} else {
pastList.push(auction);
}
});

return ({"upcoming": upcomingList, "current": currentList, "past": pastList});
}



export default sortAuctions;

0 comments on commit 07d5b4d

Please sign in to comment.