This repository has been archived by the owner on Jan 27, 2021. It is now read-only.
forked from johnshew/decoded-episode2
-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.ts
78 lines (72 loc) · 1.85 KB
/
client.ts
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
declare var $:any; //jQuery shortcut
var selectedPackage = null;
function renderContributors(contributors) {
$("#contributors_container").show();
$("#contributors_title span").html(selectedPackage);
var $container = $("#contributors tbody");
$container.html("");
$.each(contributors, function(idx, contributor) {
$container.append(
"<tr>" +
"<td><img src='" + contributor.avatar_url + "' /></td>" +
"<td>" + contributor.login + "</td>" +
"<td>" + contributor.contributions + "</td>" +
"</tr>");
});
}
function renderRepos(repos) {
var $container = $("#repos tbody");
$container.html("");
$.each(repos, function(idx, repo) {
var btnClass = "btn-default";
if(repo.favorite) {
btnClass = "btn-success";
}
$container.append(
"<tr>" +
'<td><a data-repo="' + repo.name + '" href="#" class="favorite btn ' + btnClass + '"><span class="glyphicon glyphicon-star"></span></a></td>' +
"<td><a class='repo' href='#'>" + repo.name + "</a></td>" +
"</tr>"
);
});
$(".repo").click(function(e) {
var userName = $(e.target).html();
selectedPackage = userName;
$.ajax({
url: "/contributors/" + userName,
success: function(result) {
renderContributors(result);
}
});
});
$(".favorite").click(function(e) {
e.preventDefault();
var $button = $(e.target).closest("a");
var isFavorite = $button.hasClass("btn-default");
$.ajax({
url: "/favorite/" + $button.attr("data-repo"),
type: 'post',
data: {
isFavorite: isFavorite
},
success: function(data) {
if(isFavorite) {
$button.removeClass("btn-default").addClass("btn-success");
} else {
$button.removeClass("btn-success").addClass("btn-default");
}
}
});
});
}
function getRepos() {
$.ajax({
url: "/packages",
success: function(result) {
renderRepos(result);
}
});
}
$(document).ready(function() {
getRepos();
});