File

src/app/modules/public/module/explore/components/explore-content/explore-content.component.ts

Metadata

templateUrl explore-content.component.html

Constructor

constructor(searchService: any, router: Router, activatedRoute: ActivatedRoute, paginationService: any, resourceService: any, toasterService: any, configService: any, utilService: any, orgDetailsService: any, navigationHelperService: any, publicPlayerService: PublicPlayerService, userService: any, frameworkService: any, cacheService: CacheService, navigationhelperService: any)

Methods

Public getFilters
getFilters(filters: any)
Returns: void
Private fetchContentOnParamChange
fetchContentOnParamChange()
Returns: void
Private fetchContents
fetchContents()
Returns: void
Public navigateToPage
navigateToPage(page: number)
Returns: void
Private setTelemetryData
setTelemetryData()
Returns: void
Public playContent
playContent(event: any)
Returns: void
Public inView
inView(event: any)
Returns: void
Private setNoResultMessage
setNoResultMessage()
Returns: void
updateCardData
updateCardData(downloadListdata: any)
Returns: void

Properties

activatedRoute
activatedRoute: ActivatedRoute
Public baseUrl
baseUrl: string
cacheService
cacheService: CacheService
Public cardIntractEdata
cardIntractEdata: any
configService
configService: any
Public contentList
contentList: any[]
contentName
contentName: string
Public dataDrivenFilterEvent
dataDrivenFilterEvent: EventEmitter<any>
Public dataDrivenFilters
dataDrivenFilters: any
Public facets
facets: string[]
Public facetsList
facetsList: any
Public filterType
filterType: string
frameworkService
frameworkService: any
Public hashTagId
hashTagId: string
Public initFilters
initFilters: boolean
Default value: false
Public inViewLogs
inViewLogs: any[]
isOffline
isOffline: boolean
Public loaderMessage
loaderMessage: any
navigationhelperService
navigationhelperService: any
navigationHelperService
navigationHelperService: any
Public noResultMessage
noResultMessage: any
orgDetailsService
orgDetailsService: any
Public paginationDetails
paginationDetails: any
paginationService
paginationService: any
Public queryParams
queryParams: any
resourceService
resourceService: any
router
router: Router
searchService
searchService: any
showDownloadLoader
showDownloadLoader: boolean
Default value: false
showExportLoader
showExportLoader: boolean
Default value: false
Public showLoader
showLoader: boolean
Default value: true
Public showLoginModal
showLoginModal: boolean
Default value: false
Public sortIntractEdata
sortIntractEdata: any
Public telemetryImpression
telemetryImpression: any
toasterService
toasterService: any
Public unsubscribe$
unsubscribe$: Subject<void>
userService
userService: any
utilService
utilService: any
import {
  PaginationService, ResourceService, ConfigService, ToasterService, INoResultMessage,
  ICard, ILoaderMessage, UtilService, NavigationHelperService
} from '@sunbird/shared';
import { SearchService, PlayerService, OrgDetailsService, UserService, FrameworkService } from '@sunbird/core';
import { PublicPlayerService } from '../../../../services';
import { combineLatest, Subject } from 'rxjs';
import { Component, OnInit, OnDestroy, EventEmitter, AfterViewInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import * as _ from 'lodash-es';
import { IInteractEventEdata, IImpressionEventInput } from '@sunbird/telemetry';
import { takeUntil, map, mergeMap, first, filter, debounceTime, tap, delay } from 'rxjs/operators';
import { CacheService } from '../../../../../shared/services/cache-service/cache.service';
import { environment } from '@sunbird/environment';

@Component({
  templateUrl: './explore-content.component.html'
})
export class ExploreContentComponent implements OnInit, OnDestroy, AfterViewInit {

  public showLoader = true;
  public showLoginModal = false;
  public baseUrl: string;
  public noResultMessage: INoResultMessage;
  public filterType: string;
  public queryParams: any;
  public hashTagId: string;
  public unsubscribe$ = new Subject<void>();
  public telemetryImpression: IImpressionEventInput;
  public inViewLogs = [];
  public sortIntractEdata: IInteractEventEdata;
  public dataDrivenFilters: any = {};
  public dataDrivenFilterEvent = new EventEmitter();
  public initFilters = false;
  public facets: Array<string>;
  public facetsList: any;
  public paginationDetails;
  public contentList: Array<ICard> = [];
  public cardIntractEdata: IInteractEventEdata;
  public loaderMessage: ILoaderMessage;
  isOffline: boolean = environment.isOffline;
  showExportLoader = false;
  contentName: string;
  showDownloadLoader = false;

  constructor(public searchService: SearchService, public router: Router,
    public activatedRoute: ActivatedRoute, public paginationService: PaginationService,
    public resourceService: ResourceService, public toasterService: ToasterService,
    public configService: ConfigService, public utilService: UtilService, public orgDetailsService: OrgDetailsService,
    public navigationHelperService: NavigationHelperService, private publicPlayerService: PublicPlayerService,
    public userService: UserService, public frameworkService: FrameworkService,
    public cacheService: CacheService, public navigationhelperService: NavigationHelperService) {
    this.paginationDetails = this.paginationService.getPager(0, 1, this.configService.appConfig.SEARCH.PAGE_LIMIT);
    this.filterType = this.configService.appConfig.explore.filterType;
  }
  ngOnInit() {
    this.orgDetailsService.getOrgDetails(this.activatedRoute.snapshot.params.slug).pipe(
      mergeMap((orgDetails: any) => {
        this.hashTagId = orgDetails.hashTagId;
        this.initFilters = true;
        return this.dataDrivenFilterEvent;
      }), first()
    ).subscribe((filters: any) => {
      this.dataDrivenFilters = filters;
      this.fetchContentOnParamChange();
      this.setNoResultMessage();
    },
      error => {
        this.router.navigate(['']);
      }
    );
  }
  public getFilters(filters) {
    this.facets = filters.map(element => element.code);
    const defaultFilters = _.reduce(filters, (collector: any, element) => {
      if (element.code === 'board') {
        collector.board = _.get(_.orderBy(element.range, ['index'], ['asc']), '[0].name') || '';
      }
      return collector;
    }, {});
    this.dataDrivenFilterEvent.emit(defaultFilters);
  }
  private fetchContentOnParamChange() {
    combineLatest(this.activatedRoute.params, this.activatedRoute.queryParams)
      .pipe(debounceTime(5),
        tap(data => this.inView({ inview: [] })),
        delay(10),
        tap(data => this.setTelemetryData()),
        map(result => ({ params: { pageNumber: Number(result[0].pageNumber) }, queryParams: result[1] })),
        takeUntil(this.unsubscribe$)
      ).subscribe(({ params, queryParams }) => {
        this.showLoader = true;
        this.paginationDetails.currentPage = params.pageNumber;
        this.queryParams = { ...queryParams };
        this.contentList = [];
        this.fetchContents();
      });
  }
  private fetchContents() {
    let filters = _.pickBy(this.queryParams, (value: Array<string> | string) => value && value.length);
    filters = _.omit(filters, ['key', 'sort_by', 'sortType', 'appliedFilters']);
    const softConstraintData: any = {
      filters: {
        channel: this.hashTagId,
      },
      softConstraints: _.get(this.activatedRoute.snapshot, 'data.softConstraints'),
      mode: 'soft'
    };
    if (this.dataDrivenFilters.board) {
      softConstraintData.board = [this.dataDrivenFilters.board];
    }
    const manipulatedData = this.utilService.manipulateSoftConstraint(_.get(this.queryParams,
      'appliedFilters'), softConstraintData);
    const option = {
      filters: _.get(this.queryParams, 'appliedFilters') ? filters : manipulatedData.filters,
      limit: this.configService.appConfig.SEARCH.PAGE_LIMIT,
      pageNumber: this.paginationDetails.currentPage,
      query: this.queryParams.key,
      mode: _.get(manipulatedData, 'mode'),
      facets: this.facets,
      params: this.configService.appConfig.ExplorePage.contentApiQueryParams
    };
    option.filters.contentType = filters.contentType ||
      ['Collection', 'TextBook', 'LessonPlan', 'Resource'];
    if (manipulatedData.filters) {
      option['softConstraints'] = _.get(manipulatedData, 'softConstraints');
    }
    this.frameworkService.channelData$.subscribe((channelData) => {
      if (!channelData.err) {
        option.params.framework = _.get(channelData, 'channelData.defaultFramework');
      }
    });
    this.searchService.contentSearch(option)
      .subscribe(data => {
        this.showLoader = false;
        this.facetsList = this.searchService.processFilterData(_.get(data, 'result.facets'));
        this.paginationDetails = this.paginationService.getPager(data.result.count, this.paginationDetails.currentPage,
          this.configService.appConfig.SEARCH.PAGE_LIMIT);
        const { constantData, metaData, dynamicFields } = this.configService.appConfig.LibrarySearch;
        this.contentList = this.utilService.getDataForCard(data.result.content, constantData, dynamicFields, metaData);
      }, err => {
        this.showLoader = false;
        this.contentList = [];
        this.facetsList = [];
        this.paginationDetails = this.paginationService.getPager(0, this.paginationDetails.currentPage,
          this.configService.appConfig.SEARCH.PAGE_LIMIT);
        this.toasterService.error(this.resourceService.messages.fmsg.m0051);
      });
  }
  public navigateToPage(page: number): void {
    if (page < 1 || page > this.paginationDetails.totalPages) {
      return;
    }
    const url = this.router.url.split('?')[0].replace(/[^\/]+$/, page.toString());
    this.router.navigate([url], { queryParams: this.queryParams });
    window.scroll({
      top: 0,
      left: 0,
      behavior: 'smooth'
    });
  }
  private setTelemetryData() {
    this.inViewLogs = []; // set to empty every time filter or page changes
    this.telemetryImpression = {
      context: {
        env: this.activatedRoute.snapshot.data.telemetry.env
      },
      edata: {
        type: this.activatedRoute.snapshot.data.telemetry.type,
        pageid: this.activatedRoute.snapshot.data.telemetry.pageid,
        uri: this.router.url,
        subtype: this.activatedRoute.snapshot.data.telemetry.subtype,
        duration: this.navigationhelperService.getPageLoadTime()
      }
    };
    this.cardIntractEdata = {
      id: 'content-card',
      type: 'click',
      pageid: this.activatedRoute.snapshot.data.telemetry.pageid
    };
  }
  public playContent(event) {

    if (!this.userService.loggedIn && event.data.contentType === 'Course') {
      this.showLoginModal = true;
      this.baseUrl = '/' + 'learn' + '/' + 'course' + '/' + event.data.metaData.identifier;
    } else {
      if (_.includes(this.router.url, 'browse') && this.isOffline) {
        this.publicPlayerService.playContentForOfflineBrowse(event);
      } else {
        this.publicPlayerService.playContent(event);
      }
    }
  }
  public inView(event) {
    _.forEach(event.inview, (elem, key) => {
      const obj = _.find(this.inViewLogs, { objid: elem.data.metaData.identifier });
      if (!obj) {
        this.inViewLogs.push({
          objid: elem.data.metaData.identifier,
          objtype: elem.data.metaData.contentType || 'content',
          index: elem.id
        });
      }
    });

    if (this.telemetryImpression) {
      this.telemetryImpression.edata.visits = this.inViewLogs;
      this.telemetryImpression.edata.subtype = 'pageexit';
      this.telemetryImpression = Object.assign({}, this.telemetryImpression);
    }
  }
  ngAfterViewInit() {
    setTimeout(() => {
      this.setTelemetryData();
      this.inView({ inview: [] });
    });
  }
  ngOnDestroy() {
    this.unsubscribe$.next();
    this.unsubscribe$.complete();
  }
  private setNoResultMessage() {
    if (this.isOffline && !(this.router.url.includes('/browse'))) {
      this.noResultMessage = {
        'message': 'messages.stmsg.m0007',
        'messageText': 'messages.stmsg.m0133'
      };
    } else {
      this.noResultMessage = {
        'message': 'messages.stmsg.m0007',
        'messageText': 'messages.stmsg.m0006'
      };
    }
  }

  updateCardData(downloadListdata) {
    _.each(this.contentList, (contents) => {
      this.publicPlayerService.updateDownloadStatus(downloadListdata, contents);
    });
  }
}

results matching ""

    No results matching ""