-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusers.js
98 lines (92 loc) · 3.31 KB
/
users.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
$(document).ready(function() {
var path = document.URL.split("/");
id = path[path.length - 1];
if (path[path.length -2] == "users" && id > 0)
{
$userInfo = document.getElementById("user-id");
$button = document.getElementById("btnFriend");
$we_are_friend = document.getElementById("we_are_friend").className;
$cardFriend = document.getElementById("cardContentFriend");
$userName = document.getElementById("userName");
currentUserId = $userInfo.className;
function addFriend() {
var xmlhttp = new XMLHttpRequest();
if ($listFriends = document.getElementById("listFriends"))
var issetListFriend = 1;
else
var issetListFriend = 0;
xmlhttp.onreadystatechange = function() {
if (xmlhttp.status == 200 && xmlhttp.readyState == 4)
{
if (issetListFriend == 0)
{
var node = document.createElement("ul");
node.className = "collection";
node.id = "listFriends";
$cardFriend.appendChild(node);
console.log($cardFriend);
}
$listFriends = document.getElementById("listFriends");
redButton();
$listFriends.innerHTML += '<li id="newFriend" class="collection-item blue-grey-text darken-1"><div>' + $userName.innerHTML + '<a href="/users/' + id + '" class="secondary-content light-blue-text darken-4"><i class="material-icons">send</i></a></div></li>';
$button.onclick = removeFriend;
}
}
xmlhttp.open("POST", "http://localhost:3000/users/" + id, true);
xmlhttp.send();
}
function removeFriend() {
var xmlhttp = new XMLHttpRequest();
if ($listFriends = document.getElementById("listFriends"))
var issetListFriend = 1;
else
var issetListFriend = 0;
xmlhttp.onreadystatechange = function() {
if (xmlhttp.status == 200 && xmlhttp.readyState == 4)
{
if ($listFriends.children.length == 1)
$listFriends.parentNode.removeChild($listFriends);
else
{
if(document.getElementById("newFriend"))
$listFriends.removeChild(document.getElementById("newFriend"));
for (i = 0; i < $listFriends.children.length; i++)
{
var toto = $listFriends.children[i].childNodes[1].textContent.indexOf($userName.innerHTML);
if (toto != -1)
$listFriends.removeChild($listFriends.children[i]);
}
}
blueButton();
$button.onclick = addFriend;
}
}
xmlhttp.open("DELETE", "http://localhost:3000/users/" + id, true);
xmlhttp.send();
}
function redButton() {
$('#btnFriend').removeClass('light-blue');
$('#btnFriend').removeClass('accent-3');
$('#btnFriend').addClass('red');
$('#btnFriend').addClass('darken-1');
$button.innerHTML = "Remove";
}
function blueButton() {
$('btnFriend').removeClass('red');
$('#btnFriend').removeClass('darken-1');
$('#btnFriend').addClass('light-blue');
$('#btnFriend').addClass('accent-3');
$button.innerHTML = "Add Friend";
}
if ($we_are_friend == "false")
{
blueButton();
$button.onclick = addFriend;
}
else
{
redButton();
$button.onclick = removeFriend;
}
}
});