Forum Discussion
djondinium
3 years agoExpert
Hi,
We have a bunch of functions so I can't edit them to make more user friendly, but should give you enough. We separated all the functions to do separate tasks (join open node, send request to join closed node, accept invite, decline invite + leave group (there isn't functionality to reject an invite to groups so we went to accept invite and leave group immediately).
function joinOpenNode(id, div, href, title) {
$("#" + div).html("<span id='" + div + "' class='df df-justify-center'><img src='${settings.paths.bucket}/Assets/others/spinner.svg' class='group-button-loading'></span>");
$.ajax({
url: "/api/2.0/nodes/grouphub:"+div+"/membership_requests",
headers: {
accept: 'application/json',
'content-type': 'application/json'
},
type: "POST",
data: '{"data":{"type":"membership_request"}}',
success: function(result) {
$("#" + div).remove();
window.location.href = href;
},
error: function(result) {
if(result.responseText.split('"message":"')[1].split('","data"')[0].indexOf("The specified node is not a membership node") == 0) {
createAndOpenModal(id, "${text.format('groups.modals-open-error-join-hidden')}");
$("#" + div).remove();
}
else if(result.responseText.split('"message":"')[1].split('","data"')[0].indexOf("error.api.membership.join.user_not_found_id") == 0) {
createAndOpenModal(id, "${text.format('groups.modals-open-error-join-anonymous')}");
$("#" + div).remove();
}
else {
createAndOpenModal(id, "${text.format('groups.modals-open-error-join-default')}");
}
},
async: true,
cache: false
});
}
function joinClosedNode(id, div, href, title) {
$("#" + div).html("<span id='" + div + "' class='df df-justify-center'><img src='${settings.paths.bucket}/Assets/others/spinner.svg' class='group-button-loading'></span>");
$.ajax({
url: '/api/2.0/nodes/grouphub:' + div + '/membership_requests',
method: "POST",
headers: {
accept: 'application/json',
'content-type': 'application/json'
},
data: '{"data":{"type":"membership_request"}}',
success: function(request) {
createAndOpenModal(id, "${text.format('groups.modals-closed-success', '"+ title +"')}");
$("#" + div).html('<span class="cg-group-info-join-requested">${text.format("groups.tile-buttons-join-closed-node-requested")}</span>');
},
error: function(request) {
if(request.responseText.split('"message":"')[1].split('","data"')[0].indexOf("User has already requested to join node") == 0) {
createAndOpenModal(id, "${text.format('groups.modals-closed-error-already-requested')}");
$("#" + div).remove();
}
else if(request.responseText.split('"message":"')[1].split('","data"')[0].indexOf("The specified node is not a membership node") == 0) {
createAndOpenModal(id, "${text.format('groups.modals-open-error-join-hidden')}");
$("#" + div).remove();
}
else if(request.responseText.split('"message":"')[1].split('","data"')[0].indexOf("error.api.membership.join.user_not_found_id") == 0) {
createAndOpenModal(id, "${text.format('groups.modals-closed-error-anonymous')}");
$("#" + div).remove();
}
else {
createAndOpenModal(id, "${text.format('groups.modals-closed-error-default', '"+ title +"')}");
$("#" + div).html("<span onclick=\"joinClosedNode('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-up-black'>${text.format('groups.tile-buttons-join-closed-node')}</span>");
}
},
async: true,
cache: false
});
}
function acceptInvite(id, div, href, title) {
$("#" + div).html("<span id='" + div + "' class='df df-justify-center'><img src='${settings.paths.bucket}/Assets/others/spinner.svg' class='group-button-loading'></span>");
$.ajax({
url: '/api/2.0/nodes/grouphub:' + div + '/invites',
method: "PUT",
headers: {
accept: 'application/json',
'content-type': 'application/json'
},
data: '{"invite":{"accept":true}}',
success: function(request) {
window.location.href = href;
$("#" + div).remove();
},
error: function(request) {
if(request.responseText.split('"message":"')[1].split('","data"')[0].indexOf("Invite not found for the specified user and node") == 0) {
createAndOpenModal(id, "${text.format('groups.modals-accept-invite-no-invite')}");
$("#" + div).html("<span onclick=\"declineInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-link'>${text.format('groups.tile-buttons-invite-decline')}</span><span onclick=\"acceptInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-up-black'>${text.format('groups.tile-buttons-invite-accept')}</span>");
}
else {
createAndOpenModal(id, "${text.format('groups.modals-accept-invite-error-default')}");
$("#" + div).html("<span onclick=\"declineInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-link'>${text.format('groups.tile-buttons-invite-decline')}</span><span onclick=\"acceptInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-up-black'>${text.format('groups.tile-buttons-invite-accept')}</span>");
}
},
async: true,
cache: false
});
}
function declineInvite(id, div, href, title) {
$("#" + div).html("<span id='" + div + "' class='df df-justify-center'><img src='${settings.paths.bucket}/Assets/others/spinner.svg' class='group-button-loading'></span>");
$.ajax({
url: '/api/2.0/nodes/grouphub:' + div + '/invites',
method: "PUT",
headers: {
accept: 'application/json',
'content-type': 'application/json'
},
data: '{"invite":{"accept":true}}',
success: function(request) {
leaveGroup(id, div, href, title);
},
error: function(request) {
if(request.responseText.split('"message":"')[1].split('","data"')[0].indexOf("Invite not found for the specified user and node") == 0) {
createAndOpenModal(id, "${text.format('groups.modals-decline-invite-no-invite')}");
$("#" + div).html("<span onclick=\"declineInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-link'>${text.format('groups.tile-buttons-invite-decline')}</span><span onclick=\"acceptInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-up-black'>${text.format('groups.tile-buttons-invite-accept')}</span>");
}
else {
createAndOpenModal(id, "${text.format('groups.modals-decline-invite-error-default')}");
$("#" + div).html("<span onclick=\"declineInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-link'>${text.format('groups.tile-buttons-invite-decline')}</span><span onclick=\"acceptInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-up-black'>${text.format('groups.tile-buttons-invite-accept')}</span>");
}
},
async: true,
cache: false
});
}
function leaveGroup(id, div, href, title) {
$.ajax({
url: "/api/2.0/nodes/grouphub:"+div+"/membership_requests",
headers: {
accept: 'application/json',
'content-type': 'application/json'
},
type: "DELETE",
data: '{"data":{"type":"membership_request"}}',
success: function(result) {
if($("#" + div).parent().parent().parent().find(".custom-groups-group").length == 1) {
$("#" + div).parent().parent().parent().parent().remove();
}
else {
$("#" + div).parent().parent().remove();
}
createAndOpenModal(id, "${text.format('groups.modals-decline-invite-success')}");
},
error: function(result) {
createAndOpenModal(id, "${text.format('groups.modals-decline-invite-error-default')}");
$("#" + div).html("<span onclick=\"declineInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-link'>${text.format('groups.tile-buttons-invite-decline')}</span><span onclick=\"acceptInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-up-black'>${text.format('groups.tile-buttons-invite-accept')}</span>");
},
async: true,
cache: false
});
}
Hope it helps. It does look extremely chaotic xD
Related Content
- 5 years ago
- 3 years ago