From d3bdc0b349b7b48e8af1a14dbecf08d9c3219c24 Mon Sep 17 00:00:00 2001 From: _Kevin_Zhang_ <49049813+Dragollax@users.noreply.github.com> Date: Fri, 12 Apr 2024 21:11:12 -0700 Subject: [PATCH 1/2] initial commit --- backend/auction/serializers.py | 13 +++++++ .../src/components/modals/BiddingModal.jsx | 35 +++++++++++++++++-- frontend/src/pages/VehicleDetailsPage.jsx | 14 +++++--- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/backend/auction/serializers.py b/backend/auction/serializers.py index a5fa1a0..4ca8d76 100644 --- a/backend/auction/serializers.py +++ b/backend/auction/serializers.py @@ -1,9 +1,16 @@ from rest_framework import serializers from .models import Auction, AuctionItem, AuctionVerifiedUser +from datetime import date + + +from rest_framework import serializers +from .models import Auction, AuctionDay class AuctionSerializer(serializers.ModelSerializer): + auctionday_id = serializers.SerializerMethodField() + class Meta: model = Auction fields = [ @@ -15,8 +22,14 @@ class Meta: "end_time", "cover_image", "is_published", + "auctionday_id", # Add auctionday_id to the fields ] + def get_auctionday_id(self, obj): + current_date = date.today() + auction_day = obj.days.filter(date=current_date).first() + return auction_day.id if auction_day else None + class AuctionItemSerializer(serializers.ModelSerializer): class Meta: diff --git a/frontend/src/components/modals/BiddingModal.jsx b/frontend/src/components/modals/BiddingModal.jsx index 07da419..18eb288 100644 --- a/frontend/src/components/modals/BiddingModal.jsx +++ b/frontend/src/components/modals/BiddingModal.jsx @@ -3,15 +3,18 @@ import DoNotDisturbOnOutlinedIcon from '@mui/icons-material/DoNotDisturbOnOutlin import AddCircleOutlineOutlinedIcon from '@mui/icons-material/AddCircleOutlineOutlined'; import BidNowButton from '../buttons/BidNowButton'; import CancelButton from '../buttons/CancelButton'; +import useAxios from '../../hooks/useAxios'; export default function BiddingModal({ isOpen, onClose, - handleBidNow, + auction, + vehicle, minimumBid, + onPriceUpdate }) { if (!isOpen) return null; - + const { fetchData } = useAxios(); const increments = minimumBid > 100000 ? 5000 : 1000; const [bidAmount, setBidAmount] = useState(minimumBid + increments); @@ -37,6 +40,34 @@ export default function BiddingModal({ } }; + const handleBidNow = async () => { + const bidderId = 1; + + try { + const response = await fetchData({ + method: 'POST', + endpoint: '/v1/bids/', + data: JSON.stringify({ + amount: bidAmount, + bidder_id: bidderId, + auction_id: auction.id, + auction_day_id: auction.auctionday_id, + object_id: vehicle.id, + }), + headers: { + 'Content-Type': 'application/json', + }, + }); + if (response.status === 201) { + onClose(); + onPriceUpdate(bidAmount); + } + + } catch (error) { + console.error('Error creating bid:', error); + } + }; + useEffect(() => { window.addEventListener('click', handleOutsideClick); return () => { diff --git a/frontend/src/pages/VehicleDetailsPage.jsx b/frontend/src/pages/VehicleDetailsPage.jsx index 2fedaff..dcdd909 100644 --- a/frontend/src/pages/VehicleDetailsPage.jsx +++ b/frontend/src/pages/VehicleDetailsPage.jsx @@ -37,7 +37,6 @@ export default function VehicleDetailsPage() { endpoint: '/v1/auctions/current', method: 'GET', }); - setAuction(response.data); }; fetchAuction(); @@ -63,7 +62,12 @@ export default function VehicleDetailsPage() { startTime.setDate(startTime.getDate() + 1); endTime.setDate(endTime.getDate() + 1); } - + const handlePriceUpdate = (newPrice) => { + setVehicle(prevVehicle => ({ + ...prevVehicle, + current_price: newPrice + })); + }; return (