import { Component } from '@angular/core' ; import { FormControl , FormGroup , Validators } from '@angular/forms' ; import { EdaDialog , EdaDialogAbstract , EdaDialogCloseEvent } from '@eda/shared/components/shared-components.index' ; import { AlertService , GroupService , UserService } from '@eda/services/service.index' ; import { Group } from '@eda/models/user-models/group-model' ; import { IGroup } from '@eda/services/api/group.service' ; import * as _ from 'lodash' ; @ Component ( { selector : 'app-group-detail' , templateUrl : './group-detail.component.html' } ) export class GroupDetailComponent extends EdaDialogAbstract { public dialog : EdaDialog ; public group : Group ; public form : FormGroup ; public lists = { avaibles : [ ] , selecteds : [ ] } ; public roles : any [ ] ; public imageUpload : File ; public imageTemp : any ; constructor ( private userService , private groupService , private alertService ) { super ( ) ; this . dialog = new EdaDialog ( { show : ( ) => this . onShow ( ) , hide : ( ) => this . onClose ( EdaDialogCloseEvent . NONE ) , title : 'CREAR NUEVO GRUPO' } ) ; this . form = new FormGroup ( { name : new FormControl ( null , Validators . required ) , role : new FormControl ( null , Validators . required ) , img : new FormControl ( null ) , users : new FormControl ( null ) , } ) ; this . roles = [ { label : 'USER' , value : 'USER_ROLE' } , { label : 'ADMIN' , value : 'ADMIN_ROLE' } ] ; this . lists . avaibles = [ ] ; this . lists . selecteds = [ ] ; } onShow ( ) { this . load ( ) ; } load ( ) { this . userService . getUsers ( ) . subscribe ( users => { this . lists . avaibles = users ; if ( this . controller . params . id ) { this . dialog . setTitle ( `GRUPO: ${ this . controller . params . name }` ) ; this . groupService . getGroup ( this . controller . params . id ) . subscribe ( group => { this . group = group ; for ( const user of group . users ) { this . lists . selecteds . push ( _ . find ( this . lists . avaibles , userA => _ . isEqual ( user . _id , userA . _id ) ) ) ; } this . lists . avaibles = _ . difference ( this . lists . avaibles , this . lists . selecteds ) ; this . form . patchValue ( { name : group . name , role : _ . find ( this . roles , r => _ . isEqual ( r . value , group . role ) ) } ) ; } , err => this . alertService . addError ( err ) ) ; } } , err => this . alertService . addError ( err ) ) ; } selectImage ( file ) { if ( ! file ) { this . imageUpload = null ; return ; } if ( file . type . indexOf ( 'image' ) < 0 ) { this . alertService . addError ( 'El archivo seleccionado no es una imagen' ) ; this . imageUpload = null ; return ; } this . imageUpload = file ; const reader = new FileReader ( ) ; const urlImageTemp = reader . readAsDataURL ( file ) ; reader . onloadend = ( ) => this . imageTemp = reader . result ; } save ( ) { this . form . controls [ 'role' ] . setValue ( 'USER_ROLE' ) ; if ( this . form . valid ) { const group = { name : this . form . value . name , role : this . form . value . role , img : this . imageUpload , users : this . lists . selecteds . map ( user => user . _id ) } ; if ( ! this . controller . params . id ) { this . groupService . insertGroup ( group ) . subscribe ( ( ) => this . onClose ( EdaDialogCloseEvent . NEW ) , err => this . alertService . addError ( err ) ) ; } else { this . groupService . updateGroup ( this . controller . params . id , group ) . subscribe ( ( ) => this . onClose ( EdaDialogCloseEvent . UPDATE ) , err => this . alertService . addError ( err ) ) ; } } } closeDialog ( ) { this . onClose ( EdaDialogCloseEvent . NONE ) ; } onClose ( event , response ? ) { return this . controller . close ( event ) ; } }