Skip to content

Commit ce1c4ee

Browse files
committed
Refactor tweet container
1 parent 507c73e commit ce1c4ee

File tree

3 files changed

+161
-191
lines changed

3 files changed

+161
-191
lines changed

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

Lines changed: 0 additions & 182 deletions
This file was deleted.
Lines changed: 160 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,190 @@
11
<template>
2-
<Tweet v-if="tweet" :tweet="tweet" />
2+
<div>
3+
<article class="media box tweet">
4+
<figure class="media-left">
5+
<p v-if="tweet.author.avatar" class="image is-64x64 is-square">
6+
<img class="is-rounded" :src="tweet.author.avatar">
7+
</p>
8+
<DefaultAvatar v-else class="image is-64x64" :user="tweet.author" />
9+
</figure>
10+
11+
<div class="media-content">
12+
<div class="columns">
13+
<div class="column">
14+
<div class="content">
15+
<p class="tweet-text">
16+
<strong>{{ tweet.author.name }}</strong>
17+
<br>
18+
<small>
19+
{{ tweet.created | createdDate }}
20+
</small>
21+
<br>
22+
{{ tweet.text }}
23+
<br>
24+
<a>Like</a>
25+
</p>
26+
27+
<figure v-if="tweet.imageUrl" class="image is-3by1 tweet-image">
28+
<img
29+
:src="tweet.imageUrl"
30+
alt="Tweet image"
31+
@click="showImageModal"
32+
>
33+
</figure>
34+
</div>
35+
</div>
36+
37+
<div v-if="isTweetOwner" class="column is-narrow is-12-mobile">
38+
<div class="buttons">
39+
<b-button type="is-warning" @click="onEditTweet">Edit</b-button>
40+
<b-button type="is-danger" @click="onDeleteTweet">Delete</b-button>
41+
</div>
42+
</div>
43+
</div>
44+
45+
<template v-for="comment in getCommentsByTweetId(tweet.id)">
46+
<Comment
47+
:key="comment.id"
48+
:comment="comment"
49+
/>
50+
</template>
51+
52+
<NewCommentForm :tweet-id="tweet.id" />
53+
</div>
54+
</article>
55+
56+
<b-modal :active.sync="isImageModalActive">
57+
<p class="image is-4by3">
58+
<img :src="tweet.imageUrl">
59+
</p>
60+
</b-modal>
61+
62+
<b-modal :active.sync="isEditTweetModalActive" has-modal-card>
63+
<EditTweetForm :tweet="tweet" />
64+
</b-modal>
65+
</div>
366
</template>
467

568
<script>
6-
import { mapActions, mapGetters } from 'vuex';
7-
import Tweet from './Tweet.vue';
69+
import { mapGetters, mapActions } from 'vuex';
70+
import Comment from './Comment.vue';
71+
import NewCommentForm from './NewCommentForm.vue';
72+
import EditTweetForm from './EditTweetForm.vue';
73+
import DefaultAvatar from '../../common/DefaultAvatar.vue';
74+
import showStatusToast from '../../mixin/showStatusToast';
875
976
export default {
1077
name: 'TweetContainer',
1178
1279
components: {
13-
Tweet,
80+
Comment,
81+
NewCommentForm,
82+
EditTweetForm,
83+
DefaultAvatar,
84+
},
85+
86+
mixins: [showStatusToast],
87+
88+
data: () => ({
89+
isEditTweetModalActive: false,
90+
isImageModalActive: false
91+
}),
92+
93+
async created() {
94+
try {
95+
await this.fetchTweetById(this.$route.params.id);
96+
97+
this.fetchComments(this.tweet.id);
98+
} catch (error) {
99+
console.error(error.message);
100+
}
14101
},
15102
16103
computed: {
104+
...mapGetters('auth', {
105+
user: 'getAuthenticatedUser'
106+
}),
107+
17108
...mapGetters('tweet', [
18109
'getTweetById'
19110
]),
20111
112+
...mapGetters('comment', [
113+
'getCommentsByTweetId'
114+
]),
115+
21116
tweet() {
22117
return this.getTweetById(this.$route.params.id);
23-
}
24-
},
118+
},
25119
26-
created() {
27-
this.fetchTweetById(this.$route.params.id);
120+
isTweetOwner() {
121+
return this.user.id === this.tweet.author.id;
122+
}
28123
},
29124
30125
methods: {
31126
...mapActions('tweet', [
32127
'fetchTweetById',
128+
'deleteTweet',
129+
]),
130+
131+
...mapActions('comment', [
132+
'fetchComments',
33133
]),
134+
135+
onEditTweet() {
136+
this.isEditTweetModalActive = true;
137+
},
138+
139+
onDeleteTweet() {
140+
this.$dialog.confirm({
141+
title: 'Deleting tweet',
142+
message: 'Are you sure you want to <b>delete</b> your tweet? This action cannot be undone.',
143+
confirmText: 'Delete Tweet',
144+
type: 'is-danger',
145+
146+
onConfirm: async () => {
147+
try {
148+
await this.deleteTweet(this.tweet.id);
149+
150+
this.showSuccessMessage('Tweet deleted!');
151+
152+
this.$router.push({ name: 'feed' });
153+
} catch {
154+
this.showErrorMessage('Unable to delete tweet!');
155+
}
156+
}
157+
});
158+
},
159+
160+
showImageModal() {
161+
this.isImageModalActive = true;
162+
},
34163
},
35164
};
36165
</script>
37166

38-
<style scoped lang="scss">
167+
<style lang="scss" scoped>
168+
@import "~bulma/sass/utilities/initial-variables";
169+
170+
.tweet-image {
171+
margin: 12px 0 0 0;
172+
173+
img {
174+
width: auto;
175+
cursor: pointer;
176+
}
177+
}
178+
179+
.buttons {
180+
.button {
181+
@media screen and (max-width: $tablet) {
182+
font-size: 0.8rem;
183+
}
184+
}
185+
}
186+
187+
.tweet-text {
188+
max-width: 100%;
189+
}
39190
</style>

frontend/src/styles/common.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ html, body {
4343
max-width: 960px;
4444
padding-left: 10px;
4545
padding-right: 10px;
46+
padding-bottom: 20px;
4647
}

0 commit comments

Comments
 (0)