File

src/app/modules/learn/components/course-consumption/course-consumption-page/course-consumption-page.component.ts

Implements

OnInit OnDestroy

Metadata

Index

Properties
Methods

Constructor

constructor(activatedRoute: ActivatedRoute, configService: ConfigService, courseConsumptionService: CourseConsumptionService, coursesService: CoursesService, toasterService: ToasterService, courseBatchService: CourseBatchService, resourceService: ResourceService, router: Router, groupsService: GroupsService, navigationHelperService: NavigationHelperService, permissionService: PermissionService, layoutService: LayoutService, generaliseLabelService: GeneraliseLabelService)
Parameters :
Name Type Optional
activatedRoute ActivatedRoute No
configService ConfigService No
courseConsumptionService CourseConsumptionService No
coursesService CoursesService No
toasterService ToasterService No
courseBatchService CourseBatchService No
resourceService ResourceService No
router Router No
groupsService GroupsService No
navigationHelperService NavigationHelperService No
permissionService PermissionService No
layoutService LayoutService No
generaliseLabelService GeneraliseLabelService No

Methods

Private checkCourseStatus
checkCourseStatus(courseHierarchy)
Parameters :
Name Optional
courseHierarchy No
Returns : void
Private getBatchDetailsFromEnrollList
getBatchDetailsFromEnrollList(enrolledCourses: [], undefined)
Parameters :
Name Type Optional Default value
enrolledCourses [] No []
No
Returns : any
Private getDetails
getDetails(queryParams)
Parameters :
Name Optional
queryParams No
Returns : any
getGeneraliseResourceBundle
getGeneraliseResourceBundle()
Returns : void
getGroupData
getGroupData()
Returns : void
Private handleEnrolledCourses
handleEnrolledCourses()
Returns : any
initLayout
initLayout()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void
refreshComponent
refreshComponent(isRouteChanged: boolean)
Parameters :
Name Type Optional
isRouteChanged boolean No
Returns : void
Private updateBreadCrumbs
updateBreadCrumbs()
Returns : void

Properties

Public batchId
Type : string
Public courseBatchService
Type : CourseBatchService
Public courseHierarchy
Type : any
Public courseId
Type : string
Public enrolledBatchInfo
Type : any
Private fetchEnrolledCourses$
Default value : new BehaviorSubject<boolean>(true)
Public generaliseLabelService
Type : GeneraliseLabelService
Public groupId
Type : string
layoutConfiguration
Public layoutService
Type : LayoutService
Public navigationHelperService
Type : NavigationHelperService
obs$
Public permissionService
Type : PermissionService
Public router
Type : Router
selectedCourseBatches
Type : literal type
Public showAddGroup
Type : null
Default value : null
showBatchInfo
Type : boolean
Public showError
Default value : false
Public showLoader
Default value : true
Public toasterService
Type : ToasterService
Public unsubscribe$
Default value : new Subject<void>()
import { combineLatest, Subject, throwError, BehaviorSubject } from 'rxjs';
import { map, mergeMap, first, takeUntil, delay, switchMap } from 'rxjs/operators';
import { ResourceService, ToasterService, ConfigService, NavigationHelperService, LayoutService } from '@sunbird/shared';
import { CourseConsumptionService, CourseBatchService } from './../../../services';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import * as _ from 'lodash-es';
import { CoursesService, PermissionService, GeneraliseLabelService } from '@sunbird/core';
import dayjs from 'dayjs';
import { GroupsService } from '../../../../groups/services/groups/groups.service';
@Component({
  templateUrl: './course-consumption-page.component.html'
})
export class CourseConsumptionPageComponent implements OnInit, OnDestroy {
  public courseId: string;
  public batchId: string;
  public showLoader = true;
  public showError = false;
  public courseHierarchy: any;
  public unsubscribe$ = new Subject<void>();
  public enrolledBatchInfo: any;
  public groupId: string;
  public showAddGroup = null;
  layoutConfiguration;
  showBatchInfo: boolean;
  selectedCourseBatches: { onGoingBatchCount: any; expiredBatchCount: any; openBatch: any; inviteOnlyBatch: any; courseId: any; };
  obs$;
  private fetchEnrolledCourses$ = new BehaviorSubject<boolean>(true);
  constructor(private activatedRoute: ActivatedRoute, private configService: ConfigService,
    private courseConsumptionService: CourseConsumptionService, private coursesService: CoursesService,
    public toasterService: ToasterService, public courseBatchService: CourseBatchService,
    private resourceService: ResourceService, public router: Router, private groupsService: GroupsService,
    public navigationHelperService: NavigationHelperService, public permissionService: PermissionService,
    public layoutService: LayoutService, public generaliseLabelService: GeneraliseLabelService) {
  }
  ngOnInit() {
    this.initLayout();
    this.fetchEnrolledCourses$.pipe(switchMap(this.handleEnrolledCourses.bind(this)))
      .subscribe(({ courseHierarchy, enrolledBatchDetails }: any) => {
        this.enrolledBatchInfo = enrolledBatchDetails;
        this.courseHierarchy = courseHierarchy;
        this.layoutService.updateSelectedContentType.emit(courseHierarchy.contentType);
        this.getGeneraliseResourceBundle();
        this.checkCourseStatus(courseHierarchy);
        this.updateBreadCrumbs();
        this.showLoader = false;
      }, err => {
        if (_.get(err, 'error.responseCode') && err.error.responseCode === 'RESOURCE_NOT_FOUND') {
          this.toasterService.error(this.generaliseLabelService.messages.emsg.m0002);
        } else {
          this.toasterService.error(this.resourceService.messages.fmsg.m0003); // fmsg.m0001 for enrolled issue
        }
        this.navigationHelperService.navigateToResource('/learn');
      });
  }

  private handleEnrolledCourses() {
    return this.coursesService.enrolledCourseData$.pipe(first(),
      mergeMap(({ enrolledCourses }) => {
        const routeParams: any = { ...this.activatedRoute.snapshot.params, ...this.activatedRoute.snapshot.firstChild.params };
        const queryParams = this.activatedRoute.snapshot.queryParams;
        this.courseId = routeParams.courseId;
        this.groupId = queryParams.groupId;

        if (this.groupId) {
          this.getGroupData();
        } else {
          this.showAddGroup = false;
        }
        const paramsObj = { params: this.configService.appConfig.CourseConsumption.contentApiQueryParams };
        const enrollCourses: any = this.getBatchDetailsFromEnrollList(enrolledCourses, routeParams);
        if (routeParams.batchId && !enrollCourses) { // batch not found in enrolled Batch list
          return throwError('ENROLL_BATCH_NOT_EXIST');
        }
        if (enrollCourses) { // batch found in enrolled list
          this.batchId = enrollCourses.batchId;
          if (enrollCourses.batchId !== routeParams.batchId) { // if batch from route dint match or not present
            this.router.navigate([`learn/course/${this.courseId}/batch/${this.batchId}`]); // but course was found in enroll list
          }
        } else {
          // if query params has batch and autoEnroll=true then auto enroll to that batch
          if (queryParams.batch && queryParams.autoEnroll) {
            if (this.permissionService.checkRolesPermissions(['COURSE_MENTOR'])) {
              this.router.navigate([`learn/course/${this.courseId}`]); // if user is mentor then redirect to course TOC page
            } else {
              const reqParams = {
                queryParams: { autoEnroll: queryParams.autoEnroll }
              };
              this.router.navigate([`learn/course/${this.courseId}/enroll/batch/${queryParams.batch}`], reqParams);
            }
          }
        }
        return this.getDetails(paramsObj);
      }), delay(200),
      takeUntil(this.unsubscribe$));
  }

  getGeneraliseResourceBundle() {
    this.resourceService.languageSelected$.pipe(takeUntil(this.unsubscribe$)).subscribe(item => {
      this.generaliseLabelService.initialize(this.courseHierarchy, item.value);
    });
  }

  initLayout() {
    this.layoutConfiguration = this.layoutService.initlayoutConfig();
    this.layoutService.switchableLayout().
      pipe(takeUntil(this.unsubscribe$)).subscribe(layoutConfig => {
        if (layoutConfig != null) {
          this.layoutConfiguration = layoutConfig.layout;
        }
      });
  }
  private getBatchDetailsFromEnrollList(enrolledCourses = [], { courseId, batchId }) {
    this.showBatchInfo = false;
    const allBatchesOfCourse = _.filter(enrolledCourses, { courseId })
      .sort((cur: any, prev: any) => dayjs(cur.enrolledDate).valueOf() > dayjs(prev.enrolledDate).valueOf() ? -1 : 1);
    const curBatch = _.find(allBatchesOfCourse, { batchId }); // find batch matching route batchId
    if (curBatch) { // activateRoute batch found
      return curBatch;
    }

    const { onGoingBatchCount, expiredBatchCount, openBatch, inviteOnlyBatch } = this.coursesService.findEnrolledCourses(courseId);
    if (!expiredBatchCount && !onGoingBatchCount) {
      return _.get(allBatchesOfCourse, '[0]') || null;
    } else {
      if (onGoingBatchCount === 1) {
        return _.get(openBatch, 'ongoing.length') ? _.get(openBatch, 'ongoing[0]') : _.get(inviteOnlyBatch, 'ongoing[0]');
      } else {
        this.selectedCourseBatches = { onGoingBatchCount, expiredBatchCount, openBatch, inviteOnlyBatch, courseId };
        this.showBatchInfo = true;
        return _.get(allBatchesOfCourse, '[0]') || null;
      }
    }
  }
  private getDetails(queryParams) {
    if (this.batchId) {
      return combineLatest(
        this.courseConsumptionService.getCourseHierarchy(this.courseId, queryParams),
        this.courseBatchService.getEnrolledBatchDetails(this.batchId)
      ).pipe(map(result => ({ courseHierarchy: result[0], enrolledBatchDetails: result[1] })));
    } else {
      return this.courseConsumptionService.getCourseHierarchy(this.courseId, queryParams)
        .pipe(map(courseHierarchy => ({ courseHierarchy })));
    }
  }
  private checkCourseStatus(courseHierarchy) {
    if (!['Live', 'Unlisted', 'Flagged'].includes(courseHierarchy.status)) {
      this.toasterService.warning(this.generaliseLabelService.messages.imsg.m0026);
      this.router.navigate(['/learn']);
    }
  }
  private updateBreadCrumbs() {
    if (this.batchId) {
      // this.breadcrumbsService.setBreadcrumbs([{
      //   label: this.courseHierarchy.name,
      //   url: '/learn/course/' + this.courseId + '/batch/' + this.batchId
      // }]);
    } else {
      // this.breadcrumbsService.setBreadcrumbs([{
      //   label: this.courseHierarchy.name,
      //   url: '/learn/course/' + this.courseId
      // }]);
    }
  }

  getGroupData() {
    this.groupsService.getGroupById(this.groupId, true, true).pipe(takeUntil(this.unsubscribe$)).subscribe(groupData => {
      this.groupsService.groupData = _.cloneDeep(groupData);
      this.showAddGroup = _.get(this.groupsService.addGroupFields(groupData), 'isAdmin');
    }, err => {
      this.showAddGroup = false;
      this.toasterService.error(this.resourceService.messages.emsg.m002);
    });
  }

  ngOnDestroy() {
    this.unsubscribe$.next();
    this.unsubscribe$.complete();
  }

  refreshComponent(isRouteChanged: boolean) {
    this.showBatchInfo = false;
    isRouteChanged && this.fetchEnrolledCourses$.next(true); // update component only if batch is changed.
  }
}
<app-landing-section [textToDisplay]="" [layoutConfiguration]="layoutConfiguration" [noTitle]="true">
</app-landing-section>
<app-course-consumption-header *ngIf="!showLoader && !showError && showAddGroup !== null"
    [courseHierarchy]="courseHierarchy" [enrolledBatchInfo]="enrolledBatchInfo" [groupId]="groupId"
    [showAddGroup]="showAddGroup" [layoutConfiguration]="layoutConfiguration"></app-course-consumption-header>
<div [ngClass]="layoutConfiguration ? 'sbt-inside-page-container' : ''">
<router-outlet *ngIf="!showLoader && !showError"></router-outlet>
<div *ngIf="showLoader" class="ui container">
    <div class="ui grid">
        <div class="twelve wide column">
            <app-loader></app-loader>
        </div>
    </div>
</div>
</div>
<app-batch-info *ngIf="showBatchInfo" [enrolledBatchInfo]="selectedCourseBatches"
    (routeChanged)="refreshComponent($event)">
</app-batch-info>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""