From a49e068637c7e08bc98f5cb6659ae095c13f176d Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 23 Dec 2017 17:46:55 +0100 Subject: [PATCH] Can update following state --- assets/js/components/friends/friendsList.js | 27 ++++++++++++ assets/js/pages/userPage/friendshipStatus.js | 43 ++++++++++++++++++++ assets/js/pages/userPage/profileInfos.js | 14 +++++++ 3 files changed, 84 insertions(+) diff --git a/assets/js/components/friends/friendsList.js b/assets/js/components/friends/friendsList.js index e284fa71..1fdac5bf 100644 --- a/assets/js/components/friends/friendsList.js +++ b/assets/js/components/friends/friendsList.js @@ -63,6 +63,33 @@ ComunicWeb.components.friends.list = { return this.__list; }, + /** + * Update the follow status of a user + * + * @param {Integer} friendID The friend ID to respond + * @param {Boolean} follow Specify whether the user has to be followed or not + * @param {Function} callback Specify an action to do next + * @return {Boolean} True for a success + */ + setFollowing: function(friendID, follow, callback){ + //Prepare the API request + var apiURI = "friends/setFollowing" + var params = { + "friendID": friendID, + }; + + if(follow == true) + params.follow = "true"; + else + params.follow = "false"; + + //Process request + ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback); + + //Success + return true; + }, + /** * Respond a friendship request * diff --git a/assets/js/pages/userPage/friendshipStatus.js b/assets/js/pages/userPage/friendshipStatus.js index afc56792..af19f27c 100644 --- a/assets/js/pages/userPage/friendshipStatus.js +++ b/assets/js/pages/userPage/friendshipStatus.js @@ -160,6 +160,49 @@ ComunicWeb.pages.userPage.friendshipStatus = { } + //Offer user to follow other user + else { + + //Setup button + var followButton = createElem2({ + appendTo: target, + type: "button", + class: "btn btn-primary btn-block", + }); + + if(response.following){ + followButton.innerHTML = "Following"; + followButton.setAttribute("data-following", "true"); + } + else { + followButton.innerHTML = "Follow"; + followButton.setAttribute("data-following", "false"); + } + + //Make the follow button live + followButton.onclick = function(){ + + //Lock button + this.disabled = true; + + //Check if the user has to be followed or not (reverse current state) + var follow = this.getAttribute("data-following") == "false"; + + ComunicWeb.components.friends.list.setFollowing(userID, follow, function(response){ + + //Check for errors + if(response.error){ + ComunicWeb.common.notificationSystem.showNotification("An error occured while trying to update following status !"); + } + + //Reload this component + ComunicWeb.pages.userPage.friendshipStatus.display(userID, target); + + }); + } + + } + }); } diff --git a/assets/js/pages/userPage/profileInfos.js b/assets/js/pages/userPage/profileInfos.js index c0ef156b..e371977b 100644 --- a/assets/js/pages/userPage/profileInfos.js +++ b/assets/js/pages/userPage/profileInfos.js @@ -59,5 +59,19 @@ ComunicWeb.pages.userPage.profileInfos = { class: "profile-username text-center", innerHTML: infos.firstName + " " + infos.lastName }); + + //Add user status informations (if required) + if(signed_in()){ + if(userID() != infos.userID){ + var userStatus = createElem2({ + appendTo: boxBody, + type: "div", + innerHTML: "Loading...", + }); + userStatus.style.textAlign = "center"; + ComunicWeb.pages.userPage.friendshipStatus.display(infos.userID, userStatus); + } + } + } }; \ No newline at end of file