signalR client improvements, fixes

This commit is contained in:
Loretta 2024-11-19 19:29:09 +01:00
parent ae63aa8edc
commit 59ae22f5cd
4 changed files with 42 additions and 14 deletions

View File

@ -40,8 +40,9 @@
});
$('.toast-success').css("background-color", "#4caf50");
const publicProductBidBox = document.getElementById("publicProductBidBox");
if (publicProductBidBox) {
var publicProductBidBox = document.getElementById("publicProductBidBox");
if (publicProductBidBox)
{
refreshPublicBidBox(data);
}

View File

@ -10,6 +10,7 @@ using Nop.Services.Logging;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
using Newtonsoft.Json.Serialization;
using Microsoft.AspNetCore.SignalR;
using Nop.Core;
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{
@ -19,13 +20,15 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
protected readonly IProductService _productService;
protected readonly AuctionService _auctionService;
private IHubContext<AuctionHub> _hubContext;
private readonly IWorkContext _workContext;
public SignalRMessageHandler(ILogger logger, IProductService productService, AuctionService auctionService, IHubContext<AuctionHub> hubContext)
public SignalRMessageHandler(ILogger logger, IProductService productService, AuctionService auctionService, IHubContext<AuctionHub> hubContext, IWorkContext workContext)
{
_logger = logger;
_productService = productService;
_auctionService = auctionService;
_hubContext = hubContext;
_workContext = workContext;
}
public async Task HandleMessage(MessageWrapper message)
@ -36,9 +39,15 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
return;
}
//TODO: A MessageWrapper-ben kéne küldözgetni a UserId (CustomerId-t) - J.
//if (message.UserId != (await _workContext.GetCurrentCustomerAsync()).Id)
//{
// _logger.Error($"SignalRMessageHandler.HandleMessage(); message.UserId != (await _workContext.GetCurrentCustomerAsync()).Id");
// return;
//}
switch (message.MessageType)
{
//nameof(IAuctionHubClient.SendPrice)
case "BidRequestMessage":
await HandleBidRequest(message.Data.ToString()?.JsonTo<AuctionBidRequest>());
break;
@ -62,6 +71,12 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{
await _logger.InformationAsync($"SignalRMessageHandler.HandleBidRequest(); Bid received: - Auction: {bidRequestMessage.AuctionId} Product: {bidRequestMessage.ProductId} - Bid: {bidRequestMessage.BidPrice} - Customer: {bidRequestMessage.CustomerId}");
if (bidRequestMessage.CustomerId != (await _workContext.GetCurrentCustomerAsync()).Id)
{
_logger.Error($"SignalRMessageHandler.HandleBidRequest(); bidRequestMessage.CustomerId != (await _workContext.GetCurrentCustomerAsync()).Id");
return;
}
var product = await _productService.GetProductByIdAsync(bidRequestMessage.ProductId);
if (product == null)
{
@ -80,9 +95,9 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
auctionBid.ProductAuctionMappingId = mapping.FirstOrDefault()!.Id;
//TODO: validate the bidprice amount
if (product.Price >= bidRequestMessage.BidPrice)
if (product.Price >= auctionBid.BidPrice)
{
_logger.Warning($"SignalRMessageHandler.HandleBidRequest(); product.Price >= bidRequestMessage.BidPrice; productPrice: {product.Price}; bidRequestPrice: {bidRequestMessage.BidPrice}");
_logger.Warning($"SignalRMessageHandler.HandleBidRequest(); product.Price >= bidRequestMessage.BidPrice; productPrice: {product.Price}; bidRequestPrice: {auctionBid.BidPrice}");
return;
}
@ -99,8 +114,8 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
MessageType = "bidNotification",
Data = new BidNotificationMessage
{
ProductName = bidRequestMessage.ProductId.ToString(),
BidPrice = bidRequestMessage.BidPrice.ToString(CultureInfo.InvariantCulture),
ProductName = auctionBid.ProductId.ToString(),
BidPrice = auctionBid.BidPrice.ToString(CultureInfo.InvariantCulture),
NextStepAmount = "50000"
}
};

View File

@ -74,7 +74,7 @@ public class AuctionService : IAuctionService
private async Task<bool> ValidateAuctionBid(AuctionBid auctionBid)
{
auctionBid.CustomerId = (await _workContext.GetCurrentCustomerAsync()).Id; //elvileg megnézi cache-ben, csak utána DB-zik! - J.
//auctionBid.CustomerId = (await _workContext.GetCurrentCustomerAsync()).Id; //elvileg megnézi cache-ben, csak utána DB-zik! - J.
//etc...
return true;

View File

@ -21,7 +21,7 @@
<span class="value">@String.Format("{0:c}", Model.LicitStep)</span>
</div>
<div>
<button id="signalRBidButton" class="btn btn-success">
<button id="signalRBidButton" class="btn btn-success" type="button">
Bid @String.Format("{0:c}", Model.BidPrice)
</button>
@* <button id="bidButton" class="btn btn-success">
@ -81,6 +81,10 @@
// });
$("#signalRBidButton").on("click", function () {
document.getElementById("signalRBidButton").disabled = true;
event.preventDefault();
var bidMessage = {
AuctionId: pageViewModel.AuctionId.toFixed(),
BidPrice: pageViewModel.BidPrice.toFixed(2),
@ -90,7 +94,7 @@
sendMessageToServer("BidRequestMessage", bidMessage);
return false;
});
@ -114,13 +118,21 @@
// }
// });
const widgetPriceElement = document.getElementById("price-value-"+pageViewModel.ProductId);
const budButtonelement = document.getElementById("signalRBidButton");
var widgetPriceElement = document.getElementById("price-value-"+pageViewModel.ProductId);
var budButtonElement = document.getElementById("signalRBidButton");
if (widgetPriceElement) {
widgetPriceElement.textContent = data.bidPrice; // Update the price
budButtonElement.textContent = data.bidPrice + data.nextStep;
pageViewModel.BidPrice = Number(data.bidPrice) + Number(data.nextStepAmount);
budButtonElement.textContent = pageViewModel.BidPrice.toFixed(2);
// if (pageViewModel.CustomerId == data.CustomerId) {
// }
console.log(`WidgetPrice updated to: ${data.bidPrice}`);
budButtonElement.disabled = false;
} else {
console.warn("Element with ID 'WidgetPrice' not found in the DOM.");
}