/
TweetComponent.vue
61 lines (58 loc) · 1.89 KB
/
TweetComponent.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
<template>
<div>
<div class="tweetbox">
<form @submit.prevent="handleSubmit">
<textarea name="tweet" v-model="tweetInput"></textarea>
<input type="submit" value="Tweet!" />
</form>
</div>
<div class="timeline">
<div class="tweet" v-for="tweet in tweets">
<div class="user-info">
<img :src="`images/${tweet.username}.jpg`" class="avatar" />
<div class="username">
{{ tweet.username }}
</div>
</div>
<div class="content">
{{ tweet.tweet }}
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
tweets: [],
tweetInput: ''
};
},
props: ['initialTweets'],
mounted() {
this.tweets = _.orderBy(this.initialTweets, 'created_at', 'desc');
},
methods: {
handleSubmit() {
const self = this;
const newTweet = {
tweet: this.tweetInput,
username: 'markshust',
};
// optimistic UI
self.tweets.unshift(newTweet);
self.tweetInput = '';
axios.post('/tweet', newTweet)
.then(function(response) {
console.log('posted to server, we do not have to do anything here as ui is already updated!');
})
.catch(function(error) {
console.log('an error occured, tell user and back out optimistic ui update');
alert(error);
self.tweets.shift();
});
}
}
}
</script>