-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnews-container.component.ts
79 lines (67 loc) · 2.17 KB
/
news-container.component.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { Router } from '@angular/router';
import { Subject, debounceTime } from 'rxjs';
import { OptionFeed } from '@customTypes/option.type';
import { AuthService } from '@services/auth.service';
import { UserService } from '@services/user.service';
import { Feed } from '@models/feed.model';
@Component({
selector: 'app-news-container',
templateUrl: './news-container.component.html',
styleUrls: ['./news-container.component.css']
})
export class NewsContainerComponent implements OnInit {
private isAuthenticated = false;
@Input() feeds: Feed[] = [];
@Input() recentFeed: Feed;
@Output() moreItems: EventEmitter<boolean> = new EventEmitter();
private saveFeedSub: Subject<{id: string, option: OptionFeed}> = new Subject();
constructor(
private router: Router,
private authService: AuthService,
private userService: UserService,
) {
this.saveFeedSub.pipe(
debounceTime(300))
.subscribe(({id, option}) => this.updatePreferences(id, option)
);
}
ngOnInit(): void {
this.isAuthenticated = this.authService.isAuthenticated();
}
moreDetails(id: string): void {
this.router.navigate([`/feed/${id}`]);
}
modifyPreference(id: string, option: OptionFeed): void {
if(!this.isAuthenticated) {
return this.authService.showModalAuth('init');
}
this.saveFeedSub.next({id, option});
}
updatePreferences(idFeed: string, option: OptionFeed): void {
this.userService.modifyPreferences(idFeed, option).subscribe(() => {
this.feeds.map(feed => {
if(option === 'saved' && feed._id === idFeed) {
feed.inUser = !feed.inUser;
}
if(option === 'liked' && feed._id === idFeed) {
feed.liked = !feed.liked;
feed.likes += feed.liked ? 1 : -1;
}
return feed;
});
});
}
changeStyle(element: HTMLElement, strClass: string, change: boolean): void {
if(change) {
element.classList.add(strClass);
element.style.transition = 'all 0.5s';
}
else {
element.classList.remove(strClass);
}
}
onScroll(): void {
this.moreItems.emit(true);
}
}