11import { Component , EventEmitter , Input , OnDestroy , OnInit , Output } from '@angular/core'
2- import { FormBuilder , FormControl , FormGroup , Validators } from '@angular/forms'
2+ import { AbstractControl , FormBuilder , FormControl , FormGroup , Validators } from '@angular/forms'
33import { MatSnackBar } from '@angular/material/snack-bar'
44import * as _ from 'lodash'
55import { CreateMDOService } from '../../../routes/home/services/create-mdo.services'
66import { ActivatedRoute } from '@angular/router'
77import { LoaderService } from '../../../routes/home/services/loader.service'
88import { IUploadedLogoresponse } from '../interface/interfaces'
9+ import { debounceTime , distinctUntilChanged , takeUntil } from 'rxjs/operators'
10+ import { Subject } from 'rxjs'
911@Component ( {
1012 selector : 'ws-app-create-organisation' ,
1113 templateUrl : './create-organisation.component.html' ,
@@ -14,6 +16,7 @@ import { IUploadedLogoresponse } from '../interface/interfaces'
1416export class CreateOrganisationComponent implements OnInit , OnDestroy {
1517
1618 @Input ( ) rowData : any
19+ @Input ( ) orgList : any [ ] = [ ]
1720 @Input ( ) dropdownList : {
1821 statesList : any [ ] ,
1922 ministriesList : any [ ]
@@ -31,14 +34,17 @@ export class CreateOrganisationComponent implements OnInit, OnDestroy {
3134 selectedLogo : any
3235 selectedLogoName = ''
3336 validFileTypes = [ 'image/png' , 'image/jpeg' , 'image/jpg' ]
34- maxFileSize = 500 // In KB
37+ maxFileSize = 5 // In MB
3538 loggedInUserId = ""
3639 isLoading = false
3740 filteredStates : any [ ] = [ ]
3841 filteredMinistry : any [ ] = [ ]
3942 heirarchyObject : any
4043 selectedLogoFile : any
4144 uploadedLogoResponse ! : IUploadedLogoresponse
45+ organizationNameList : string [ ] = [ ]
46+
47+ untilDestroyed$ = new Subject < void > ( ) ;
4248 constructor (
4349 private formBuilder : FormBuilder ,
4450 private snackBar : MatSnackBar ,
@@ -56,14 +62,18 @@ export class CreateOrganisationComponent implements OnInit, OnDestroy {
5662 if ( this . openMode === 'editMode' ) {
5763 this . getOrganization ( this . rowData . organisation , this . rowData . type . toLowerCase ( ) )
5864 }
65+
66+ this . organizationNameList = this . orgList . map ( org => org . organisation . trim ( ) . toLowerCase ( ) )
5967 }
6068
6169 ngOnDestroy ( ) : void {
6270 this . removeOverflowHidden ( )
71+
72+ this . untilDestroyed$ . next ( )
73+ this . untilDestroyed$ . complete ( )
6374 }
6475
6576 initialization ( ) {
66- console . log ( this . rowData )
6777
6878 if ( this . dropdownList ) {
6979 this . statesList = _ . get ( this . dropdownList , 'statesList' , [ ] )
@@ -85,6 +95,16 @@ export class CreateOrganisationComponent implements OnInit, OnDestroy {
8595 this . valueChangeEvents ( )
8696 }
8797
98+ createDuplicateOrgNameValidator ( organizationNameList : string [ ] ) {
99+ return ( control : AbstractControl ) => {
100+ if ( ! organizationNameList || ! control . value ) {
101+ return null
102+ }
103+ const isDuplicate = organizationNameList . includes ( control . value . trim ( ) . toLowerCase ( ) )
104+ return isDuplicate ? { duplicateOrgName : true } : null
105+ }
106+ }
107+
88108 get controls ( ) {
89109 return this . organisationForm . controls
90110 }
@@ -109,22 +129,36 @@ export class CreateOrganisationComponent implements OnInit, OnDestroy {
109129
110130 valueChangeEvents ( ) {
111131 if ( this . organisationForm && this . organisationForm . controls . category ) {
112- this . organisationForm . controls . category . valueChanges . subscribe ( val => {
113- if ( val === 'state' ) {
114- this . organisationForm . controls . state . setValidators ( [ Validators . required ] )
115- this . organisationForm . controls . state . updateValueAndValidity ( )
116- this . organisationForm . controls . ministry . setValue ( '' )
117- this . organisationForm . controls . ministry . clearValidators ( )
118- this . organisationForm . controls . ministry . updateValueAndValidity ( )
119- } else if ( val === 'ministry' ) {
120- this . organisationForm . controls . ministry . setValidators ( [ Validators . required ] )
121- this . organisationForm . controls . ministry . updateValueAndValidity ( )
122- this . organisationForm . controls . state . setValue ( '' )
123- this . organisationForm . controls . state . clearValidators ( )
124- this . organisationForm . controls . state . updateValueAndValidity ( )
132+ this . organisationForm . controls . category . valueChanges
133+ . pipe ( takeUntil ( this . untilDestroyed$ ) ) . subscribe ( val => {
134+ if ( val === 'state' ) {
135+ this . organisationForm . controls . state . setValidators ( [ Validators . required ] )
136+ this . organisationForm . controls . state . updateValueAndValidity ( )
137+ this . organisationForm . controls . ministry . setValue ( '' )
138+ this . organisationForm . controls . ministry . clearValidators ( )
139+ this . organisationForm . controls . ministry . updateValueAndValidity ( )
140+ } else if ( val === 'ministry' ) {
141+ this . organisationForm . controls . ministry . setValidators ( [ Validators . required ] )
142+ this . organisationForm . controls . ministry . updateValueAndValidity ( )
143+ this . organisationForm . controls . state . setValue ( '' )
144+ this . organisationForm . controls . state . clearValidators ( )
145+ this . organisationForm . controls . state . updateValueAndValidity ( )
146+ }
147+ } )
148+ }
149+
150+
151+ this . organisationForm . controls . organisationName . valueChanges
152+ . pipe ( takeUntil ( this . untilDestroyed$ ) , debounceTime ( 500 ) , distinctUntilChanged ( ) )
153+ . subscribe ( ( _value ) => {
154+ const control = this . organisationForm . controls . organisationName
155+ const error = this . createDuplicateOrgNameValidator ( this . organizationNameList ) ( control )
156+ if ( error ) {
157+ control . setErrors ( error )
158+ } else {
159+ control . setErrors ( null )
125160 }
126161 } )
127- }
128162 }
129163
130164 get getCategory ( ) {
@@ -216,15 +250,15 @@ export class CreateOrganisationComponent implements OnInit, OnDestroy {
216250 if ( input . files ?. length ) {
217251 this . selectedLogoFile = input . files [ 0 ]
218252 this . selectedLogoName = this . selectedLogoFile . name
219- const maxFileSize = this . maxFileSize * 1024
253+ const maxFileSize = this . maxFileSize * 1024 * 1024
220254
221255 if ( ! this . validFileTypes . includes ( this . selectedLogoFile . type ) ) {
222256 this . snackBar . open ( 'Invalid file type' , 'X' , { panelClass : [ 'error' ] } )
223257 return
224258 }
225259
226260 if ( this . selectedLogoFile . size > maxFileSize ) {
227- this . snackBar . open ( `File size exceeds ${ this . maxFileSize } KB . Please select a smaller file.` , 'X' , { panelClass : [ 'error' ] } )
261+ this . snackBar . open ( `File size exceeds ${ this . maxFileSize } MB . Please select a smaller file.` , 'X' , { panelClass : [ 'error' ] } )
228262 return
229263 }
230264 this . uploadOrganizationLogo ( )
0 commit comments