import { Component , OnInit , Output , Input , ViewEncapsulation , EventEmitter } from '@angular/core' ; import { DragulaService } from 'ng2-dragula' ; import { IWhitelistUser } from '../../../../../../../../shared/models/IWhitelistUser' ; import { ICourse } from '../../../../../../../../shared/models/ICourse' ; import { CourseService , UserDataService , WhitelistUserService } from '../../../../shared/services/data.service' ; import { ProgressService } from '../../../../shared/services/data/progress.service' ; import { ShowProgressService } from '../../../../shared/services/show-progress.service' ; import { IUser } from '../../../../../../../../shared/models/IUser' ; @ Component ( { selector : 'app-whitelist-show' , templateUrl : './whitelist-show.component.html' , styleUrls : [ './whitelist-show.component.scss' ] , encapsulation : ViewEncapsulation . None } ) export class WhitelistShowComponent implements OnInit { @ Output ( ) onDragendRemoveWhitelist = new EventEmitter < IWhitelistUser > ( ) ; @ Input ( ) course : ICourse ; @ Input ( ) show : boolean ; signedInStudents : IUser [ ] ; constructor ( private courseService , private showProgress , private userService , private whitelistUserService ) { } ngOnInit ( ) { this . userService . searchUsers ( 'student' , '' ) . then ( ( res ) => { this . signedInStudents = res . users ; } ) ; } updateCourse ( ) { this . showProgress . toggleLoadingGlobal ( true ) ; this . courseService . updateItem ( { '_id' : this . course . _id , 'whitelist' : this . course . whitelist . map ( ( wUser ) => wUser . _id ) } ) . then ( ( ) => { this . showProgress . toggleLoadingGlobal ( false ) ; } ) ; } async remove ( user ) { const idList = this . course . whitelist . map ( ( u ) => u . _id ) ; const index = idList . indexOf ( user . _id ) ; this . course . whitelist . splice ( index , 1 ) ; const idListCourse = this . course . whitelist . map ( ( c ) => c . _id ) ; const indexCourse = idListCourse . indexOf ( user . _id ) ; if ( indexCourse >= 0 ) { this . course . whitelist . splice ( indexCourse , 1 ) ; this . updateCourse ( ) ; } await this . whitelistUserService . deleteItem ( user ) ; this . onDragendRemoveWhitelist . emit ( user ) ; } isInCourse ( user ) { if ( this . course ) { const idListCourse = this . course . students . map ( ( u ) => u . profile . firstName . toLowerCase ( ) + u . profile . lastName . toLowerCase ( ) + u . uid ) ; return idListCourse . indexOf ( user . firstName + user . lastName + user . uid ) >= 0 ; } return false ; } isSignedIn ( user ) { if ( this . signedInStudents ) { const idListCourse = this . signedInStudents . map ( ( u ) => u . profile . firstName . toLowerCase ( ) + u . profile . lastName . toLowerCase ( ) + u . uid ) ; return idListCourse . indexOf ( user . firstName + user . lastName + user . uid ) >= 0 ; } return false ; } }