/
Comments.vue
150 lines (137 loc) · 6.22 KB
/
Comments.vue
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
<template>
<div>
<div v-for="comment in comments">
<div class="row margin-bottom-5">
<div class="col-sm-12 text-md">
<b>{{ comment.user.name }}</b> said:
</div>
<div class="col-sm-12 text-justify">{{ comment.content }}
<span class="text-grey"> {{ comment.createdAtHuman }}</span>
<span class="text-primary pointer" data-toggle="modal"
data-target="#reply-form"
v-on:click="initiateReplyForm(comment.id)"
> Reply</span>
</div>
</div>
<div class="row margin-bottom-5 margin-left-30" v-for="commentReply in comment.replies">
<div class="col-sm-12 text-md">
<b>{{ commentReply.user.name }}</b> replied:
</div>
<div class="col-sm-12 text-justify">{{commentReply.content}}
<span class="text-grey"> {{commentReply.createdAtHuman}}</span>
</div>
</div>
</div>
<div class="row" v-if="comments.length === 0">
<div class="col-sm-12">
<span class="text-grey">No comment yet</span>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="reply-form" tabindex="-1" role="dialog" aria-labelledby="reply-form">
<div class="modal-dialog" role="document">
<form action="#" method="post" onsubmit="return false;"
name="add_comment_form"
v-on:submit.prevent="addReply(reply)">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel">Reply</h4>
</div>
<div class="modal-body row">
<div class="col-md-12">
<input type="hidden" v-model="reply.parent_comment_id" name="parent_comment_id"
id="parent_comment_id">
<div class="form-group col-sm-12 no-padding">
<textarea name="content" class="form-control" rows="3" v-model="reply.content"
placeholder="Comment*" required></textarea>
</div>
<div class="form-group col-sm-6 no-padding-left">
<input type="text" name="name" value="" class="form-control"
v-model="reply.name"
placeholder="Name*" required>
</div>
<div class="form-group col-sm-6 no-padding-right">
<input type="email" name="email" class="form-control"
v-model="reply.email"
placeholder="Email*" required>
</div>
</div>
</div>
<div class="modal-footer">
<div class="form-group col-sm-12 no-padding-right checkbox">
<label class="padding-right-5">
<input type="checkbox" v-model="reply.notify"
name="notify"> Notify me about new article
</label>
<button type="submit"
class="btn btn-primary pull-right"
id="reply-submit-btn">Reply</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</template>
<script>
import * as Ladda from 'ladda';
export default {
name: "Comments",
props: {
comments: Array,
add_comment_url: String,
},
data() {
return {
reply: {
parent_comment_id: '',
content: '',
name: '',
email: '',
notify: false,
}
}
},
methods: {
initiateReplyForm(commentID) {
this.reply.parent_comment_id = commentID;
},
addReply (reply) {
let l = Ladda.create(document.querySelector('#reply-submit-btn'));
l.start();
axios.post(this.add_comment_url, reply)
.then( (response) => {
console.log(response);
//hide comment form
$("#comments").html(response.body);
$('.modal').modal('hide');
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
//show success alert
let successAlert = $('#success-alert');
successAlert.show();
successAlert.fadeOut(1000 * 10);
$('#success-msg').html('Success! your reply will be published soon');
//clear form values
$('input').val('');
$('textarea').val('');
l.stop();
})
.catch( (response) => {
//show error alert
var errorAlert = $('#error-alert');
errorAlert.show();
errorAlert.fadeOut(1000 * 10);
$('#error-msg').html(response.body.errorMsg);
});
return false;
}
}
}
</script>
<style scoped>
</style>