Skip to content

Commit ea9385e

Browse files
authored
Merge pull request #69 from BinaryStudioAcademy/feature/comments-store-redesign
Feature/comments store redesign
2 parents 76b127b + a9231ef commit ea9385e

File tree

7 files changed

+68
-30
lines changed

7 files changed

+68
-30
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,12 @@ export default {
205205
206206
async onLikeOrDislikeTweet() {
207207
try {
208-
await this.likeOrDislikeTweet(this.tweet.id);
209-
210-
this.showSuccessMessage('Success');
208+
await this.likeOrDislikeTweet({
209+
id: this.tweet.id,
210+
userId: this.user.id
211+
});
211212
} catch (error) {
212-
this.showErrorMessage('Error');
213+
console.error(error.message);
213214
}
214215
}
215216
},

frontend/src/services/Normalizer.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
// map api response into our custom format
22

33
export const userMapper = user => ({
4-
...user,
4+
id: user.id,
5+
avatar: user.avatar,
6+
email: user.email,
7+
nickname: user.nickname,
58
firstName: user.first_name,
69
lastName: user.last_name,
710
});
811

912
export const emptyUser = () => ({
1013
id: null,
14+
avatar: null,
1115
email: '',
16+
nickname: '',
1217
firstName: '',
1318
lastName: '',
14-
avatar: null,
15-
nickname: '',
1619
});
1720

1821
export const commentMapper = comment => ({
19-
...comment,
22+
id: comment.id,
23+
body: comment.body,
2024
authorId: comment.author_id,
25+
tweetId: comment.tweet_id,
2126
created: comment.created_at,
2227
updated: comment.updated_at,
2328
author: userMapper(comment.author),
2429
});
2530

2631
export const likeMapper = like => ({
27-
userId: like.user_id,
28-
createdAt: like.created_at
32+
userId: like.user_id
2933
});
3034

3135
export const tweetMapper = tweet => ({
32-
...tweet,
36+
id: tweet.id,
37+
text: tweet.text,
3338
imageUrl: tweet.image_url,
3439
created: tweet.created_at,
3540
author: userMapper(tweet.author),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default {
1313
direction: 'asc',
1414
});
1515

16-
commit(SET_COMMENTS, { tweetId, comments });
16+
commit(SET_COMMENTS, comments);
1717
commit(SET_LOADING, false, { root: true });
1818

1919
return Promise.resolve();
@@ -30,7 +30,7 @@ export default {
3030
try {
3131
const comment = await api.post('/comments', { tweet_id: tweetId, body: text });
3232

33-
commit(ADD_COMMENT, { tweetId, comment });
33+
commit(ADD_COMMENT, comment);
3434
commit(`tweet/${INCREMENT_COMMENTS_COUNT}`, tweetId, { root: true });
3535
commit(SET_LOADING, false, { root: true });
3636

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1+
import moment from 'moment';
2+
13
export default {
2-
getCommentsByTweetId: state => tweetId => state.comments[tweetId] || [],
4+
getCommentsSortedByCreatedDateAsc: state => Object
5+
.values(state.comments)
6+
.sort(
7+
(a, b) => (
8+
moment(a.created).isBefore(moment(b.created)) ? -1 : 1
9+
)
10+
),
11+
12+
getCommentsByTweetId: (state, getters) => tweetId => getters
13+
.getCommentsSortedByCreatedDateAsc
14+
.filter(comment => comment.tweetId === tweetId),
315

416
tweetIsCommentedByUser: (state, getters) => (tweetId, userId) => getters
517
.getCommentsByTweetId(tweetId)

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@ import { SET_COMMENTS, ADD_COMMENT } from './mutationTypes';
22
import { commentMapper } from '@/services/Normalizer';
33

44
export default {
5-
[SET_COMMENTS]: (state, { tweetId, comments }) => {
5+
[SET_COMMENTS]: (state, comments) => {
6+
let commentsByIdMap = {};
7+
8+
comments.forEach(comment => {
9+
commentsByIdMap = {
10+
...commentsByIdMap,
11+
[comment.id]: commentMapper(comment)
12+
};
13+
});
14+
15+
state.comments = commentsByIdMap;
16+
},
17+
18+
[ADD_COMMENT]: (state, comment) => {
619
state.comments = {
720
...state.comments,
8-
[tweetId]: comments.map(commentMapper)
21+
[comment.id]: commentMapper(comment)
922
};
1023
},
11-
12-
[ADD_COMMENT]: (state, { tweetId, comment }) => {
13-
state.comments[tweetId] = [
14-
...state.comments[tweetId],
15-
commentMapper(comment)
16-
];
17-
},
1824
};

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,22 @@ export default {
138138
}
139139
},
140140

141-
async likeOrDislikeTweet({ commit }, id) {
141+
async likeOrDislikeTweet({ commit }, { id, userId }) {
142142
commit(SET_LOADING, true, { root: true });
143143

144144
try {
145145
const data = await api.put(`/tweets/${id}/like`);
146146

147147
if (data.status === 'added') {
148-
commit(LIKE_TWEET, id);
148+
commit(LIKE_TWEET, {
149+
id,
150+
userId
151+
});
149152
} else {
150-
commit(DISLIKE_TWEET, id);
153+
commit(DISLIKE_TWEET, {
154+
id,
155+
userId
156+
});
151157
}
152158

153159
commit(SET_LOADING, false, { root: true });

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ import { tweetMapper } from '@/services/Normalizer';
1111

1212
export default {
1313
[SET_TWEETS]: (state, tweets) => {
14+
let storeTweets = {};
15+
1416
tweets.forEach(tweet => {
15-
state.tweets = {
16-
...state.tweets,
17+
storeTweets = {
18+
...storeTweets,
1719
[tweet.id]: tweetMapper(tweet)
1820
};
1921
});
22+
23+
state.tweets = storeTweets;
2024
},
2125

2226
[SET_TWEET_IMAGE]: (state, { id, imageUrl }) => {
@@ -38,11 +42,15 @@ export default {
3842
state.tweets[id].commentsCount++;
3943
},
4044

41-
[LIKE_TWEET]: (state, id) => {
45+
[LIKE_TWEET]: (state, { id, userId }) => {
4246
state.tweets[id].likesCount++;
47+
48+
state.tweets[id].likes.push({ userId });
4349
},
4450

45-
[DISLIKE_TWEET]: (state, id) => {
51+
[DISLIKE_TWEET]: (state, { id, userId }) => {
4652
state.tweets[id].likesCount--;
53+
54+
state.tweets[id].likes = state.tweets[id].likes.filter(like => like.userId !== userId);
4755
}
4856
};

0 commit comments

Comments
 (0)