forked from thinkful-fewd/stackerAJAX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
147 lines (122 loc) · 4.15 KB
/
app.js
File metadata and controls
147 lines (122 loc) · 4.15 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
$(document).ready( function() {
$('.unanswered-getter').submit( function(event){
// zero out results if previous search has run
$('.results').html('');
// get the value of the tags the user submitted
var tags = $(this).find("input[name='tags']").val();
getUnanswered(tags);
});
$('.inspiration-getter').submit( function(event){
// zero out results if previous search has run
$('.results').html('');
// get the value of the tags the user submitted
var tags = $(this).find("input[name='answerers']").val();
getTopAnswerers(tags);
});
});
// this function takes the question object returned by StackOverflow
// and creates new result to be appended to DOM
var showQuestion = function(question) {
// clone our result template code
var result = $('.templates .question').clone();
// Set the question properties in result
var questionElem = result.find('.question-text a');
questionElem.attr('href', question.link);
questionElem.text(question.title);
// set the date asked property in result
var asked = result.find('.asked-date');
var date = new Date(1000*question.creation_date);
asked.text(date.toString());
// set the #views for question property in result
var viewed = result.find('.viewed');
viewed.text(question.view_count);
// set some properties related to asker
var asker = result.find('.asker');
asker.html('<p>Name: <a target="_blank" href=http://stackoverflow.com/users/' + question.owner.user_id + ' >' +
question.owner.display_name +
'</a>' +
'</p>' +
'<p>Reputation: ' + question.owner.reputation + '</p>'
);
return result;
};
var showAnswerer = function(answer) {
var result = $('.templates .topanswerer').clone();
var postCountElem = result.find('.post-count');
postCountElem.text(answer.post_count);
var scoreElem = result.find('.score');
scoreElem.text(answer.score);
var answerer = result.find('.answerer');
answerer.html('<p>Name: <a target="_blank" href=http://stackoverflow.com/users/' + answer.user.user_id + ' >' +
answer.user.display_name +
'</a>' +
'</p>' +
'<p>Reputation: ' + answer.user.reputation + '</p>'
);
return result;
};
// this function takes the results object from StackOverflow
// and creates info about search results to be appended to DOM
var showSearchResults = function(query, resultNum) {
var results = resultNum + ' results for <strong>' + query;
return results;
};
// takes error string and turns it into displayable DOM element
var showError = function(error){
var errorElem = $('.templates .error').clone();
var errorText = '<p>' + error + '</p>';
errorElem.append(errorText);
};
// takes a string of semi-colon separated tags to be searched
// for on StackOverflow
var getUnanswered = function(tags) {
// the parameters we need to pass in our request to StackOverflow's API
var request = { tagged: tags,
site: 'stackoverflow',
order: 'desc',
sort: 'creation'
};
var result = $.ajax({
url: "http://api.stackexchange.com/2.2/questions/unanswered",
data: request,
dataType: "jsonp",
type: "GET",
})
.done(function(result){
var searchResults = showSearchResults(request.tagged, result.items.length);
$('.search-results').html(searchResults);
$.each(result.items, function(i, item) {
var question = showQuestion(item);
$('.results').append(question);
});
})
.fail(function(jqXHR, error, errorThrown){
var errorElem = showError(error);
$('.search-results').append(errorElem);
});
};
// Ajax call for Top Answerers
var getTopAnswerers = function(tags) {
var request = {
site: 'stackoverflow'
};
var result = $.ajax({
url: 'http://api.stackexchange.com/2.2/tags/' +tags +'/top-answerers/all_time',
data: request,
dataType: "jsonp",
type: "GET",
})
.done(function (result){
var searchResults = showSearchResults(tags, result.items.length);
$('.search-results').html(searchResults);
$.each(result.items, function(i, item){
console.log(item);
var answerer = showAnswerer(item);
$('.results').append(answerer);
});
})
.fail(function(jqXHR, error, errorThrown){
var errorElem = showError(error);
$('.search-results').append(errorElem);
});
};