11import { Component , OnInit , ViewEncapsulation } from '@angular/core' ;
22import { ActivatedRoute , Router } from '@angular/router' ;
3-
4- import { FeedService } from '@services/feed.service' ;
3+ import { debounceTime , forkJoin , Subject } from 'rxjs' ;
54
65import { 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}
0 commit comments