-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
61 lines (61 loc) · 1.96 KB
/
scripts.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
$(document).ready(function(){
// When user clicks on submit comment to add comment under post
$(document).on('click', '#submit_comment', function(e) {
e.preventDefault();
var comment_text = $('#comment_text').val();
var url = $('#comment_form').attr('action');
// Stop executing if not value is entered
if (comment_text === "" ) return;
$.ajax({
url: url,
type: "POST",
data: {
comment_text: comment_text,
comment_posted: 1
},
success: function(data){
var response = JSON.parse(data);
if (data === "error") {
alert('There was an error adding comment. Please try again');
} else {
$('#comments-wrapper').prepend(response.comment)
$('#comments_count').text(response.comments_count);
$('#comment_text').val('');
}
}
});
});
// When user clicks on submit reply to add reply under comment
$(document).on('click', '.reply-btn', function(e){
e.preventDefault();
// Get the comment id from the reply button's data-id attribute
var comment_id = $(this).data('id');
// show/hide the appropriate reply form (from the reply-btn (this), go to the parent element (comment-details)
// and then its siblings which is a form element with id comment_reply_form_ + comment_id)
$(this).parent().siblings('form#comment_reply_form_' + comment_id).toggle(500);
$(document).on('click', '.submit-reply', function(e){
e.preventDefault();
// elements
var reply_textarea = $(this).siblings('textarea'); // reply textarea element
var reply_text = $(this).siblings('textarea').val();
var url = $(this).parent().attr('action');
$.ajax({
url: url,
type: "POST",
data: {
comment_id: comment_id,
reply_text: reply_text,
reply_posted: 1
},
success: function(data){
if (data === "error") {
alert('There was an error adding reply. Please try again');
} else {
$('.replies_wrapper_' + comment_id).append(data);
reply_textarea.val('');
}
}
});
});
});
});