From db348b3f51dcb27d2e3e81169883481de1916e45 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 10 Sep 2021 14:03:52 +0200 Subject: [PATCH] follow profile working from Web App to Network Node --- Desktop/UI/WebApp.js | 88 ++++++++++++++++--- .../DK/modules/WebSocketsInterface.js | 20 ++++- .../NT/Modules/ClientInterface.js | 10 +++ 3 files changed, 101 insertions(+), 17 deletions(-) diff --git a/Desktop/UI/WebApp.js b/Desktop/UI/WebApp.js index a80a54df5..303afa9ab 100644 --- a/Desktop/UI/WebApp.js +++ b/Desktop/UI/WebApp.js @@ -131,39 +131,58 @@ function newWebApp() { */ function onError(errorMessage) { console.log('[ERROR] Query not executed. ' + errorMessage) + console.log('[ERROR] query = ' + JSON.stringify(query)) } } function setupEventHandlers() { /* - Add events to process button clicks. + Add events to process button clicks , and mouseWheel. */ document.addEventListener("click", onClick) - document.addEventListener('mousewheel', onMouseWheel, false) - function onClick(event) { + async function onClick(event) { if (event.target && event.target.nodeName === "BUTTON") { switch (event.target.action) { case 'Follow Profile': { - let span = document.getElementById('profile-to-follow-span-' + event.target.userProfileId) - let button = document.getElementById('profile-to-follow-button-' + event.target.userProfileId) - span.setAttribute("class", "profile-to-unfollow-span") - button.setAttribute("class", "profile-to-unfollow-button") - button.action = 'Unfollow Profile' + await followUserProfile(event.target.userProfileId) + .then(updateButton) + .catch(onError) + + function updateButton() { + let span = document.getElementById('profile-to-follow-span-' + event.target.userProfileId) + let button = document.getElementById('profile-to-follow-button-' + event.target.userProfileId) + span.setAttribute("class", "profile-to-unfollow-span") + button.setAttribute("class", "profile-to-unfollow-button") + button.action = 'Unfollow Profile' + } break } case 'Unfollow Profile': { - let span = document.getElementById('profile-to-follow-span-' + event.target.userProfileId) - let button = document.getElementById('profile-to-follow-button-' + event.target.userProfileId) - span.setAttribute("class", "profile-to-follow-span") - button.setAttribute("class", "profile-to-follow-button") - button.action = 'Follow Profile' + await unfollowUserProfile(event.target.userProfileId).catch(onError) + .then(updateButton) + .catch(onError) + + function updateButton() { + let span = document.getElementById('profile-to-follow-span-' + event.target.userProfileId) + let button = document.getElementById('profile-to-follow-button-' + event.target.userProfileId) + span.setAttribute("class", "profile-to-follow-span") + button.setAttribute("class", "profile-to-follow-button") + button.action = 'Follow Profile' + } break } } } + + /* + Error Handling + */ + function onError(errorMessage) { + console.log('[ERROR] Click event failed. ' + errorMessage) + } } function onMouseWheel(event) { @@ -234,9 +253,50 @@ function newWebApp() { tblBody.appendChild(row) - table.appendChild(tblBody) htmlElement.appendChild(table) table.setAttribute("class", "profile-to-follow-table") } + + + async function followUserProfile(userProfileId) { + + return new Promise((resolve, reject) => { asyncCall(resolve, reject) }) + + async function asyncCall(resolve, reject) { + let eventMessage + let event + /* + Test Query User Profiles. + */ + eventMessage = { + eventType: SA.projects.socialTrading.globals.eventTypes.FOLLOW_USER_PROFILE, + eventId: SA.projects.foundations.utilities.miscellaneousFunctions.genereteUniqueId(), + targetUserProfileId: userProfileId + } + + event = { + requestType: 'Event', + eventMessage: JSON.stringify(eventMessage) + } + + await UI.projects.socialTrading.modules.webSocketsClient.sendMessage( + JSON.stringify(event) + ) + .then(onResponse) + .catch(onError) + + async function onResponse(reponse) { + resolve() + } + /* + Error Handling + */ + function onError(errorMessage) { + console.log('[ERROR] Event not executed. ' + errorMessage) + console.log('[ERROR] event = ' + JSON.stringify(event)) + reject(errorMessage) + } + } + } } diff --git a/Projects/Social-Trading/DK/modules/WebSocketsInterface.js b/Projects/Social-Trading/DK/modules/WebSocketsInterface.js index 89b577960..22de639d2 100644 --- a/Projects/Social-Trading/DK/modules/WebSocketsInterface.js +++ b/Projects/Social-Trading/DK/modules/WebSocketsInterface.js @@ -64,11 +64,25 @@ exports.newDesktopModulesWebSocketsInterface = function newDesktopModulesWebSock return } - let response = await webAppInterface.messageReceived(messageHeader.payload) - webApp.socket.send(JSON.stringify(response)) + await webAppInterface.messageReceived(messageHeader.payload) + .then(sendResponseToWebApp) + .catch(onError) + + function sendResponseToWebApp(response) { + webApp.socket.send(JSON.stringify(response)) + } + + function onError(errorMessage) { + console.log('[ERROR] Web Sockets Interface -> onMenssage -> errorMessage = ' + errorMessage) + let response = { + result: 'Error', + message: errorMessage + } + webApp.socket.send(JSON.stringify(response)) + } } catch (err) { - console.log('[ERROR] Web Sockets Interface -> setUpWebSocketServer -> err.stack = ' + err.stack) + console.log('[ERROR] Web Sockets Interface -> onMenssage -> err.stack = ' + err.stack) } } } diff --git a/Projects/Social-Trading/NT/Modules/ClientInterface.js b/Projects/Social-Trading/NT/Modules/ClientInterface.js index 16a1f1978..742b7541f 100644 --- a/Projects/Social-Trading/NT/Modules/ClientInterface.js +++ b/Projects/Social-Trading/NT/Modules/ClientInterface.js @@ -103,6 +103,16 @@ exports.newSocialTradingModulesClientInterface = function newSocialTradingModule return response } /* + We will not accept events that don't have an eventId. + */ + if (eventReceived.eventId === undefined) { + let response = { + result: 'Error', + message: 'eventId Not Provided.' + } + return response + } + /* We will not accept events that have already been processed. */