|
| 1 | +import { ChangeDetectionStrategy, Component, Input } from "@angular/core"; |
| 2 | +import { FormBuilder, FormGroup, Validators } from "@angular/forms"; |
| 3 | +import { BehaviorSubject } from "rxjs"; |
| 4 | +import Resolution from "../../@models/resolution.model"; |
| 5 | +import { ApiService } from "../../@services/api.service"; |
| 6 | + |
| 7 | +@Component({ |
| 8 | + selector: "lib-utask-resoution-expression", |
| 9 | + templateUrl: "./resolution-expression.html", |
| 10 | + styleUrls: ["./resolution-expression.sass"], |
| 11 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 12 | +}) |
| 13 | +export class ResolutionExpressionComponent { |
| 14 | + private _resolution: Resolution; |
| 15 | + private _steps$ = new BehaviorSubject<String[]>([]); |
| 16 | + private _result$ = new BehaviorSubject<String | null>(null); |
| 17 | + private _error$ = new BehaviorSubject<String | null>(null); |
| 18 | + |
| 19 | + readonly formGroup: FormGroup; |
| 20 | + |
| 21 | + readonly steps$ = this._steps$.asObservable(); |
| 22 | + readonly result$ = this._result$.asObservable(); |
| 23 | + readonly error$ = this._error$.asObservable(); |
| 24 | + |
| 25 | + @Input("resolution") set resolution(r: Resolution) { |
| 26 | + if ((this._resolution = r)) { |
| 27 | + this._steps$.next(Object.keys(r.steps)); |
| 28 | + } else { |
| 29 | + this._steps$.next([]); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + get resolution(): Resolution { |
| 34 | + return this._resolution; |
| 35 | + } |
| 36 | + |
| 37 | + constructor(private _api: ApiService, _builder: FormBuilder) { |
| 38 | + this.formGroup = _builder.group({ |
| 39 | + step: ["", [Validators.required]], |
| 40 | + expression: ["", [Validators.required]], |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + reset(): void { |
| 45 | + this.formGroup.reset(); |
| 46 | + this._result$.next(null); |
| 47 | + this._error$.next(null); |
| 48 | + } |
| 49 | + |
| 50 | + submit(): void { |
| 51 | + const { step, expression } = this.formGroup.value; |
| 52 | + |
| 53 | + this._api.resolution |
| 54 | + .templating(this._resolution, step, expression) |
| 55 | + .subscribe( |
| 56 | + (result) => { |
| 57 | + if (result.error) { |
| 58 | + this._result$.next(null); |
| 59 | + this._error$.next(result.error); |
| 60 | + } else { |
| 61 | + this._result$.next(result.result); |
| 62 | + this._error$.next(null); |
| 63 | + } |
| 64 | + }, |
| 65 | + (e) => { |
| 66 | + this._result$.next(null); |
| 67 | + this._error$.next(e.error.error); |
| 68 | + } |
| 69 | + ); |
| 70 | + } |
| 71 | +} |
0 commit comments