Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hook up bid now button #84

Merged
merged 3 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions backend/auction/serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from datetime import date

from rest_framework import serializers

from .models import Auction, AuctionItem, AuctionVerifiedUser


class AuctionSerializer(serializers.ModelSerializer):
auctionday_id = serializers.SerializerMethodField()

class Meta:
model = Auction
fields = [
Expand All @@ -15,8 +19,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:
Expand Down
31 changes: 29 additions & 2 deletions frontend/src/components/modals/BiddingModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -37,6 +40,30 @@ export default function BiddingModal({
}
};

const handleBidNow = async () => {
const bidderId = 1;

try {
const response = await fetchData({
method: 'POST',
endpoint: '/v1/bids/',
data: {
amount: bidAmount,
bidder_id: bidderId,
auction_id: auction.id,
auction_day_id: auction.auctionday_id,
object_id: vehicle.id,
},
});
if (response.status === 201) {
onClose();
onPriceUpdate(bidAmount);
}
} catch (error) {
/* empty */
}
};

useEffect(() => {
window.addEventListener('click', handleOutsideClick);
return () => {
Expand Down
14 changes: 10 additions & 4 deletions frontend/src/pages/VehicleDetailsPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export default function VehicleDetailsPage() {
endpoint: '/v1/auctions/current',
method: 'GET',
});

setAuction(response.data);
};
fetchAuction();
Expand All @@ -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 (
<div className="flex flex-col max-w-screen min-w-screen min-h-screen justify-between">
<ScrollRestoration />
Expand Down Expand Up @@ -187,10 +191,12 @@ export default function VehicleDetailsPage() {
</div>
{bidModalOpen && (
<BiddingModal
vehicle={vehicle}
auction={auction}
isOpen={bidModalOpen}
onClose={() => setBidModalOpen(false)}
handleBidNow={() => {}}
minimumBid={210000}
minimumBid={vehicle.current_price}
onPriceUpdate={handlePriceUpdate}
/>
)}

Expand Down
Loading