From e89ae955c2a5f7e1f35746a17df11b41541634d4 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 23 Dec 2017 09:14:34 +0100 Subject: [PATCH] Can create friendship relations --- assets/js/components/friends/friendsList.js | 42 +++++++++++++++++ assets/js/pages/userPage/friendshipStatus.js | 48 ++++++++++++++++++-- 2 files changed, 86 insertions(+), 4 deletions(-) diff --git a/assets/js/components/friends/friendsList.js b/assets/js/components/friends/friendsList.js index 2314c683..e284fa71 100644 --- a/assets/js/components/friends/friendsList.js +++ b/assets/js/components/friends/friendsList.js @@ -90,6 +90,48 @@ ComunicWeb.components.friends.list = { return true; }, + /** + * Send (create) a friendship request + * + * @param {Integer} friendID The friend ID to respond + * @param {Function} afterResponse Specify an action to do next + * @return {Boolean} True for a success + */ + sendRequest: function(friendID, afterResponse){ + //Prepare the API request + var apiURI = "friends/sendRequest" + var params = { + "friendID": friendID, + }; + + //Process request + ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, afterResponse); + + //Success + return true; + }, + + /** + * Remove (ancel) a friendship request + * + * @param {Integer} friendID The target friendID + * @param {Function} afterResponse Specify an action to do next + * @return {Boolean} True for a success + */ + removeRequest: function(friendID, afterResponse){ + //Prepare the API request + var apiURI = "friends/removeRequest" + var params = { + "friendID": friendID, + }; + + //Process request + ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, afterResponse); + + //Success + return true; + }, + /** * Get the current status of a friendship relation * diff --git a/assets/js/pages/userPage/friendshipStatus.js b/assets/js/pages/userPage/friendshipStatus.js index 46b64011..afc56792 100644 --- a/assets/js/pages/userPage/friendshipStatus.js +++ b/assets/js/pages/userPage/friendshipStatus.js @@ -98,26 +98,66 @@ ComunicWeb.pages.userPage.friendshipStatus = { else if(response.sent_request){ //Offer the user to cancel a frienship request - createElem2({ + var cancelRequest = createElem2({ appendTo: target, type: "button", class: "btn btn-xs btn-danger", innerHTML: "Cancel request" }); + cancelRequest.onclick = function(){ + + //Lock button + this.disabled = true; + + //Send the request + ComunicWeb.components.friends.list.removeRequest(userID, function(response){ + + //Check for errors + if(response.error){ + ComunicWeb.common.notificationSystem.showNotification("An error occured while trying to remove the request !"); + } + + //Reload this component + ComunicWeb.pages.userPage.friendshipStatus.display(userID, target); + + }); + + } + } - //Display default message - else { + //Display send request message + else if(response.are_friend == false) { //Offer the user to send a frienship request - createElem2({ + var sendRequestButton = createElem2({ appendTo: target, type: "button", class: "btn btn-xs btn-primary", innerHTML: "Send request" }); + sendRequestButton.onclick = function(){ + + //Lock button + this.disabled = true; + + //Send the request + ComunicWeb.components.friends.list.sendRequest(userID, function(response){ + + //Check for errors + if(response.error){ + ComunicWeb.common.notificationSystem.showNotification("An error occured while trying to send the request !"); + } + + //Reload this component + ComunicWeb.pages.userPage.friendshipStatus.display(userID, target); + + }); + + } + } });