diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index 8c12115f..cbdba9a9 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -575,6 +575,13 @@ var ComunicWeb = { */ utils: { //TODO : implement + }, + + /** + * Friends actions + */ + actions: { + //TODO : implement } }, diff --git a/assets/js/components/friends/actions.js b/assets/js/components/friends/actions.js new file mode 100644 index 00000000..f4cd4ee3 --- /dev/null +++ b/assets/js/components/friends/actions.js @@ -0,0 +1,44 @@ +/** + * Friends actions + * + * @author Pierre HUBERT + */ + +ComunicWeb.components.friends.actions = { + + /** + * Refresh informations of a single personnal friend + * + * @param {number} friendID The ID of the target friend + * @param {HTMLElement} target The target element + */ + refresh_single_personnal: function(friendID, target){ + + //Get informations about the friendship + ComunicWeb.components.friends.interface.get_single_friend(friendID, function(r){ + + //Check for errors + if(r.error){ + ComunicWeb.common.notificationSystem.showNotification("Could not get informations about a friendship !", "danger"); + return; + } + + //Get informations about the user + ComunicWeb.user.userInfos.getUserInfos(friendID, function(user){ + + //Check for errors + if(user.error){ + ComunicWeb.common.notificationSystem.showNotification("Could get informations about a friend !", "danger"); + return; + } + + //Refresh the UI + ComunicWeb.components.friends.ui.show_personnal_friend(target, r, user); + + }); + + }); + + } + +}; \ No newline at end of file diff --git a/assets/js/components/friends/interface.js b/assets/js/components/friends/interface.js index 08e32354..6b7e29b4 100644 --- a/assets/js/components/friends/interface.js +++ b/assets/js/components/friends/interface.js @@ -6,6 +6,25 @@ ComunicWeb.components.friends.interface = { + /** + * Get single friend informations + * + * @param {number} friendID The ID of the target friend + * @param {function} callback Callback function + */ + get_single_friend: function(friendID, callback){ + + //Prepare the API request + var apiURI = "friends/get_single_infos"; + var params = { + friendID: friendID + }; + + //Perform API request + ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback); + + }, + /** * Get the list of friends of the current user * diff --git a/assets/js/components/friends/ui.js b/assets/js/components/friends/ui.js index 98249a7e..4879798a 100644 --- a/assets/js/components/friends/ui.js +++ b/assets/js/components/friends/ui.js @@ -109,9 +109,8 @@ ComunicWeb.components.friends.ui = { } else { - //Display again the friend - friend.accepted = 1; - ComunicWeb.components.friends.ui.show_personnal_friend(friendContener, friend, user); + //Update friendship informations + ComunicWeb.components.friends.actions.refresh_single_personnal(friendID, friendContener); } }); @@ -125,8 +124,45 @@ ComunicWeb.components.friends.ui = { else { //Display following state + var followButton = createElem2({ + appendTo: friendContener, + type: "button", + class: "btn btn-primary" + }); + + if(friend.following == 0){ + followButton.innerHTML = "Follow"; + followButton.setAttribute("data-set-following", "true"); + } + else { + followButton.innerHTML = "Following"; + followButton.setAttribute("data-set-following", "false"); + } + + add_space(friendContener); //Check if the user can post text on user page + followButton.onclick = function(){ + + //Check if the request is to follow or not the user + var follow = this.getAttribute("data-set-following") == "true"; + + //Lock button + followButton.disabled = true; + + //Perform callback action + ComunicWeb.components.friends.list.setFollowing(friendID, follow, function(r){ + + //Check for errors + if(r.error){ + ComunicWeb.common.notificationSystem.showNotification("Could not update follow state !", "danger"); + } + + //Update friendship informations + ComunicWeb.components.friends.actions.refresh_single_personnal(friendID, friendContener); + }); + + } } diff --git a/system/config/dev.config.php b/system/config/dev.config.php index 0bbb62d3..d58813f1 100644 --- a/system/config/dev.config.php +++ b/system/config/dev.config.php @@ -218,6 +218,7 @@ class Dev { "js/components/friends/listModal.js", "js/components/friends/interface.js", "js/components/friends/utils.js", + "js/components/friends/actions.js", //Private conversations "js/components/conversations/manager.js",