Skip to content

Commit

Permalink
Merge pull request #256 from asumandang/fix-draft-form
Browse files Browse the repository at this point in the history
Unexpected behavior on "draft poll" page
  • Loading branch information
mensch72 authored Nov 11, 2023
2 parents db6c4d1 + 143fec3 commit 58216e1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/app/draftpoll/draftpoll.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { environment } from 'src/environments/environment';

import { unique_name_validator$ } from '../sharedcomponents/unique-form-validator';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators'
import { map, startWith } from 'rxjs/operators'

type option_data_t = { oid?, name?, desc?, url?, ratings? };

Expand Down Expand Up @@ -863,6 +863,7 @@ export class DraftpollPage implements OnInit {

existingOptionName$(currentControlName: string): Observable<string[]> {
return this.formGroup.valueChanges.pipe(
startWith({}),
map(values => {
const optionNameKeys = Object.keys(values as Object).filter(k => { return k.includes('option_name') && k !== currentControlName })
const existingOptionNames: string[] = [];
Expand Down
22 changes: 16 additions & 6 deletions src/app/sharedcomponents/unique-form-validator.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import { AbstractControl, ValidationErrors, AsyncValidatorFn } from '@angular/forms';
import { Observable } from 'rxjs';
import { map, take } from 'rxjs/operators'
import {
AbstractControl,
ValidationErrors,
AsyncValidatorFn,
} from "@angular/forms";
import { Observable, of } from "rxjs";
import { map, take } from "rxjs/operators";

/** The option-name has to be unique */

export function unique_name_validator$(value$: Observable<string[]>): AsyncValidatorFn {
export function unique_name_validator$(
value$: Observable<string[]>
): AsyncValidatorFn {
return (control: AbstractControl): Observable<ValidationErrors | null> => {
if (!control.valueChanges || control.pristine) {
return of(null);
}

return value$.pipe(
map(names => {
map((names) => {
return names.includes(control.value) ? { not_unique: true } : null;
}),
take(1)
)
);
};
}

0 comments on commit 58216e1

Please sign in to comment.