Skip to content

Commit 6113613

Browse files
authored
Merge pull request #55 from BinaryStudioAcademy/feature/comment-count
Feature/comment count
2 parents b4665b3 + 335dc6a commit 6113613

File tree

12 files changed

+85
-20
lines changed

12 files changed

+85
-20
lines changed

frontend/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module.exports = {
4444
tabWidth: 4
4545
}],
4646
'no-trailing-spaces': 'off',
47+
'no-plusplus': 'off'
4748
},
4849
parserOptions: {
4950
parser: 'babel-eslint',

frontend/src/components/common/Navbar.vue

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<template>
2-
<nav class="navbar" role="navigation" aria-label="main navigation">
2+
<nav
3+
v-if="user.id"
4+
class="navbar"
5+
role="navigation"
6+
aria-label="main navigation"
7+
>
38
<div class="navbar-brand">
49
<a
510
role="button"
@@ -105,9 +110,12 @@ export default {
105110
}),
106111
},
107112
108-
created() {
109-
this.fetchAuthenticatedUser()
110-
.catch(error => console.log(`Error occurred: ${error.message}`));
113+
async created() {
114+
try {
115+
await this.fetchAuthenticatedUser();
116+
} catch (error) {
117+
console.log(`Error occurred: ${error.message}`);
118+
}
111119
},
112120
113121
methods: {

frontend/src/components/common/TweetPreview.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
<nav class="level is-mobile">
3434
<div class="level-left">
3535
<a class="level-item">
36-
<span class="icon is-medium">
36+
<span class="icon is-medium has-text-info">
3737
<font-awesome-icon icon="comments" />
3838
</span>
39-
{{ tweet.comments_count || 0 }}
39+
{{ tweet.commentsCount }}
4040
</a>
4141
<a class="level-item">
42-
<span class="icon is-medium">
42+
<span class="icon is-medium has-text-info">
4343
<font-awesome-icon icon="heart" />
4444
</span>
4545
{{ tweet.likes_count || 0 }}

frontend/src/components/view/tweet/Comment.vue

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,20 @@
1515
<br>
1616
{{ comment.body }}
1717
<br>
18-
<small><a>Like</a> · <a>Reply</a> · {{ comment.created | createdDate }}</small>
18+
<small>
19+
{{ comment.created | createdDate }}
20+
</small>
1921
</p>
22+
<nav class="level is-mobile">
23+
<div class="level-left">
24+
<a class="level-item">
25+
<span class="icon is-medium has-text-info">
26+
<font-awesome-icon icon="heart" />
27+
</span>
28+
{{ comment.likesCount }}
29+
</a>
30+
</div>
31+
</nav>
2032
</div>
2133
</div>
2234
</article>
@@ -36,5 +48,13 @@ export default {
3648
</script>
3749

3850
<style lang="scss" scoped>
51+
nav {
52+
margin-left: -8px;
53+
}
3954
55+
.content {
56+
p {
57+
margin-bottom: 0;
58+
}
59+
}
4060
</style>

frontend/src/components/view/tweet/NewCommentForm.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
class="textarea"
1313
v-model="text"
1414
placeholder="Add a comment..."
15+
@keyup.ctrl.exact.enter="onPostComment"
1516
/>
1617
</p>
1718
</div>
@@ -67,6 +68,7 @@ export default {
6768
tweetId: this.tweetId,
6869
text: this.text,
6970
});
71+
7072
this.clearInput();
7173
},
7274
},

frontend/src/components/view/tweet/TweetContainer.vue

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,28 @@
3232
<br>
3333
{{ tweet.text }}
3434
<br>
35-
<a>Like</a>
3635
</p>
3736

37+
<nav class="level is-mobile">
38+
<div class="level-left">
39+
<a class="level-item">
40+
<span
41+
class="icon is-medium has-text-info"
42+
:class="{ 'has-text-danger': tweetIsCommentedByUser(tweet.id, user.id) }"
43+
>
44+
<font-awesome-icon icon="comments" />
45+
</span>
46+
{{ tweet.commentsCount }}
47+
</a>
48+
<a class="level-item">
49+
<span class="icon is-medium has-text-info">
50+
<font-awesome-icon icon="heart" />
51+
</span>
52+
{{ tweet.likes_count || 0 }}
53+
</a>
54+
</div>
55+
</nav>
56+
3857
<figure v-if="tweet.imageUrl" class="image is-3by1 tweet-image">
3958
<img
4059
:src="tweet.imageUrl"
@@ -45,7 +64,7 @@
4564
</div>
4665
</div>
4766

48-
<div v-if="isTweetOwner" class="column is-narrow is-12-mobile">
67+
<div v-if="isTweetOwner(tweet.id, user.id)" class="column is-narrow is-12-mobile">
4968
<div class="buttons">
5069
<b-button type="is-warning" @click="onEditTweet">Edit</b-button>
5170
<b-button type="is-danger" @click="onDeleteTweet">Delete</b-button>
@@ -117,20 +136,18 @@ export default {
117136
}),
118137
119138
...mapGetters('tweet', [
120-
'getTweetById'
139+
'getTweetById',
140+
'isTweetOwner'
121141
]),
122142
123143
...mapGetters('comment', [
124-
'getCommentsByTweetId'
144+
'getCommentsByTweetId',
145+
'tweetIsCommentedByUser'
125146
]),
126147
127148
tweet() {
128149
return this.getTweetById(this.$route.params.id);
129150
},
130-
131-
isTweetOwner() {
132-
return this.user.id === this.tweet.author.id;
133-
}
134151
},
135152
136153
methods: {

frontend/src/services/Normalizer.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ export const commentMapper = comment => ({
1313
authorId: comment.author_id,
1414
created: comment.created_at,
1515
updated: comment.updated_at,
16-
author: userMapper(comment.author)
16+
author: userMapper(comment.author),
17+
// @todo fix backend api
18+
likesCount: comment.likes_count || 0
1719
});
1820

1921
export const tweetMapper = tweet => ({
2022
...tweet,
2123
imageUrl: tweet.image_url,
2224
created: tweet.created_at,
2325
author: userMapper(tweet.author),
24-
comments: tweet.comments.map(commentMapper)
26+
commentsCount: tweet.comments_count
2527
});

frontend/src/store/modules/comment/actions.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import api from '@/api/Api';
22
import { SET_LOADING } from '../../mutationTypes';
33
import { SET_COMMENTS, ADD_COMMENT } from './mutationTypes';
4+
import { INCREMENT_COMMENTS_COUNT } from '../tweet/mutationTypes';
45
import { commentMapper } from '@/services/Normalizer';
56

67
export default {
@@ -30,6 +31,7 @@ export default {
3031
const comment = await api.post('/comments', { tweet_id: tweetId, body: text });
3132

3233
commit(ADD_COMMENT, { tweetId, comment });
34+
commit(`tweet/${INCREMENT_COMMENTS_COUNT}`, tweetId, { root: true });
3335
commit(SET_LOADING, false, { root: true });
3436

3537
return Promise.resolve(commentMapper(comment));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
export default {
22
getCommentsByTweetId: state => tweetId => state.comments[tweetId] || [],
3+
4+
tweetIsCommentedByUser: (state, getters) => (tweetId, userId) => getters
5+
.getCommentsByTweetId(tweetId)
6+
.find(comment => comment.authorId === userId),
37
};

frontend/src/store/modules/tweet/getters.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ export default {
66
moment(a.created).isBefore(moment(b.created)) ? 1 : -1
77
)
88
),
9-
getTweetById: state => id => state.tweets[id]
9+
10+
getTweetById: state => id => state.tweets[id],
11+
12+
isTweetOwner: (state, getters) => (tweetId, userId) => getters.getTweetById(tweetId).author.id === userId,
1013
};

0 commit comments

Comments
 (0)