Skip to content

Commit f65cc97

Browse files
committed
Add like feature
1 parent 38c55da commit f65cc97

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# AngularShopTutorial
1+
# AngularTutorial
22

33
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.3.2.
44

@@ -25,3 +25,19 @@ Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protrac
2525
## Further help
2626

2727
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).
28+
29+
<hr/>
30+
31+
# Exercice
32+
33+
## Setup:
34+
35+
- Fork the project, and clone it
36+
- create your own dev branch `git checkout -b dev`
37+
- launch `npm install` to download dependencies
38+
39+
## Sync with forked repository:
40+
41+
- use command `git fetch upstream` to retrieve master branch from forked repository. It will create a local upstream/master branch.
42+
- go to your local master branch: `git checkout master`
43+
- merge the upstream master to your local master: ` git merge upstream/master`. You're up to date!

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)