-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtimeline.component.ts
More file actions
54 lines (48 loc) · 1.28 KB
/
timeline.component.ts
File metadata and controls
54 lines (48 loc) · 1.28 KB
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
import { Component, OnInit } from '@angular/core';
import { Tweet } from '../tweet';
@Component({
selector: 'app-timeline',
templateUrl: './timeline.component.html',
styleUrls: ['./timeline.component.less']
})
export class TimelineComponent implements OnInit {
tweets: Tweet[] = [
{
created_at: 'Wed Apr 05 12:30:12 +0000 2017',
id: 1,
text: 'Je mets les pieds où je veux, Little John… et c\'est souvent dans la gueule.',
user: 'James Braddock'
},
{
created_at: 'Thu Apr 06 15:24:15 +0000 2017',
id: 2,
text: 'Qui a deux pouces et qui s\'en fout ? Bob Kelso !',
user: 'Bob kelso',
favoriteCount: 2
},
];
user: string = 'Hugo';
constructor() { }
ngOnInit() {
}
addTweet(text: string) {
const tweet: Tweet = {
created_at: new Date().toISOString(),
id: this.tweets.length + 1,
text,
user: this.user
}
this.tweets.push(tweet);
}
likeTweet(id) {
const tweetIndex = this.tweets.findIndex(tweet => tweet.id === id);
const tweet = this.tweets[tweetIndex];
if(!tweet.isLiked) {
tweet.favoriteCount ? tweet.favoriteCount++ : tweet.favoriteCount = 1;
tweet.isLiked = true;
} else {
tweet.favoriteCount--;
tweet.isLiked = false;
}
}
}