Skip to content

Commit d20e0f5

Browse files
committed
🐛 debounce time save feeds added
1 parent 9e84d73 commit d20e0f5

14 files changed

+115
-38
lines changed

src/app/core/services/feed.service.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,32 @@ export class FeedService {
4040
`${base_url}/feed?skip=${skip}&limit=${limit}` :
4141
`${base_url}/feed/byUser/subscription?skip=${skip}&limit=${limit}`;
4242
const headers = !isAuthenticated ? {} : this.headers;
43-
return this.http.get<IResponseFeed>(url, headers).pipe(map(resp => this.mapInUserResource(resp.feeds)));
43+
return this.http.get<IResponseFeed>(url, headers)
44+
.pipe(map(resp => this.mapInUserResource(resp.feeds) as Feed[]));
4445

4546
}
4647

4748
getFeed(id: string): Observable<Feed> {
4849
const url = `${base_url}/feed/${id}`;
49-
return this.http.get<IResponseFeed>(url).pipe(map((resp) => resp.feed));
50+
return this.http.get<IResponseFeed>(url)
51+
.pipe(map((resp) => this.mapInUserResource(resp.feed) as Feed));
5052
}
5153

5254
getSavedFeeds(skip = 0, limit = 10): Observable<Feed[]> {
5355
const url = `${base_url}/feed/byUser/saved?skip=${skip}&limit=${limit}`;
54-
return this.http.get<IResponseFeed>(url, this.headers).pipe(map((resp) => this.mapInUserResource(resp.feeds)));
56+
return this.http.get<IResponseFeed>(url, this.headers)
57+
.pipe(map((resp) => this.mapInUserResource(resp.feeds) as Feed[]));
5558
}
5659

57-
private mapInUserResource(feeds: Feed[]): Feed[] {
60+
private mapInUserResource(feeds: Feed[] | Feed): Feed[] | Feed {
5861
const userActive = this.authService.getUserActive;
5962
if(userActive) {
6063
const { savedFeeds } = userActive;
61-
feeds.map(feed => feed.inUser = savedFeeds?.includes(feed._id));
64+
if(Array.isArray(feeds)) {
65+
feeds.map(feed => feed.inUser = savedFeeds?.includes(feed._id));
66+
} else {
67+
feeds.inUser = savedFeeds?.includes(feeds._id);
68+
}
6269
}
6370
return feeds;
6471
}

src/app/features/feed/pages/feed/feed.component.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@
5454
color: var(--font-color-secondary);
5555
}
5656

57-
.content .details .bookmark {
57+
.content .details .save {
5858
font-size: 24px;
5959
color: var(--primary-color);
60+
z-index: 10;
61+
cursor: pointer;
6062
}
6163

6264
.content .details-website {

src/app/features/feed/pages/feed/feed.component.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
<span class="title-new">{{feed.title}}</span>
66
<div class="details">
77
<span class="detail">{{feed.writer}} &bull; {{feed.pubDate | date: 'dd/MM/yyyy, h:mm a' }}</span>
8-
<div class="bookmark">
9-
<i class="bx bx-bookmark"></i>
10-
</div>
8+
<a class="save" (click)="saveFeed(feed._id)">
9+
<i class="bx bx-bookmark-plus" [class.bxs-bookmark-minus]="feed.inUser" #iconi
10+
(mouseover)="changeStyle(iconi, true)" (mouseout)="changeStyle(iconi, false)"></i>
11+
</a>
1112
</div>
1213
<div class="feed" [innerHTML]="feed.content | html">
1314
</div>

src/app/features/feed/pages/feed/feed.component.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
22
import { ActivatedRoute, Router } from '@angular/router';
3-
4-
import { FeedService } from '@services/feed.service';
3+
import { debounceTime, forkJoin, Subject } from 'rxjs';
54

65
import { Feed } from '@models/feed.model';
7-
import { forkJoin } from 'rxjs';
6+
7+
import { AuthService } from '@services/auth.service';
8+
import { FeedService } from '@services/feed.service';
9+
import { UserService } from '@services/user.service';
810
// use encapsulation to styles works correctly from css files
911
@Component({
1012
selector: 'app-feed',
@@ -17,11 +19,20 @@ export class FeedComponent implements OnInit {
1719
public recentsFeed: Feed[];
1820
public isLoading: boolean = true;
1921

22+
private saveFeedSub: Subject<string> = new Subject();
23+
2024
constructor(
2125
private activatedRoute: ActivatedRoute,
2226
private feedService: FeedService,
27+
private authService: AuthService,
28+
private userService: UserService,
2329
private router: Router
24-
) { }
30+
) {
31+
this.saveFeedSub.pipe(
32+
debounceTime(250))
33+
.subscribe((idFeed) => this.updatePreferences(idFeed)
34+
);
35+
}
2536

2637
ngOnInit(): void {
2738
this.activatedRoute.params.subscribe(({ feedID }) => this.loadData(feedID));
@@ -45,4 +56,22 @@ export class FeedComponent implements OnInit {
4556
this.router.navigate([`/feed/${id}`]);
4657
}
4758

59+
saveFeed(id: string) {
60+
if(!this.authService.isAuthenticated) {
61+
return this.authService.showModalAuth();
62+
}
63+
this.saveFeedSub.next(id);
64+
}
65+
66+
updatePreferences(idFeed: string): void {
67+
this.userService.modifyPreferences(idFeed, 'saved').subscribe(() => {
68+
this.feed.inUser = !this.feed.inUser;
69+
})
70+
}
71+
72+
changeStyle(element: any, change: boolean): void {
73+
if(change) element.classList.add('bxs-bookmark');
74+
else element.classList.remove('bxs-bookmark')
75+
}
76+
4877
}

src/app/features/home/components/websites-card/websites-card.component.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
align-items: center;
2626
}
2727

28-
.card.card-websites .item > a {
28+
.card.card-websites a.item {
2929
text-decoration: none;
3030
font-size: 14px;
3131
font-weight: 500;

src/app/features/home/components/websites-card/websites-card.component.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,5 @@
99
class="btn btn-primary btn-subscribe">{{ (website.inUser) ?
1010
'Following' : 'Subscribe' }}</button>
1111
</div>
12-
<div class="item">
13-
<a routerLink="/websites">Show more...</a>
14-
</div>
12+
<a class="item" routerLink="/websites">Show more...</a>
1513
</div>

src/app/features/home/home.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<section class="recently">
33
<div class="title-wrapper">
44
<span class="title-section">Latest news</span>
5-
<a class="refresh" *ngIf="!isLoading" (click)="reloadData()">
5+
<a class="refresh" [class.loading]="isLoading" *ngIf="getSkip >= 10" (click)="reloadData()">
66
<i class="bx bx-refresh"></i>
77
</a>
88
</div>

src/app/features/home/home.component.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ export class HomeComponent implements OnInit, OnDestroy {
4646

4747
resetDataInitial() {
4848
this.isAuthenticated = true;
49-
// fixed it - using service or emitter
50-
document.body.scrollTop = 0; // this is for Safari
51-
document.documentElement.scrollTop = 0; // for another one
5249
this.skip = 0;
5350
this.getDataInitial();
5451
}
@@ -59,6 +56,9 @@ export class HomeComponent implements OnInit, OnDestroy {
5956
}
6057

6158
getDataInitial() {
59+
document.body.scrollTop = 0; // this is for Safari
60+
document.documentElement.scrollTop = 0; // for another one
61+
this.skip = 0;
6262
this.isLoading = true;
6363
forkJoin({
6464
feeds: this.getFeeds(),
@@ -86,7 +86,8 @@ export class HomeComponent implements OnInit, OnDestroy {
8686
this.getFeeds().subscribe(feeds => this.feeds = [...this.feeds, ...feeds]);
8787
}
8888

89-
90-
89+
get getSkip(): number {
90+
return this.skip;
91+
}
9192

9293
}

src/app/features/home/pages/explore/explore.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<section class="recently">
33
<div class="title-wrapper">
44
<span class="title-section">Explore news</span>
5-
<a class="refresh" *ngIf="!isLoading" (click)="reloadData()">
5+
<a class="refresh" [class.loading]="isLoading" *ngIf="getSkip >= 10" (click)="reloadData()">
66
<i class="bx bx-refresh"></i>
77
</a>
88
</div>

src/app/features/home/pages/explore/explore.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export class ExploreComponent implements OnInit {
3434
}
3535

3636
getDataInitial() {
37+
document.body.scrollTop = 0; // this is for Safari
38+
document.documentElement.scrollTop = 0; // for another one
39+
this.skip = 0;
3740
this.isLoading = true;
3841
forkJoin({
3942
feeds: this.getFeeds(),
@@ -63,4 +66,8 @@ export class ExploreComponent implements OnInit {
6366
this.getFeeds().subscribe(feeds => this.feeds = [...this.feeds, ...feeds]);
6467
}
6568

69+
get getSkip(): number {
70+
return this.skip;
71+
}
72+
6673
}

0 commit comments

Comments
 (0)