@xsolla/payment-client-core 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import * as i0 from '@angular/core';
2
2
  import { InjectionToken, Injectable, Inject, isDevMode, NgModule } from '@angular/core';
3
- import { firstValueFrom, lastValueFrom, map, Subject, tap, BehaviorSubject, filter as filter$1, takeUntil, skip, throwError } from 'rxjs';
3
+ import { firstValueFrom, lastValueFrom, map, Subject, tap, BehaviorSubject, filter as filter$1, takeUntil, skip, distinctUntilChanged, throwError } from 'rxjs';
4
4
  import * as i1 from '@angular/common/http';
5
5
  import { HttpParams, HttpHeaders, HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
6
6
  import { UntypedFormGroup, Validators, UntypedFormControl, ReactiveFormsModule } from '@angular/forms';
@@ -580,10 +580,17 @@ class ShowFieldStateAction {
580
580
  fieldName: control.fieldName,
581
581
  fieldState: control.fieldState,
582
582
  error: control.error,
583
+ isFocused: control.isFocused,
583
584
  };
584
585
  }
585
586
  }
586
587
 
588
+ var InputEventName;
589
+ (function (InputEventName) {
590
+ InputEventName["blur"] = "blur";
591
+ InputEventName["focus"] = "focus";
592
+ })(InputEventName || (InputEventName = {}));
593
+
587
594
  class InitializeResponse {
588
595
  constructor(response) {
589
596
  this.paymentForm = null;
@@ -3034,6 +3041,17 @@ class PaymentService {
3034
3041
  if (!this.form) {
3035
3042
  throw new Error('form is empty!');
3036
3043
  }
3044
+ if (!this.form.valid) {
3045
+ this.form.markAllAsTouched();
3046
+ Object.keys(this.form.controls).forEach((controlKey) => {
3047
+ const control = this.form?.get(controlKey);
3048
+ if (control) {
3049
+ const controlObject = this.getControlState(controlKey, control);
3050
+ this.nextActionService.emitFlowUpdates(new ShowFieldStateAction({ ...controlObject, isFocused: true }));
3051
+ }
3052
+ });
3053
+ return;
3054
+ }
3037
3055
  const submitResponse = await this.submitService.request(this.fields, this.form);
3038
3056
  this.data = submitResponse;
3039
3057
  this.emitFinanceDetails(submitResponse.summary);
@@ -3078,6 +3096,25 @@ class PaymentService {
3078
3096
  const formControl = this.form?.get(name);
3079
3097
  formControl?.setValue(value);
3080
3098
  }
3099
+ changeFieldState(name, state) {
3100
+ const formControl = this.form?.get(name);
3101
+ if (!formControl) {
3102
+ return;
3103
+ }
3104
+ const controlObject = this.getControlState(name, formControl);
3105
+ switch (state) {
3106
+ case InputEventName.blur: {
3107
+ formControl.markAsTouched();
3108
+ this.nextActionService.emitFlowUpdates(new ShowFieldStateAction({ ...controlObject, isFocused: true }));
3109
+ return;
3110
+ }
3111
+ case InputEventName.focus: {
3112
+ formControl.markAsUntouched();
3113
+ this.nextActionService.emitFlowUpdates(new ShowFieldStateAction({ ...controlObject, isFocused: false }));
3114
+ return;
3115
+ }
3116
+ }
3117
+ }
3081
3118
  destroy() {
3082
3119
  this.data = null;
3083
3120
  this.financeDetails = null;
@@ -3105,20 +3142,23 @@ class PaymentService {
3105
3142
  const control = form.get(key);
3106
3143
  if (control) {
3107
3144
  control.statusChanges
3108
- .pipe(takeUntil(this.formDestroy$))
3145
+ .pipe(distinctUntilChanged(), takeUntil(this.formDestroy$))
3109
3146
  .subscribe((status) => {
3110
- const controlError = control.errors?.['rewritableError'] ??
3111
- null;
3112
- const controlObject = {
3113
- fieldName: key,
3114
- fieldState: status,
3115
- error: controlError,
3116
- };
3147
+ const controlObject = this.getControlState(key, control, status);
3117
3148
  this.nextActionService.emitFlowUpdates(new ShowFieldStateAction(controlObject));
3118
3149
  });
3119
3150
  }
3120
3151
  });
3121
3152
  }
3153
+ getControlState(controlName, controlObject, status) {
3154
+ const controlError = controlObject.errors?.['rewritableError'] ??
3155
+ null;
3156
+ return {
3157
+ fieldName: controlName,
3158
+ fieldState: status ?? controlObject.status,
3159
+ error: controlError,
3160
+ };
3161
+ }
3122
3162
  }
3123
3163
  PaymentService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: PaymentService, deps: [{ token: InitializeService }, { token: SubmitService }, { token: SyncService }, { token: FormService }, { token: StatusService }, { token: FinanceDetailsService }, { token: NextActionService }, { token: ControlConfigService }], target: i0.ɵɵFactoryTarget.Injectable });
3124
3164
  PaymentService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: PaymentService, providedIn: 'root' });
@@ -3277,6 +3317,9 @@ class CoreLibraryService {
3277
3317
  form: {
3278
3318
  getControlConfig: (field) => this.paymentService.getFieldConfig(field),
3279
3319
  changeFieldValue: (name, value) => this.paymentService.changeFieldValue(name, value),
3320
+ changeFieldState: (name, state) => {
3321
+ this.paymentService.changeFieldState(name, state);
3322
+ },
3280
3323
  },
3281
3324
  };
3282
3325
  }