137 lines
5.2 KiB
JavaScript
137 lines
5.2 KiB
JavaScript
var MessageHandler = (function () {
|
|
// Handlers for each message type
|
|
let animation = "slideDown";
|
|
const handlers = {
|
|
announcement: function (data) {
|
|
var myObject = JSON.parse(data);
|
|
toastr.info(`<div class="item announcemantToast">${myObject.message}</div>`, myObject.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) {
|
|
console.log(data);
|
|
var myObject = JSON.parse(data);
|
|
toastr.success(`<div class="item bidToast"><p>${myObject.bidPrice}</p><p>${myObject.productName}</p></div>`, "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");
|
|
|
|
var publicProductBidBox = document.getElementById("publicProductBidBox");
|
|
if (publicProductBidBox)
|
|
{
|
|
refreshPublicBidBox(myObject);
|
|
}
|
|
},
|
|
ProductToAuctionStatusNotification: function (data) {
|
|
console.log(data);
|
|
var myObject = JSON.parse(data);
|
|
var auctionDto = myObject.auctionDto;
|
|
var productToAuctionDto = myObject.auctionDto.productToAuctionDtos[0];
|
|
|
|
toastr.success(`<div class="item bidToast"><p>${productToAuctionDto.auctionStatus}</p><p>${productToAuctionDto.id}</p></div>`, "Status changed", {
|
|
"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");
|
|
|
|
// var publicProductBidBox = document.getElementById("publicProductBidBox");
|
|
// if (publicProductBidBox)
|
|
// {
|
|
// refreshPublicBidBox(myObject);
|
|
// }
|
|
},
|
|
openItemMessage: function (data) {
|
|
var myObject = JSON.parse(data);
|
|
toastr.success(`<div class="item bidToast"><p>${myObject.bidPrice}</p><p>${myObject.productName}</p></div>`, "Item auction is OPENED!", {
|
|
"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");
|
|
|
|
var publicProductBidBox = document.getElementById("publicProductBidBox");
|
|
if (publicProductBidBox) {
|
|
refreshPublicBidBox(myObject);
|
|
}
|
|
|
|
},
|
|
|
|
// 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 {
|
|
var parsedMessage = JSON.parse(message);
|
|
var messageType = parsedMessage.messageType;
|
|
var senderId = parsedMessage.senderId;
|
|
var messageData = parsedMessage.data;
|
|
console.log("Message type:" + messageType);
|
|
console.log("Message sender:" + senderId);
|
|
console.log("Message content" + parsedMessage.data);
|
|
// Route to appropriate handler, default if no match
|
|
(handlers[messageType] || handlers.default)(messageData);
|
|
} catch (e) {
|
|
console.error("Error parsing message:", e);
|
|
}
|
|
}
|
|
|
|
return {
|
|
handle: messageRouter
|
|
};
|
|
})();
|