@xsolla/payment-client-core 0.1.0 → 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.
Files changed (31) hide show
  1. package/esm2020/lib/core/payment/actions/next-action.interface.mjs +1 -1
  2. package/esm2020/lib/core/payment/actions/next-actions.mjs +3 -1
  3. package/esm2020/lib/core/payment/actions/show-errors/show-errors.action.class.mjs +1 -1
  4. package/esm2020/lib/core/payment/actions/show-errors/show-errors.action.type.mjs +1 -1
  5. package/esm2020/lib/core/payment/actions/show-field-state/show-field-state.action.class.mjs +12 -0
  6. package/esm2020/lib/core/payment/actions/show-field-state/show-field-state.action.token.mjs +13 -0
  7. package/esm2020/lib/core/payment/actions/show-field-state/show-field-state.action.type.mjs +2 -0
  8. package/esm2020/lib/core/payment/form/input-events.enum.mjs +6 -0
  9. package/esm2020/lib/core/payment/payment.interface.mjs +1 -1
  10. package/esm2020/lib/core/payment/payment.service.mjs +55 -24
  11. package/esm2020/lib/core/payment/show-field-state.interface.mjs +2 -0
  12. package/esm2020/lib/core-library.service.mjs +4 -1
  13. package/fesm2015/xsolla-payment-client-core.mjs +91 -28
  14. package/fesm2015/xsolla-payment-client-core.mjs.map +1 -1
  15. package/fesm2020/xsolla-payment-client-core.mjs +89 -26
  16. package/fesm2020/xsolla-payment-client-core.mjs.map +1 -1
  17. package/lib/core/payment/actions/next-action.interface.d.ts +3 -2
  18. package/lib/core/payment/actions/show-errors/show-errors.action.class.d.ts +2 -2
  19. package/lib/core/payment/actions/show-errors/show-errors.action.type.d.ts +2 -2
  20. package/lib/core/payment/actions/show-field-state/show-field-state.action.class.d.ts +8 -0
  21. package/lib/core/payment/actions/show-field-state/show-field-state.action.token.d.ts +4 -0
  22. package/lib/core/payment/actions/show-field-state/show-field-state.action.type.d.ts +11 -0
  23. package/lib/core/payment/form/input-events.enum.d.ts +4 -0
  24. package/lib/core/payment/payment.interface.d.ts +3 -1
  25. package/lib/core/payment/payment.service.d.ts +5 -1
  26. package/lib/core/payment/show-field-state.interface.d.ts +8 -0
  27. package/package.json +1 -1
  28. package/xsolla-payment-client-core-0.1.2.tgz +0 -0
  29. package/esm2020/lib/core/payment/extended-xps-error.mjs +0 -2
  30. package/lib/core/payment/extended-xps-error.d.ts +0 -4
  31. package/xsolla-payment-client-core-0.1.0.tgz +0 -0
@@ -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';
@@ -573,15 +573,24 @@ const isSubmitResponse = (value) => {
573
573
  return value instanceof SubmitResponse;
574
574
  };
575
575
 
576
- class ShowErrorsAction {
577
- constructor(errors) {
578
- this.type = 'show_errors';
576
+ class ShowFieldStateAction {
577
+ constructor(control) {
578
+ this.type = 'show_field_state';
579
579
  this.data = {
580
- errors,
580
+ fieldName: control.fieldName,
581
+ fieldState: control.fieldState,
582
+ error: control.error,
583
+ isFocused: control.isFocused,
581
584
  };
582
585
  }
583
586
  }
584
587
 
588
+ var InputEventName;
589
+ (function (InputEventName) {
590
+ InputEventName["blur"] = "blur";
591
+ InputEventName["focus"] = "focus";
592
+ })(InputEventName || (InputEventName = {}));
593
+
585
594
  class InitializeResponse {
586
595
  constructor(response) {
587
596
  this.paymentForm = null;
@@ -631,6 +640,15 @@ const initializeResponseFactoryProvider = {
631
640
  useValue: (...args) => new InitializeResponse(...args),
632
641
  };
633
642
 
643
+ class ShowErrorsAction {
644
+ constructor(errors) {
645
+ this.type = 'show_errors';
646
+ this.data = {
647
+ errors,
648
+ };
649
+ }
650
+ }
651
+
634
652
  class ShowFieldsAction {
635
653
  constructor(submitResponse) {
636
654
  this.type = 'show_fields';
@@ -751,6 +769,17 @@ const showErrorsActionFactoryProvider = {
751
769
  },
752
770
  };
753
771
 
772
+ const showFieldStateActionFactoryToken = new InjectionToken('ShowFieldStateAction');
773
+ const showFieldStateActionFactoryProvider = {
774
+ provide: showFieldStateActionFactoryToken,
775
+ useValue: {
776
+ factory: (...args) => new ShowFieldStateAction(...args),
777
+ isSuitable: () => {
778
+ return true;
779
+ },
780
+ },
781
+ };
782
+
754
783
  const nextActionsToken = new InjectionToken('Action');
755
784
  const nextActions = [
756
785
  showFieldsActionFactoryProvider,
@@ -760,6 +789,7 @@ const nextActions = [
760
789
  const flowUpdateNextActions = [
761
790
  statusUpdatedActionFactoryProvider,
762
791
  showErrorsActionFactoryProvider,
792
+ showFieldStateActionFactoryProvider,
763
793
  ];
764
794
 
765
795
  class NextActionService {
@@ -2992,9 +3022,11 @@ class PaymentService {
2992
3022
  this.financeDetails = null;
2993
3023
  this.fields = [];
2994
3024
  this.destroy$ = new Subject();
3025
+ this.formDestroy$ = new Subject();
2995
3026
  }
2996
3027
  async initialize(config) {
2997
3028
  this.destroy$ = new Subject();
3029
+ this.formDestroy$ = new Subject();
2998
3030
  this.listenFinanceDetails();
2999
3031
  this.config = config;
3000
3032
  this.data = await this.initializeService.request(config);
@@ -3009,6 +3041,17 @@ class PaymentService {
3009
3041
  if (!this.form) {
3010
3042
  throw new Error('form is empty!');
3011
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
+ }
3012
3055
  const submitResponse = await this.submitService.request(this.fields, this.form);
3013
3056
  this.data = submitResponse;
3014
3057
  this.emitFinanceDetails(submitResponse.summary);
@@ -3053,11 +3096,31 @@ class PaymentService {
3053
3096
  const formControl = this.form?.get(name);
3054
3097
  formControl?.setValue(value);
3055
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
+ }
3056
3118
  destroy() {
3057
3119
  this.data = null;
3058
3120
  this.financeDetails = null;
3059
3121
  this.form = null;
3060
3122
  this.fields = [];
3123
+ this.formDestroy$.next();
3061
3124
  this.destroy$.next();
3062
3125
  }
3063
3126
  emitFinanceDetails(summary) {
@@ -3075,30 +3138,27 @@ class PaymentService {
3075
3138
  .subscribe((financeDetails) => (this.financeDetails = financeDetails));
3076
3139
  }
3077
3140
  listenSyncValidationErrors(form) {
3078
- form.statusChanges.subscribe(() => {
3079
- const controls = this.form?.controls;
3080
- if (controls) {
3081
- const invalidControls = Object.entries(controls).filter(([key]) => controls[key].invalid);
3082
- if (invalidControls.length) {
3083
- const syncValidationErrors = invalidControls
3084
- .map(([controlName, control]) => {
3085
- const error = control.errors?.['rewritableError'];
3086
- if (!error) {
3087
- return null;
3088
- }
3089
- return {
3090
- code: 101,
3091
- fieldValidationError: error?.code,
3092
- message: error?.message ?? '',
3093
- element_name: controlName,
3094
- };
3095
- })
3096
- .filter(Boolean);
3097
- this.nextActionService.emitFlowUpdates(new ShowErrorsAction(syncValidationErrors));
3098
- }
3141
+ Object.keys(form.controls).forEach((key) => {
3142
+ const control = form.get(key);
3143
+ if (control) {
3144
+ control.statusChanges
3145
+ .pipe(distinctUntilChanged(), takeUntil(this.formDestroy$))
3146
+ .subscribe((status) => {
3147
+ const controlObject = this.getControlState(key, control, status);
3148
+ this.nextActionService.emitFlowUpdates(new ShowFieldStateAction(controlObject));
3149
+ });
3099
3150
  }
3100
3151
  });
3101
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
+ }
3102
3162
  }
3103
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 });
3104
3164
  PaymentService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: PaymentService, providedIn: 'root' });
@@ -3257,6 +3317,9 @@ class CoreLibraryService {
3257
3317
  form: {
3258
3318
  getControlConfig: (field) => this.paymentService.getFieldConfig(field),
3259
3319
  changeFieldValue: (name, value) => this.paymentService.changeFieldValue(name, value),
3320
+ changeFieldState: (name, state) => {
3321
+ this.paymentService.changeFieldState(name, state);
3322
+ },
3260
3323
  },
3261
3324
  };
3262
3325
  }