wtf javascript error fix, ViewModel being empty on document.ready

This commit is contained in:
Adam 2024-11-21 11:51:31 +01:00
parent 1e1d612b0f
commit 29adf2772a
3 changed files with 38 additions and 33 deletions

View File

@ -145,12 +145,11 @@ public class AuctionPublicViewComponent : NopViewComponent
productBidBoxViewModel.IsAdmin = await _customerService.IsAdminAsync(customer);
productBidBoxViewModel.IsGuest = await _customerService.IsGuestAsync(customer);
productBidBoxViewModel.AuctionClosed = auction.Closed;
productBidBoxViewModel.Status = productToAuctionId.FirstOrDefault().AuctionStatus;
productBidBoxViewModel.IsItemActive = isActive;
productBidBoxViewModel.WidgetZone = widgetZone;
productBidBoxViewModel.BasePrice = productDetailsModel.ProductPrice.OldPriceValue;
productBidBoxViewModel.CurrentPrice = productDetailsModel.ProductPrice.PriceValue;
productBidBoxViewModel.ProductToAuctionsId = productToAuctionId.FirstOrDefault().Id;
productBidBoxViewModel.ProductToAuctionId = productToAuctionId.FirstOrDefault().Id;
productBidBoxViewModel.AuctionId = auctionId;
productBidBoxViewModel.CustomerId = customer.Id;
productBidBoxViewModel.ProductId = productDetailsModel.Id;

View File

@ -10,12 +10,12 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Models
{
public record ProductBidBoxViewModel: BaseNopModel
{
public int ProductToAuctionsId { get; set; }
public int ProductToAuctionId { get; set; }
public bool IsAdmin { get; set; }
public bool IsGuest { get; set; }
public int AuctionId { get; set; }
public bool AuctionClosed { get; set; }
public AuctionStatus Status { get; set; }
public bool IsItemActive { get; set; }
public int ProductId { get; set; }
public int CustomerId { get; set; }

View File

@ -1,5 +1,5 @@
@model ProductBidBoxViewModel
@inject IJsonHelper JsonHelper;
@* @inject IJsonHelper JsonHelper; *@
@* @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(myObj) as String) *@
@ -75,32 +75,38 @@
}
<script>
var pageViewModel = undefined;
var bidBoxPageViewModel;
$(window).load(function () {
try {
$(document).ready(function () {
pageViewModel = @Html.Raw(Json.Serialize(Model));
console.log(pageViewModel);
console.log(pageViewModel.WidgetZone);
bidBoxPageViewModel = @Html.Raw(Json.Serialize(Model));
}
catch (e) {
console.log(e); // Logs the error
}
console.log("bidBoxPageViewModel " + bidBoxPageViewModel);
console.log(bidBoxPageViewModel.WidgetZone);
console.log(typeof sendMessageToServer);
$("#signalRBidButton").on("click", function () {
document.getElementById("signalRBidButton").disabled = true;
event.preventDefault();
if (!bidBoxPageViewModel) {
console.log("we need viewmodel data");
bidBoxPageViewModel = @Html.Raw(Json.Serialize(Model));
}
var bidMessage = {
ProductAuctionMappingId: pageViewModel.ProductToAuctions,
AuctionId: pageViewModel.AuctionId,
BidPrice: pageViewModel.BidPrice,
ProductId: pageViewModel.ProductId,
CustomerId: pageViewModel.CustomerId
ProductAuctionMappingId: bidBoxPageViewModel.ProductToAuctionId,
AuctionId: bidBoxPageViewModel.AuctionId,
BidPrice: bidBoxPageViewModel.BidPrice,
ProductId: bidBoxPageViewModel.ProductId,
CustomerId: bidBoxPageViewModel.CustomerId
};
var content = JSON.stringify(bidMessage);
console.log(content);
sendMessageToServer("BidRequestMessage", pageViewModel.CustomerId, content);
console.log("WTF " + content);
sendMessageToServer("BidRequestMessage", bidBoxPageViewModel.CustomerId, content);
return false;
});
@ -111,12 +117,12 @@
event.preventDefault();
var openItemMessage = {
ProductAuctionMappingId: pageViewModel.ProductToAuctions,
AdminId: pageViewModel.CustomerId
ProductAuctionMappingId: bidBoxPageViewModel.ProductToAuctionId,
AdminId: bidBoxPageViewModel.CustomerId
};
var content = JSON.stringify(openItemMessage);
console.log(content);
sendMessageToServer("OpenItemRequestMessage", pageViewModel.CustomerId, content);
sendMessageToServer("OpenItemRequestMessage", bidBoxPageViewModel.CustomerId, content);
return false;
});
@ -126,12 +132,12 @@
event.preventDefault();
var itemFirstWarningMessage = {
ProductAuctionMappingId: pageViewModel.ProductToAuctions,
AdminId: pageViewModel.CustomerId
ProductAuctionMappingId: bidBoxPageViewModel.ProductToAuctionId,
AdminId: bidBoxPageViewModel.CustomerId
};
var content = JSON.stringify(itemFirstWarningMessage);
console.log(content);
sendMessageToServer("FirstWarningMessage", pageViewModel.CustomerId, content);
sendMessageToServer("FirstWarningMessage", bidBoxPageViewModel.CustomerId, content);
return false;
});
@ -145,23 +151,23 @@
currency: 'HUF',
});
var widgetPriceElement = document.getElementById("price-value-" + pageViewModel.ProductId);
var widgetPriceElement = document.getElementById("price-value-" + bidBoxPageViewModel.ProductId);
var budButtonElement = document.getElementById("signalRBidButton");
var licitStepElement = document.getElementById("licitStepText");
if (widgetPriceElement) {
widgetPriceElement.textContent = HUFFormatter.format(data.bidPrice); // Update the price
licitStepElement.textContent = HUFFormatter.format(data.nextStepAmount);
pageViewModel.BidPrice = Number(data.bidPrice) + Number(data.nextStepAmount);
bidBoxPageViewModel.BidPrice = Number(data.bidPrice) + Number(data.nextStepAmount);
budButtonElement.textContent = "Bid " + HUFFormatter.format(pageViewModel.BidPrice);
budButtonElement.textContent = "Bid " + HUFFormatter.format(bidBoxPageViewModel.BidPrice);
// if (pageViewModel.CustomerId == data.CustomerId) {
// if (bidBoxPageViewModel.CustomerId == data.CustomerId) {
// }
console.log(`WidgetPrice updated to: ${data.bidPrice}`);
console.log(`WidgetPrice updated to: ${data.bidPrice}, next bid is ${bidBoxPageViewModel.BidPrice}`);
budButtonElement.disabled = false;
} else {
console.warn("Element with ID 'WidgetPrice' not found in the DOM.");