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 real time bid updates to frontend #79

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion backend/bid/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def bid_updated(sender, instance, created, **kwargs):
}

async_to_sync(channel_layer.group_send)(
"auction_{}".format(instance.auction_id),
"bid_updates",
{"type": "bid.update", "bid_data": bid_data},
)

Expand Down
11 changes: 9 additions & 2 deletions backend/bid/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

class BidListApiView(APIView):
permission_classes = [IsAuthenticated]

serializer_class = BidSerializer
cognitoService = AWSCognitoService()

Expand Down Expand Up @@ -52,8 +53,14 @@ def post(self, request, *args, **kwargs):

item = get_object_or_404(model, id=data.get("object_id"))

highest_bid = Bid.objects.filter(item=item).order_by("-amount").first()
if highest_bid and int(data["amount"]) <= highest_bid.amount:
highest_bid = Bid.objects.filter(object_id=item.id).order_by("-amount").first()
if highest_bid is None:
if int(data["amount"]) < item.starting_price:
return Response(
{"error": "Your bid must be higher than the starting price."},
status=status.HTTP_400_BAD_REQUEST,
)
elif highest_bid and int(data["amount"]) <= highest_bid.amount:
return Response(
{"error": "Your bid must be higher than the current highest bid."},
status=status.HTTP_400_BAD_REQUEST,
Expand Down
25 changes: 24 additions & 1 deletion frontend/src/pages/ListingsPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export default function ListingsPage() {
const [selectedMinPrice, setSelectedMinPrice] = useState(0);
const [selectedMaxPrice, setSelectedMaxPrice] = useState(0);
const { fetchData } = useAxios();

const wsURL = process.env.REACT_APP_NODE_ENV === 'dev'
? process.env.REACT_APP_DEV_BACKEND_WS_BASE_URL
: process.env.REACT_APP_PROD_BACKEND_WS_BASE_URL
const sortByItems = ['All', 'Trucks', 'Equipment', 'Trailers'];

const updateMinPrice = ({ target: { value } }) => {
Expand All @@ -51,6 +53,27 @@ export default function ListingsPage() {
}
};

useEffect(() => {
const chatSocket = new WebSocket(wsURL);

chatSocket.onmessage = function(event) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevinrczhang Use arrow function instead to fix it

const message = JSON.parse(event.data);

if (message.bid_data) {
const bidData = message.bid_data;

setUnits(prevUnits =>
prevUnits.map(unit =>
unit.id === bidData.object_id
? { ...unit, current_price: bidData.amount }
: unit
)
);
}
};

return () => chatSocket.close();
}, []);
const updateMaxPrice = ({ target: { value } }) => {
if (value === '') {
setSelectedMaxPrice(0);
Expand Down
Loading