var MessageHandler = (function () { // Handlers for each message type let animation = "slideDown"; const handlers = { announcement: function (data) { toastr.info(`
${data.message}
`, data.title, { "closeButton": true, "positionClass": "toast-bottom-right", "newestOnTop": true, "progressBar": true, "preventDuplicates": false, "onclick": null, "showDuration": "30000", "hideDuration": "1000", "timeOut": "5000", "extendedTimeOut": "1000", "showEasing": "swing", "hideEasing": "linear", "showMethod": animation, "hideMethod": "fadeOut" }); $('.toast-info').css("background-color", "#008080"); }, bidNotification: function (data) { toastr.success(`

${data.bidPrice}

${data.productName}

`, "New bid arrived", { "closeButton": true, "positionClass": "toast-bottom-right", "newestOnTop": true, "progressBar": true, "preventDuplicates": false, "onclick": null, "showDuration": "30000", "hideDuration": "1000", "timeOut": "5000", "extendedTimeOut": "1000", "showEasing": "swing", "hideEasing": "linear", "showMethod": animation, "hideMethod": "fadeOut" }); $('.toast-success').css("background-color", "#4caf50"); }, auctionUpdateAlternate: function (data) { const widgetPriceElement = document.getElementById("WidgetPrice"); if (widgetPriceElement) { widgetPriceElement.textContent = data.bidPrice; // Update the price console.log(`WidgetPrice updated to: ${data.bidPrice}`); } else { console.warn("Element with ID 'WidgetPrice' not found in the DOM."); } }, auctionUpdate: function (data) { // Refresh the ViewComponent using AJAX $.ajax({ url: '/Auction/RefreshAuctionWidget', // Controller endpoint method: 'GET', success: function (response) { //$('#auction-widget').html(response); // Update the DOM element console.log("Auction widget refreshed!"); }, error: function (error) { console.error("Error refreshing auction widget:", error); } }); }, // Add more handlers as needed default: function (data) { console.warn("Unhandled message type:", data); } }; // Message router to route to the appropriate handler based on message type function messageRouter(message) { // Parse the JSON message try { const parsedMessage = JSON.parse(message); const messageType = parsedMessage.messageType; const messageData = parsedMessage.data; console.log("Message type:" + messageType); // Route to appropriate handler, default if no match (handlers[messageType] || handlers.default)(messageData); } catch (e) { console.error("Error parsing message:", e); } } return { handle: messageRouter }; })();