-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from moleculemaker/feat/add-validation
Feat/add validation
- Loading branch information
Showing
11 changed files
with
288 additions
and
151 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
src/app/components/novostoic/marvinjs-input/marvinjs-input.component.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 90 additions & 5 deletions
95
src/app/components/novostoic/marvinjs-input/marvinjs-input.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,100 @@ | ||
import { Component, EventEmitter, Input, Output } from "@angular/core"; | ||
import { BehaviorSubject } from "rxjs"; | ||
import { Component, EventEmitter, Input, Output, forwardRef } from "@angular/core"; | ||
import { AbstractControl, AsyncValidator, ControlValueAccessor, NG_ASYNC_VALIDATORS, NG_VALUE_ACCESSOR, ValidationErrors, Validator } from "@angular/forms"; | ||
import { BehaviorSubject, Observable, debounce, debounceTime, filter, map, switchMap, take } from "rxjs"; | ||
import { NovostoicService } from "~/app/services/novostoic.service"; | ||
|
||
@Component({ | ||
selector: "app-marvinjs-input", | ||
templateUrl: "./marvinjs-input.component.html", | ||
styleUrls: ["./marvinjs-input.component.scss"], | ||
providers: [ | ||
{ | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: forwardRef(() => MarvinjsInputComponent), | ||
multi: true | ||
}, | ||
{ | ||
provide: NG_ASYNC_VALIDATORS, | ||
useExisting: forwardRef(() => MarvinjsInputComponent), | ||
multi: true | ||
} | ||
] | ||
}) | ||
export class MarvinjsInputComponent { | ||
export class MarvinjsInputComponent implements ControlValueAccessor, AsyncValidator { | ||
@Input() placeholder: string = ""; | ||
@Input() smiles: string = ""; | ||
@Output() smilesChange = new EventEmitter(); | ||
@Input() errors: ValidationErrors | null; | ||
@Input() dirty: boolean = false; | ||
|
||
_value = ""; | ||
get value() { | ||
return this._value; | ||
} | ||
set value(value: string) { | ||
this._value = value; | ||
this.onChange(value); | ||
this.onTouched(); | ||
} | ||
|
||
_smiles = ""; | ||
get smiles() { | ||
return this._smiles; | ||
} | ||
|
||
set smiles(value: string) { | ||
this._smiles = value; | ||
if (this.value !== value) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
showDialog$ = new BehaviorSubject(false); | ||
|
||
constructor(private novostoicService: NovostoicService) {} | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
/* Control Value Accessor Interface */ | ||
/* -------------------------------------------------------------------------- */ | ||
disabled = false; | ||
onChange = (value: string) => {}; | ||
onTouched = () => {}; | ||
|
||
writeValue(obj: string): void { | ||
this.value = obj; | ||
} | ||
|
||
registerOnChange(fn: any): void { | ||
this.onChange = fn; | ||
} | ||
|
||
registerOnTouched(fn: any): void { | ||
this.onTouched = fn; | ||
} | ||
|
||
setDisabledState?(isDisabled: boolean): void { | ||
this.disabled = isDisabled; | ||
} | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
/* Async Validator Interface */ | ||
/* -------------------------------------------------------------------------- */ | ||
onValidatorChange = () => {}; | ||
|
||
validate(control: AbstractControl<any, any>): Promise<ValidationErrors | null> | Observable<ValidationErrors | null> { | ||
return control.valueChanges.pipe( | ||
debounceTime(500), | ||
switchMap((v) => this.novostoicService.validateChemical(v)), | ||
map((chemical) => { | ||
if (chemical) { | ||
this.smiles = chemical.smiles; | ||
return null; | ||
} | ||
return { chemicalNotSupported: true }; | ||
}), | ||
take(1), | ||
); | ||
} | ||
|
||
registerOnValidatorChange?(fn: () => void): void { | ||
this.onValidatorChange = fn; | ||
} | ||
} |
Oops, something went wrong.