import { ChangeDetectorRef , Component , OnDestroy , OnInit } from '@angular/core' ; import { GroupService } from "../../services/groups/group.service" ; import { UtilsService } from "../../services/utils.service" ; import { MembersActionsComponent } from "../../secondary-components/members-actions/members-actions.component" ; import { ActivatedRoute , Router } from "@angular/router" ; import * as _ from "lodash" ; import { TableHeader } from "../../models/table-header.model" ; import { ContextService } from "../../services/context.service" ; import { GroupsUtilsService } from "../../services/groups/groups-utils.service" ; import { User } from "../../models/user.model" ; @ Component ( { selector : 'app-members' , templateUrl : './members.component.html' , styleUrls : [ './members.component.scss' ] } ) export class MembersComponent { public group ; public members ; public tableConfig ; public user ; public isOwner ; public confirmModalConfig : any = { } ; public returnLink = '../../' ; public modal = '' ; public showModal = false ; constructor ( private activatedRoute , private utils , private router , private groupUtils , private context , private groupService , private chRef ) { this . user = this . context . getUser ( ) ; this . group = this . activatedRoute . snapshot . data [ 'membersResolve' ] . group ; this . members = this . activatedRoute . snapshot . data [ 'membersResolve' ] . members ; this . isOwner = this . groupUtils . userIsOwner ( this . user . id , this . group ) ; this . tableConfig = { selectable : true , selection : [ ] , headers : [ new TableHeader ( 'Name' , 'name' , 'string' ) , new TableHeader ( 'ID' , 'id' , 'string' ) , new TableHeader ( 'Status' , 'status' , 'string' ) ] , actionsComponent : MembersActionsComponent } ; this . processUsers ( ) ; } leaveGroup ( ) { this . confirmModalConfig = { confirmMessage : 'Are you sure you want to leave this group?' , onConfirm : ( ) => { this . groupService . leaveGroup ( 0 , 0 ) . then ( ( response ) => { this . groupsLink ( ) ; } ) ; } } ; this . confirmModal ( ) ; } promoteAllToOwner ( ) { this . confirmModalConfig = { confirmMessage : 'Promote (' + this . tableConfig . selection . length + ') user(s) to owner of ' + this . group . name + '?' , onConfirm : ( ) => { this . groupService . promoteAllToOwner ( this . tableConfig . selection ) . then ( ( responses ) => { _ ( this . members ) . filter ( ( member ) => { return _ . find ( this . tableConfig . selection , { id : member . id } ) ; } ) . map ( ( member ) => { member . status = 'Owner' ; return member ; } ) . value ( ) ; this . tableConfig . selection = [ ] ; } ) ; } } ; this . confirmModal ( ) ; } removeAllFromGroup ( ) { this . confirmModalConfig = { confirmMessage : 'Remove (' + this . tableConfig . selection . length + ') user(s) from ' + this . group . name + '?' , onConfirm : ( ) => { this . groupService . removeAllFromGroup ( this . tableConfig . selection ) . then ( ( responses ) => { _ . remove ( this . members , ( member , index , array ) => { return _ . find ( this . tableConfig . selection , ( selectionElement ) => { return selectionElement . id === member . id ; } ) ; } ) ; this . tableConfig . selection = [ ] ; } ) ; } } ; this . confirmModal ( ) ; } groupsLink ( ) { this . router . navigate ( [ this . returnLink ] , { relativeTo : this . activatedRoute } ) ; } confirmModal ( ) { this . modal = 'confirm' ; this . showModal = true ; } addMemberModal ( ) { this . modal = 'add-member' ; this . showModal = true ; } requestAccessModal ( ) { console . log ( 'called' ) ; this . modal = 'request-access' ; this . showModal = true ; } addToGroup ( user ) { this . group . members . push ( user ) ; } processUsers ( ) { for ( let i = 0 ; i < this . members . length ; i += 1 ) { let user = this . members [ i ] ; let status = this . groupUtils . userIsMemOwnAdminOfGroup ( this . members [ i ] . id , this . group ) ; user . status = status ; } } alreadyRequested ( ) { return false ; } }