@ -1,6 +1,6 @@
import { ChangeDetectorRef , Component , Input , OnDestroy , OnInit , ViewChild , ViewEncapsulation } from "@angular/core" ;
import { FormArray , FormBuilder , FormGroup } from "@angular/forms" ;
import { User} from "../../login/utils/helper.class" ;
import { Role, User} from "../../login/utils/helper.class" ;
import { UserManagementService } from "../../services/user-management.service" ;
import { Subscription } from "rxjs" ;
import { NotificationService } from "../notification.service" ;
@ -22,7 +22,7 @@ import {NotificationHandler} from "../../utils/notification-handler";
< / div >
< / n g - t e m p l a t e >
< ng - template [ ngIf ] = " form.get ( ' groups ' ) & & availableGroups " >
< div # recipients input type = "chips" [ options ] = " availableG roups"
< div # recipients input type = "chips" [ options ] = " g roups"
placeholder = "Sent to:" hint = "Add a recipient" inputClass = "recipients" [ formInput ] = "form.get('groups')" >
< / div >
< div class = "uk-position-relative uk-margin-medium-top" >
@ -50,10 +50,11 @@ export class NotifyFormComponent implements OnInit, OnDestroy {
public form : FormGroup ;
@Input ( )
public availableGroups : Option [ ] = null ;
public groups : Option [ ] = [ ] ;
@Input ( ) service : string ;
public types : string [ ] = [ ] ;
public user : User ;
public focused : boolean = false ;
public groups : string [ ] = [ ] ;
@ViewChild ( 'recipients' , { static : false } ) recipients : InputComponent ;
private notification : Notification ;
private subscriptions : any [ ] = [ ] ;
@ -65,6 +66,12 @@ export class NotifyFormComponent implements OnInit, OnDestroy {
private notificationService : NotificationService ) {
}
@Input ( )
set entities ( entities : string [ ] ) {
this . types = entities . map ( entity = > Role . typeMember ( entity ) ) ;
this . types = this . types . concat ( entities . map ( entity = > Role . typeManager ( entity ) ) ) ;
}
ngOnInit() {
this . reset ( ) ;
this . subscriptions . push ( this . userManagementService . getUserInfo ( ) . subscribe ( user = > {
@ -96,11 +103,12 @@ export class NotifyFormComponent implements OnInit, OnDestroy {
groups : this.fb.array ( [ ] ) ,
message : this.fb.control ( message )
} ) ;
this . groups = [ ] ;
this . groups = this . availableGroups ;
this . subscriptions . push ( this . form . get ( 'groups' ) . valueChanges . subscribe ( value = > {
this . groups = [ ] ;
value . forEach ( group = > {
this . groups . push ( this . availableGroups . find ( available = > available . value === group ) . label ) ;
let typeGroups = this . typeGroups ;
this . groups = this . availableGroups . map ( group = > {
group . hidden = typeGroups . includes ( group . value )
return group ;
} ) ;
this . cdr . detectChanges ( ) ;
} ) ) ;
@ -111,7 +119,7 @@ export class NotifyFormComponent implements OnInit, OnDestroy {
if ( this . message ) {
if ( notification === null ) {
notification = new Notification ( 'CUSTOM' , [ this . service ] , null , null ) ;
notification . groups = this . groupsAsFromArray. value ;
notification . groups = this . parseGroups( ) ;
this . sending = true ;
}
this . notification = notification ;
@ -119,7 +127,7 @@ export class NotifyFormComponent implements OnInit, OnDestroy {
// TODO remove
this . notification . name = this . user . firstname ;
this . notification . surname = this . user . lastname ;
this . subscriptions . push ( this . notificationService . sendNotification ( this . notification ) . subscribe ( notification = > {
this . subscriptions . push ( this . notificationService . sendNotification ( this . notification ) . subscribe ( ( ) = > {
this . sending = false ;
NotificationHandler . rise ( 'A notification has been <b>sent</b> successfully' ) ;
this . reset ( ) ;
@ -131,6 +139,24 @@ export class NotifyFormComponent implements OnInit, OnDestroy {
}
}
parseGroups ( ) : string [ ] {
let typeGroups = this . typeGroups ;
return this . groupsAsFromArray . getRawValue ( ) . filter ( group = > ! this . types . includes ( group ) && ! typeGroups . includes ( group ) ) . concat ( typeGroups ) ;
}
get typeGroups ( ) : string [ ] {
let groups : string [ ] = [ ] ;
this . groupsAsFromArray . getRawValue ( ) . filter ( group = > this . types . includes ( group ) ) . forEach ( roleType = > {
if ( roleType . includes ( "MANAGER_" ) ) {
let type = roleType . replace ( "MANAGER_" , "" ) ;
groups = groups . concat ( this . availableGroups . map ( option = > option . value ) . filter ( group = > group . includes ( type ) && group . includes ( "_MANAGER" ) ) ) ;
} else {
groups = groups . concat ( this . availableGroups . map ( option = > option . value ) . filter ( group = > group . includes ( roleType + "_" ) && ! group . includes ( "_MANAGER" ) && ! group . includes ( "MANAGER_" ) ) ) ;
}
} ) ;
return groups ;
}
get groupsAsFromArray ( ) : FormArray {
return this . form . get ( 'groups' ) ? ( < FormArray > this . form . get ( 'groups' ) ) : null ;
}