-
Notifications
You must be signed in to change notification settings - Fork 40
/
CommentForm.vue
316 lines (293 loc) · 7.49 KB
/
CommentForm.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<template>
<div v-if="me" class="comment-container">
<!-- <span class="active-without-border" v-if="isLoggedIn">
<router-link class="profile-img-link" to="/profile">
<img class="profile-img" :src="errorImg || avatarUrl" @error="imgOnError">
</router-link>
</span> -->
<CommentTribute
class="comment-box__container"
:options="options"
@tribute-replaced="tributeReplaced"
@tribute-no-match="tributeNoMatch">
<textarea
placeholder="Your comment ..."
class="comment-box"
ref="contentTextarea"
:disabled="isSubmitting"
type="text"
v-model="commentContent"
/>
</CommentTribute>
<div v-if="isLoggedIn" style="align-self: flex-end;">
<div v-if="isSubmitting">
<spinner :show="true"></spinner>
</div>
<div class="buttons-comment" v-else>
<button
class="button-submit"
:disabled="isSubmitting"
@click="submitComment">
{{submitButtonText}}
</button>
<button v-if="showCancel" class='btn-cancel btn btn-link'
:disabled="isSubmitting"
@click='cancelPressed'><i class="fa fa-times"/></button>
</div>
</div>
<div v-else>
<button @click='errorSubmit' class="button-submit">{{submitButtonText}}</button>
</div>
</div>
</template>
<script>
import CommentTribute from "@/components/comment/CommentTribute";
import ProfileLabel from "@/components/profile/ProfileLabel";
import { debounce, each, map } from "lodash";
import Spinner from "@/components/Spinner";
import { mapState, mapActions, mapGetters } from "vuex";
export default {
name: "comment-form",
props: {
userData: {
type: Object,
default: function() {
return {
avatarUrl: ""
};
}
},
initialComment: {
type: String
},
highlight: {
type: String,
},
showCancel: {
type: Boolean,
default: false
},
cancelPressed: {
type: Function
},
isSubmitting: {
type: Boolean,
required: true
},
submitCallback: {
type: Function,
required: true
},
existingMentions: {
type: Array,
default: () => {
return [];
}
},
submitButtonText: {
type: String,
default: "Submit"
},
},
components: {
CommentTribute,
ProfileLabel,
Spinner
},
data() {
return {
// Options for auto-complete mentions
options: {
menuItemTemplate: function(item) {
const img = item.original.user.avatarUrl
? item.original.user.avatarUrl
: "https://s3-us-west-2.amazonaws.com/sd-profile-pictures/profile-icon-9.png";
return `<span><img width="30px" src='${img}'/> ${
item.original.value
} </span>`;
},
noMatchTemplate: "",
allowSpaces: false,
values: []
},
mentionedUsers: this.existingMentions,
commentContent: this.initialComment,
errorImg: ''
};
},
watch: {
commentContent: function() {
// Check mentions
if (this.isLoggedIn) {
const currentMentions = [];
each(this.mentionedUsers, user => {
const mentionText = "@" + user.name;
if (this.commentContent.indexOf(mentionText) >= 0) {
currentMentions.push(user);
}
});
this.mentionedUsers = currentMentions;
} else {
this.$toasted.error('You must login to post a comment')
}
},
initialComment: function() {
this.commentContent = this.initialComment;
}
},
computed: {
// local computed methods +
...mapGetters(["isLoggedIn"]),
...mapState({
me(state) {
return state.me;
},
alreadySubscribed(state) {
if (!this.isLoggedIn) return false;
if (state.me && state.me.subscription && state.me.subscription.active) {
return true;
} else {
return false;
}
},
avatarUrl(state) {
return this.me.avatarUrl || state.placeholderAvatar;
}
}),
hasMentions() {
return this.mentionedUsers.length > 0;
}
},
methods: {
...mapActions(["searchUsers"]),
errorSubmit() {
this.$toasted.error('You must login to post a comment')
},
// TODO: Focus not working
focusTextarea() {
this.$refs.contentTextarea.focus()
},
handleSelectUser(user) {
// Check if contains user already to avoid duplicates.
let containsUserAlready = false;
each(this.mentionedUsers, function(existingUser) {
if (existingUser._id.toString() === user._id.toString()) {
containsUserAlready = true;
}
});
if (!containsUserAlready) {
this.mentionedUsers.push(user);
}
},
imgOnError() {
this.errorImg = 'https://s3-us-west-2.amazonaws.com/sd-profile-pictures/profile-icon-9.png'
},
tributeReplaced({ detail }) {
const { user } = detail.item.original;
// todo: TRY: catch
// V-model gets out of sync when new element is inserted so updating it:
this.commentContent = this.$refs.contentTextarea.value;
this.handleSelectUser(user);
},
tributeNoMatch: debounce(function(searchQuery) {
this.searchUsers({ name: searchQuery }).then(users => {
this.setUserList(users);
});
}, 10),
// Remove dupes for now since we have a weird hack:
alreadyContainsMention(user) {
let contains = false;
each(this.options.values, mention => {
if (user._id == mention.user._id) {
contains = true;
}
});
return contains;
},
// TODO: loop over and match. Start with longer matches
// Search for [space]@_${value} so when we replace while we replace
// with . What if mention is first character.
setUserList(userList) {
this.options.values.splice(0, 10);
userList.forEach(user => {
if (!this.alreadyContainsMention(user)) {
this.options.values.push({ key: user.name, value: user.name, user });
}
});
},
submitComment() {
const data = {
content: this.commentContent,
mentions: mentions
}
const mentions = map(this.mentionedUsers, user => {
return user._id;
});
if (this.highlight) {
data.highlight = this.highlight
}
this.mentionedUsers = []; // resetting mentioned users
this.commentContent = ''
this.submitCallback(data);
}
}
};
</script>
<style scoped lang="stylus">
@import '../../css/variables'
.btn-cancel
padding 0 0 0 10px
.button-submit
outline none
border-radius 4px
margin-top 10px
a
color white
&:hover
text-decoration none
.buttons-comment
display flex
.comment-container
background none
display flex
flex-direction column
align-items center
width: 100%;
.profile-img
width 35px
height 35px
border-radius 50px
margin-right 10px
.comment-box
margin 0 10px
margin-left 0
margin-right 0
width 100%
padding 5px
padding-left 10px
border-radius 4px
border-color #c5c5c5
resize none
height inherit
outline none
border: 1px solid #ccc
height 100px
.comment-box__container
display flex
width 100%
&:hover
text-decoration none
.buttons-comment
display: flex;
.comment-container
display: flex;
align-items: center;
width: 100%;
.profile-img
width: 35px;
height: 35px;
border-radius: 50px;
.comment-box__container
display: flex;
width: 100%;
</style>