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

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

View File

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

View File

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