|
1 | 1 | <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> |
3 | 66 | </template> |
4 | 67 |
|
5 | 68 | <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'; |
8 | 75 |
|
9 | 76 | export default { |
10 | 77 | name: 'TweetContainer', |
11 | 78 |
|
12 | 79 | 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 | + } |
14 | 101 | }, |
15 | 102 |
|
16 | 103 | computed: { |
| 104 | + ...mapGetters('auth', { |
| 105 | + user: 'getAuthenticatedUser' |
| 106 | + }), |
| 107 | +
|
17 | 108 | ...mapGetters('tweet', [ |
18 | 109 | 'getTweetById' |
19 | 110 | ]), |
20 | 111 |
|
| 112 | + ...mapGetters('comment', [ |
| 113 | + 'getCommentsByTweetId' |
| 114 | + ]), |
| 115 | +
|
21 | 116 | tweet() { |
22 | 117 | return this.getTweetById(this.$route.params.id); |
23 | | - } |
24 | | - }, |
| 118 | + }, |
25 | 119 |
|
26 | | - created() { |
27 | | - this.fetchTweetById(this.$route.params.id); |
| 120 | + isTweetOwner() { |
| 121 | + return this.user.id === this.tweet.author.id; |
| 122 | + } |
28 | 123 | }, |
29 | 124 |
|
30 | 125 | methods: { |
31 | 126 | ...mapActions('tweet', [ |
32 | 127 | 'fetchTweetById', |
| 128 | + 'deleteTweet', |
| 129 | + ]), |
| 130 | +
|
| 131 | + ...mapActions('comment', [ |
| 132 | + 'fetchComments', |
33 | 133 | ]), |
| 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 | + }, |
34 | 163 | }, |
35 | 164 | }; |
36 | 165 | </script> |
37 | 166 |
|
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 | +} |
39 | 190 | </style> |
0 commit comments