From 29e1f3d68a85ab742a49eabbeed7607f6b92c46f Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 4 Jun 2017 16:02:51 +0200 Subject: [PATCH] Added accept / reject friendship requests --- assets/css/components/friends/friendsBar.css | 2 +- assets/js/common/api.js | 3 +- assets/js/components/friends/friendsBar.js | 139 ++++++++++++++----- assets/js/components/friends/friendsList.js | 29 +++- 4 files changed, 133 insertions(+), 40 deletions(-) diff --git a/assets/css/components/friends/friendsBar.css b/assets/css/components/friends/friendsBar.css index b3ed09b4..1732075d 100644 --- a/assets/css/components/friends/friendsBar.css +++ b/assets/css/components/friends/friendsBar.css @@ -98,6 +98,6 @@ } #friendsList .statusRow { - width: 48px; + width: 58px; text-align: center; } \ No newline at end of file diff --git a/assets/js/common/api.js b/assets/js/common/api.js index 3b70c838..413029c4 100644 --- a/assets/js/common/api.js +++ b/assets/js/common/api.js @@ -61,7 +61,8 @@ ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, requireLoginToke } //We can do the next step - nextAction(result); + if(nextAction) + nextAction(result); } } diff --git a/assets/js/components/friends/friendsBar.js b/assets/js/components/friends/friendsBar.js index ff265df4..c949fa43 100644 --- a/assets/js/components/friends/friendsBar.js +++ b/assets/js/components/friends/friendsBar.js @@ -110,42 +110,8 @@ ComunicWeb.components.friends.bar = { //Show each friend for(i in friendsList){ - - //Extract friend ID - var friendID = friendsList[i].ID_friend; - - //Create a row - var friendRow = createElem("tr", listFriendsElem); - - //Add user avatar - var imageRow = createElem("td", friendRow); - var imageElem = createElem("img", imageRow); - imageElem.src = usersInfos["user-"+friendID].accountImage; - imageElem.className = "account-image"; - - //Add user name - var nameRow = createElem("td", friendRow); - nameRow.innerHTML = usersInfos["user-"+friendID].firstName + " " + usersInfos["user-"+friendID].lastName; - - //Add user login status - var statusRow = createElem("td", friendRow); - statusRow.className = "statusRow"; - - //Check if user is online or not - var currentTime = ComunicWeb.common.date.time(); - var timeDifference = currentTime - friendsList[i].time_last_activity; - - if(timeDifference < 30){ - //User is logged in - var iconsStats = createElem("i", statusRow); - iconsStats.className = "fa fa-fw fa-circle"; - iconsStats.style.color = "green"; - } - else { - //User isn't logged in - var logoutTime = createElem("small", statusRow); - logoutTime.innerHTML = ComunicWeb.common.date.diffToStr(timeDifference); - } + //Show informations about the friend + ComunicWeb.components.friends.bar.showFriendInfos(usersInfos["user-"+friendsList[i].ID_friend], friendsList[i], listFriendsElem); } //Enable slimscroll @@ -157,6 +123,80 @@ ComunicWeb.components.friends.bar = { }); }, + /** + * Show a friend informations + * + * @param {Object} userInfos Informations about the user + * @param {Object} friendshipInfos Informations about the friendship + * @param {HTMLElement} listFriendsElem The target for the friends list + * @return {Boolean} True for a success + */ + showFriendInfos: function(userInfos, friendshipInfos, listFriendsElem){ + //Extract friend ID + var friendID = friendshipInfos.ID_friend; + + //Create a row + var friendRow = createElem("tr", listFriendsElem); + + //Add user avatar + var imageRow = createElem("td", friendRow); + var imageElem = createElem("img", imageRow); + imageElem.src = userInfos.accountImage; + imageElem.className = "account-image"; + + //Add user name + var nameRow = createElem("td", friendRow); + nameRow.innerHTML = userInfos.firstName + " " + userInfos.lastName; + + //Add user login status + var statusRow = createElem("td", friendRow); + statusRow.className = "statusRow"; + + //Check if the user was accepted or not + if(friendshipInfos.accepted == "1"){ + + //Check if user is online or not + var currentTime = ComunicWeb.common.date.time(); + var timeDifference = currentTime - friendshipInfos.time_last_activity; + + if(timeDifference < 30){ + //User is logged in + var iconsStats = createElem("i", statusRow); + iconsStats.className = "fa fa-fw fa-circle"; + iconsStats.style.color = "green"; + } + else { + //User isn't logged in + var logoutTime = createElem("small", statusRow); + logoutTime.innerHTML = ComunicWeb.common.date.diffToStr(timeDifference); + } + } + else { + //We offer user to accept invitation + var acceptButton = createElem("button", statusRow); + acceptButton.className = "btn btn-xs btn-success"; + acceptButton.innerHTML = ""; + acceptButton.onclick = function(){ + ComunicWeb.components.friends.bar.processFriendShipRequest(friendID, true, statusRow) + }; + + //Insert space + var space = createElem("span", statusRow); + space.innerHTML = " "; + + //But he can also refuse it + var refuseButton = createElem("button", statusRow); + refuseButton.className = "btn btn-xs btn-danger"; + refuseButton.innerHTML = ""; + refuseButton.onclick = function(){ + ComunicWeb.components.friends.bar.processFriendShipRequest(friendID, false, statusRow) + }; + } + + //Sucess + return true; + }, + /** * Toogle show / hide friends bar * @@ -180,6 +220,31 @@ ComunicWeb.components.friends.bar = { //Success return true; - } + }, + + /** + * Accept / Deny a friendship request + * + * @param {Integer} friendID The ID of the friend to accept / rejet + * @param {Boolean} accepted True if friendship is accepted, false else + * @param {HTMLElement} statusRow The parent node of friendship area + * @return {Boolean} True for a success + */ + processFriendShipRequest: function(friendID, accepted, statusRow){ + + //Log action + ComunicWeb.debug.logMessage("Process friendship request "+friendID); + + //Change statusRow style + if(accepted) + statusRow.innerHTML = "Accepted"; + else + statusRow.innerHTML = "Refused"; + + //Perform an API request + ComunicWeb.components.friends.list.respondRequest(friendID, accepted); + + }, + } \ No newline at end of file diff --git a/assets/js/components/friends/friendsList.js b/assets/js/components/friends/friendsList.js index b16e901d..fc6b02e6 100644 --- a/assets/js/components/friends/friendsList.js +++ b/assets/js/components/friends/friendsList.js @@ -61,5 +61,32 @@ ComunicWeb.components.friends.list = { get: function(){ //Return the list return this.__list; - } + }, + + /** + * Respond a friendship request + * + * @param {Integer} friendID The friend ID to respond + * @param {Boolean} accept Specify if the request was accepted or not + * @param {Function} afterResponse Specify an action to do next + * @return {Boolean} True for a success + */ + respondRequest: function(friendID, accept, afterResponse){ + //Prepare the API request + var apiURI = "friends/respondRequest" + var params = { + "friendID": friendID, + }; + + if(accept) + params.accept = "true"; + else + params.accept = "false"; + + //Process request + ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, afterResponse); + + //Success + return true; + }, }; \ No newline at end of file