ComunicWeb/assets/js/pages/userPage/friendshipStatus.js

212 lines
5.3 KiB
JavaScript
Raw Permalink Normal View History

2017-12-20 18:18:18 +00:00
/**
* Handle the update of the friendship status
*
* @author Pierre HUBERT
*/
ComunicWeb.pages.userPage.friendshipStatus = {
/**
* Display the friendship status
*
* @param {Integer} userID The ID of the target user
* @param {HTMLElement} target The target element
*/
display: function(userID, target){
//Get the current status of the friendship
ComunicWeb.components.friends.list.getStatus(userID, function(response){
//Empty the target area
emptyElem(target);
target.innerHTML = "";
//Check for errors
if(response.error){
2018-08-04 07:32:40 +00:00
message = ComunicWeb.common.messages.createCalloutElem(
lang("user_page_friendship_section_err_load_title"),
lang("user_page_friendship_section_err_load_message"), "danger");
2017-12-20 18:18:18 +00:00
target.appendChild(message);
return;
}
//Check if the user has received a friendship request
if(response.received_request){
//Offer the user to reject a frienship request
var rejectRequest = createElem2({
2017-12-20 18:18:18 +00:00
appendTo: target,
type: "button",
class: "btn btn-xs btn-danger",
2018-08-04 07:32:40 +00:00
innerHTML: lang("user_page_friendship_section_reject_request")
2017-12-20 18:18:18 +00:00
});
createElem2({
appendTo: target,
type: "span",
innerHTML: " ",
})
//Offer the user to accept a frienship request
var acceptRequest = createElem2({
2017-12-20 18:18:18 +00:00
appendTo: target,
type: "button",
class: "btn btn-xs btn-success",
2018-08-04 07:32:40 +00:00
innerHTML: lang("user_page_friendship_section_accept_request")
2017-12-20 18:18:18 +00:00
});
//Prepare the buttons
acceptRequest.setAttribute("data-accept", "true");
rejectRequest.setAttribute("data-accept", "false");
//Setup the action
var respondRequest = function(){
//Lock the buttons
acceptRequest.disabled = true;
rejectRequest.disabled = true;
//Get the status of the request
var accept = this.getAttribute("data-accept") == "true";
//Perform the action
ComunicWeb.components.friends.list.respondRequest(userID, accept, function(response){
//Unlock the buttons
acceptRequest.disabled = false;
rejectRequest.disabled = false;
//Check for errors
if(response.error){
2018-08-04 07:32:40 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("user_page_friendship_section_err_update_request_status"),
"danger", 5);
}
else {
//Reopen user page
2018-07-14 12:18:21 +00:00
openUserPageFromID(userID);
}
});
}
acceptRequest.onclick = respondRequest;
rejectRequest.onclick = respondRequest;
2017-12-20 18:18:18 +00:00
}
//Check if user has sent a friendship request
else if(response.sent_request){
//Offer the user to cancel a frienship request
2017-12-23 08:14:34 +00:00
var cancelRequest = createElem2({
2017-12-20 18:18:18 +00:00
appendTo: target,
type: "button",
class: "btn btn-xs btn-danger",
2018-08-04 07:32:40 +00:00
innerHTML: lang("user_page_friendship_section_cancel_request")
2017-12-20 18:18:18 +00:00
});
2017-12-23 08:14:34 +00:00
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){
2018-08-04 07:32:40 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("user_page_friendship_section_err_remove_request"));
2017-12-23 08:14:34 +00:00
}
//Reload this component
ComunicWeb.pages.userPage.friendshipStatus.display(userID, target);
});
}
2017-12-20 18:18:18 +00:00
}
2017-12-23 08:14:34 +00:00
//Display send request message
else if(response.are_friend == false) {
2017-12-20 18:18:18 +00:00
//Offer the user to send a frienship request
2017-12-23 08:14:34 +00:00
var sendRequestButton = createElem2({
2017-12-20 18:18:18 +00:00
appendTo: target,
type: "button",
class: "btn btn-xs btn-primary",
2018-08-04 07:32:40 +00:00
innerHTML: lang("user_page_friendship_section_send_request")
2017-12-20 18:18:18 +00:00
});
2017-12-23 08:14:34 +00:00
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){
2018-08-04 07:32:40 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("user_page_friendship_section_err_send_request"));
2017-12-23 08:14:34 +00:00
}
//Reload this component
ComunicWeb.pages.userPage.friendshipStatus.display(userID, target);
});
}
2017-12-20 18:18:18 +00:00
}
2017-12-23 16:46:55 +00:00
//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){
2018-08-04 07:32:40 +00:00
followButton.innerHTML = lang("user_page_friendship_section_following");
2017-12-23 16:46:55 +00:00
followButton.setAttribute("data-following", "true");
}
else {
2018-08-04 07:32:40 +00:00
followButton.innerHTML = lang("user_page_friendship_section_follow");
2017-12-23 16:46:55 +00:00
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){
2018-08-04 07:32:40 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("user_page_friendship_section_err_update_following_status"));
2017-12-23 16:46:55 +00:00
}
//Reload this component
ComunicWeb.pages.userPage.friendshipStatus.display(userID, target);
});
}
}
2017-12-20 18:18:18 +00:00
});
}
}