Skip to content

Commit

Permalink
Task #58821: NA scenario dept field - org field made mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
christyfernandes committed Jul 7, 2022
1 parent e52c7f2 commit a70cfd9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ <h1 class="mat-h1 margin-remove inline-block ng-star-inserted">
<div class="input-wrapper margin-top-s">
<label for="Organisation" class=" margin-remove-bottom form-label"
i18n="Organisation|Label which explains the user to enter Organisation" i18n-aria-label
aria-label="Organisation Field label">
aria-label="Organisation Field label"
[ngClass]="{'required': orgRequired}">
Organisation
</label>
<mat-form-field appearance="outline" class="margin-top-s w-full">
Expand Down
67 changes: 58 additions & 9 deletions src/app/routes/public/public-signup/public-signup.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,54 @@ import { DOCUMENT, isPlatformBrowser } from '@angular/common'
// tslint:disable-next-line: import-name
import _ from 'lodash'

// export function forbiddenNamesValidator(optionsArray: any): ValidatorFn {
// return (control: AbstractControl): { [key: string]: any } | null => {
// if (!optionsArray) {
// return null
// // tslint:disable-next-line: no-else-after-return
// } else {
// const index = optionsArray.findIndex((op: any) => {
// // tslint:disable-next-line: prefer-template
// // return new RegExp('^' + op.channel + '$').test(control.channel)
// // return op.channel === control.value.channel
// return op.channel === control.value.channel
// })
// return index < 0 ? { forbiddenNames: { value: control.value.channel } } : null
// }
// }
// }

export function forbiddenNamesValidator(optionsArray: any): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } | null => {
if (!optionsArray) {
return null
// tslint:disable-next-line: no-else-after-return
} else {
const index = optionsArray.findIndex((op: any) => {
// tslint:disable-next-line: prefer-template
// return new RegExp('^' + op.channel + '$').test(control.channel)
return op.channel === control.value.channel
})
return index < 0 ? { forbiddenNames: { value: control.value.channel } } : null
if (control.value) {
const index = optionsArray.findIndex((op: any) => {
// tslint:disable-next-line: prefer-template
// return new RegExp('^' + op.orgname + '$').test(control.orgname)
return op.orgname === control.value.orgname
})
return index < 0 ? { forbiddenNames: { value: control.value.orgname } } : null
}
return null
}
}
}

export function forbiddenNamesValidatorNonEmpty(optionsArray: any): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } | null => {
if (!optionsArray) {
return null
// tslint:disable-next-line: no-else-after-return
} else {
const index = optionsArray.findIndex((op: any) => {
// tslint:disable-next-line: prefer-template
// return new RegExp('^' + op.orgname + '$').test(control.orgname)
return op.orgname === control.value.orgname
})
return index < 0 ? { forbiddenNames: { value: control.value.orgname } } : null
}
}
}
Expand Down Expand Up @@ -64,6 +100,7 @@ export class PublicSignupComponent implements OnInit, OnDestroy {
portalID = ''
confirm = false
disableBtn = false
orgRequired = false
ministeries: any[] = []
masterMinisteries!: Observable<any> | undefined
orgs: any[] = []
Expand Down Expand Up @@ -206,12 +243,11 @@ export class PublicSignupComponent implements OnInit, OnDestroy {
map(orgname => orgname ? this.filterOrgs(orgname) : this.orgs.slice())
)

this.masterOrgs.subscribe((event: any) => {
this.masterOrgs.subscribe((_event: any) => {
// tslint:disable-next-line: no-non-null-assertion
this.registrationForm.get('organisation')!.setValidators([forbiddenNamesValidator(event)])
// this.registrationForm.get('organisation')!.setValidators([forbiddenNamesValidator(event)])
// tslint:disable-next-line: no-non-null-assertion
// this.registrationForm.get('organisation')!.setValidators(null)
this.registrationForm.updateValueAndValidity()
})
}

Expand Down Expand Up @@ -388,9 +424,22 @@ export class PublicSignupComponent implements OnInit, OnDestroy {
if (res && res.result && res.result && res.result.response && res.result.response.content) {
this.orgs = res.result.response.content

// If value in department is NA then make the organisation field as required
// tslint:disable-next-line: no-non-null-assertion
// const value = this.registrationForm.get('department')!.value
if (value && (value.orgname === 'NA' || value.orgname === 'na')) {
this.orgRequired = true
// tslint:disable-next-line: no-non-null-assertion
this.registrationForm.get('organisation')!.setValidators([Validators.required, forbiddenNamesValidatorNonEmpty(this.orgs)])
} else {
this.orgRequired = false
// tslint:disable-next-line: no-non-null-assertion
this.registrationForm.get('organisation')!.setValidators([forbiddenNamesValidator(this.orgs)])
}
// to reset organisation values when department is changed
// tslint:disable-next-line: no-non-null-assertion
this.registrationForm.get('organisation')!.setValue('')
this.registrationForm.updateValueAndValidity()
this.onOrgsChange()
}
})
Expand Down

0 comments on commit a70cfd9

Please sign in to comment.