import { ChangeDetectionStrategy , Component , Input , OnDestroy , Inject , PLATFORM_ID , OnInit , ChangeDetectorRef , Injector , ElementRef , QueryList , ViewChildren , } from '@angular/core' ; import { FeedService } from './feed.service' ; import { ActivatedRoute , Router } from '@angular/router' ; import { ChannelsV2Service } from '../channels-v2.service' ; import { FeedFilterDateRange , FeedFilterType , } from '../../../../common/components/feed-filter/feed-filter.component' ; import { FeedsService } from '../../../../common/services/feeds.service' ; import { FeedsUpdateService } from '../../../../common/services/feeds-update.service' ; import { Observable , of , Subscription } from 'rxjs' ; import { isPlatformBrowser } from '@angular/common' ; import { Session } from '../../../../services/session' ; import { ThemeService } from '../../../../common/services/theme.service' ; import { ModalService } from '../../../composer/components/modal/modal.service' ; import { ComposerService } from '../../../composer/services/composer.service' ; import { catchError , take } from 'rxjs/operators' ; /**
 * Channel feed component
 */ @ Component ( { selector : 'm-channel__feed' , changeDetection : ChangeDetectionStrategy . OnPush , templateUrl : 'feed.component.html' , styleUrls : [ 'feed.component.ng.scss' ] , providers : [ FeedService , FeedsService , ComposerService ] , } ) export class ChannelFeedComponent implements OnDestroy , OnInit { private subscriptions : Subscription [ ] = [ ] ; @ ViewChildren ( 'feedViewChildren' , { read : ElementRef } ) feedViewChildren : QueryList < ElementRef > ; isGrid : boolean = false ; dateRangeEnabled : boolean = false ; @ Input ( 'layout' ) set _layout ( layout ) { this . isGrid = layout === 'grid' ; this . detectChanges ( ) ; } param view private @ Input ( 'view' ) set _view ( view ) { switch ( view ) { case 'activities' : case 'images' : case 'videos' : case 'blogs' : this . feedService . sort$ . next ( 'latest' ) ; this . feedService . type$ . next ( view ) ; break ; case 'scheduled' : this . feedService . sort$ . next ( 'scheduled' ) ; this . feedService . type$ . next ( 'activities' ) ; break ; default : this . feedService . sort$ . next ( 'latest' ) ; this . feedService . type$ . next ( 'activities' ) ; } } feed : Object [ ] = [ ] ; param feed param service param router constructor ( public feedService , public service , protected router , protected route , public feedsUpdate , private session , protected cd , private themesService , private composerModal , private injector , @ Inject ( PLATFORM_ID ) platformId ) { if ( isPlatformBrowser ( platformId ) ) { this . subscriptions . push ( this . service . guid$ . subscribe ( guid => { this . feedService . guid$ . next ( guid ) ; this . feedService . dateRange$ . next ( { fromDate : null , toDate : null } ) ; } ) ) ; } } returns { Observable < boolean > } get isDarkTheme$ ( ) : Observable < boolean > { return this . themesService . isDark$ ; } returns { boolean } get isOwnedChannel ( ) : boolean { return ( this . session . getLoggedInUser ( ) . guid === this . service . guid$ . getValue ( ) ) ; } ngOnInit ( ) { this . subscriptions . push ( this . feedService . service . feed . subscribe ( feed => { this . feed = feed ; } ) , this . feedsUpdate . postEmitter . subscribe ( newPost => { if ( this . feedService . guid$ . getValue ( ) === this . session . getLoggedInUser ( ) . guid ) { this . prepend ( newPost ) ; } } ) ) ; } prepend ( activity ) { if ( ! activity ) { return ; } let feedItem = { entity : activity , urn : activity . urn , guid : activity . guid , } ; this . feedService . service . rawFeed . next ( [ ... [ feedItem ] , ... this . feedService . service . rawFeed . getValue ( ) , ] ) ; } /**
     * Destroy lifecycle hook
     */ ngOnDestroy ( ) { for ( const subscription of this . subscriptions ) { subscription . unsubscribe ( ) ; } } param type onTypeChange ( type ) { const filter = type !== 'activities' ? type : '' ; this . router . navigate ( [ '/' , this . service . username$ . getValue ( ) , filter ] , { queryParamsHandling : 'preserve' , } ) ; } returns { Promise < void > } public async onFirstPostButtonClick ( ) { this . subscriptions . push ( this . feedService . type$ . pipe ( take ( 1 ) , catchError ( error => { console . error ( error ) ; return of ( null ) ; } ) ) . subscribe ( ( filter ) => { if ( filter === 'blogs' ) { this . router . navigate ( [ '/blog/v2/edit/new' ] ) ; return ; } this . openComposerModal ( ) ; } ) ) ; } returns { Promise < void > } public async openComposerModal ( ) { try { await this . composerModal . setInjector ( this . injector ) . present ( ) . toPromise ( ) ; } catch ( e ) { console . error ( e ) ; } } detectChanges ( ) { this . cd . markForCheck ( ) ; this . cd . detectChanges ( ) ; } returns public showInfiniteScroll ( ) { return ( this . feedService . service . inProgress . getValue ( ) || this . feedService . service . rawFeed . getValue ( ) . length > 0 ) ; } }