Skip to content

Commit 54ebeb6

Browse files
committed
Add like feature
1 parent 38c55da commit 54ebeb6

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

src/app/timeline/timeline.component.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
{{ tweet.text }}
88
</div>
99
<div class="favorite">
10-
<i class="far fa-heart"></i>
11-
<span *ngIf="tweet.favoriteCount">{{ tweet.favoriteCount }}</span>
10+
<i class="button fa-heart" [ngClass]="{
11+
'fas': tweet.isLiked,
12+
'far': !tweet.isLiked
13+
}"
14+
(click)="likeTweet(tweet.id)"></i>
15+
<span *ngIf="tweet.favoriteCount != null">{{ tweet.favoriteCount }}</span>
1216
</div>
1317
</div>
1418

src/app/timeline/timeline.component.less

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,7 @@
6161
cursor: pointer;
6262
}
6363
}
64+
65+
.favorite-count {
66+
margin-left: 4px;
67+
}

src/app/timeline/timeline.component.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,15 @@ export class TimelineComponent implements OnInit {
4040
this.tweets.push(tweet);
4141
}
4242

43+
likeTweet(id) {
44+
const tweetIndex = this.tweets.findIndex(tweet => tweet.id === id);
45+
const tweet = this.tweets[tweetIndex];
46+
if(!tweet.isLiked) {
47+
tweet.favoriteCount ? tweet.favoriteCount++ : tweet.favoriteCount = 1;
48+
tweet.isLiked = true;
49+
} else {
50+
tweet.favoriteCount--;
51+
tweet.isLiked = false;
52+
}
53+
}
4354
}

src/app/tweet.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export interface Tweet {
33
id: number,
44
text: string,
55
user: string,
6-
favoriteCount?: number
6+
favoriteCount?: number,
7+
isLiked?: boolean
78
}

0 commit comments

Comments
 (0)