-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathmain.js
More file actions
167 lines (122 loc) · 3.74 KB
/
main.js
File metadata and controls
167 lines (122 loc) · 3.74 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
$(function() {
$("form").on("submit", function(e) {
e.preventDefault();
topStories()
signUpUser()
// var $newTitle = $('#newTitle').val()
// var $newURL = $('#urlToAdd').val()
// var icon = "<span class=\"glyphicon glyphicon-star-empty\" aria-hidden=\"true\"></span>"
// var $newLi = $("<li>" + icon + " " + $newTitle + " " + $newURL + "</li>");
// $("ol").append($newLi);
// $newTitle.val("");
// $newURL.val("");
});
$('ol').click(function(event){
$(event.target).toggleClass('glyphicon-star-empty glyphicon-star');
addToFavorites()
});
$("btnFavs").on("click", function(e) {
e.preventDefault();
addToFavorites();
retrieveFavorites();
} )
//-----------------------------------------------------------------------
// Create the Top Stories List
var icon = "<span class=\"glyphicon glyphicon-star-empty\" aria-hidden=\"true\"></span>"
function topStories() {
$.get( "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty", function( data ) {
var arrIDs = [];
//create array of Top 20 story IDs, that push a complete url to the array
for (var i = 0; i < 20; i++) {
arrIDs.push("https://hacker-news.firebaseio.com/v0/item/" + data[i] + ".json")
}
//use the url's from above to run another get funtion to get the details for each story ID
for (var i = 0; i < arrIDs.length; i++) {
$.get(arrIDs[i])
.then(function (data) {
// $('ol').append("<li>" + icon + "<h5>" + data.title + "</h5> (" + data.url + ") </li>")
$('ol').append("<li>" + icon + "<h5>" + data.title + "</h5> </li>")
})
.fail(function(err) {
console.log(err);
});
}
})
}
var $email = $('#email').val();
// console.log($email);
var $password = $('#password').val();
// console.log($password);
var authKy = "";
function signUpUser(email, password) {
$.ajax({
method: "POST",
headers: {
"Content-Type": "application/json"
},
url: "https://hn-favorites.herokuapp.com/signup",
data: JSON.stringify({
email: $email,
password: $password
})
})
.then(function(data) { authKy = (data.auth_token) })
.fail(err => console.warn(err))
}
function logInUser(email, password) {
$.ajax({
method: "POST",
headers: {
"Content-Type": "application/json"
},
url: "https://hn-favorites.herokuapp.com/signup",
data: JSON.stringify({
email: $email,
password: $password
})
})
.then(function(data) { authKy = (data.auth_token)})
.fail(err => console.warn(err))
}
//If a story is clicked, do addToFavorites() function that will add it to the favorites list
function addToFavorites() {
$.ajax({
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": authKy
},
url: "https://hn-favorites.herokuapp.com/stories.json",
data: JSON.stringify({
hacker_news_story: {
by: "aj",
story_id: "123",
title: "Test story",
url: "abc.com"
}
})
})
.then(function(data) { console.log(data) })
.fail(err => console.warn(err))
}
function retrieveFavorites() {
$.ajax({
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": authKy
},
url: "https://hn-favorites.herokuapp.com/stories.json",
data: JSON.stringify({
hacker_news_story: {
by: "aj",
story_id: "123",
title: "Test story",
url: "abc.com"
}
})
})
.then(function(data) { console.log("fav's response: " + data) })
.fail(err => console.warn(err))
}
});