Browse Source

refactor(core): rename throwErr parameter in rest.service

add throwError after the handle error
add autofocus to confirmation
pull/1672/head
mehmet-erim 7 years ago
parent
commit
2b9e6002c6
  1. 2
      npm/ng-packs/packages/account/src/lib/services/account.service.ts
  2. 47
      npm/ng-packs/packages/core/src/lib/directives/form-submit.directive.ts
  3. 2
      npm/ng-packs/packages/core/src/lib/models/rest.ts
  4. 4
      npm/ng-packs/packages/core/src/lib/services/profile.service.ts
  5. 6
      npm/ng-packs/packages/core/src/lib/services/rest.service.ts
  6. 8
      npm/ng-packs/packages/theme-shared/src/lib/components/confirmation/confirmation.component.ts

2
npm/ng-packs/packages/account/src/lib/services/account.service.ts

@ -25,6 +25,6 @@ export class AccountService {
body,
};
return this.rest.request<RegisterRequest, RegisterResponse>(request, { throwErr: true });
return this.rest.request<RegisterRequest, RegisterResponse>(request, { skipHandleError: true });
}
}

47
npm/ng-packs/packages/core/src/lib/directives/form-submit.directive.ts

@ -1,19 +1,18 @@
import {
ChangeDetectorRef,
Directive,
ElementRef,
EventEmitter,
Input,
OnDestroy,
OnInit,
Self,
ChangeDetectorRef,
HostBinding,
Input,
Output,
EventEmitter,
Self,
} from '@angular/core';
import { FormGroupDirective, FormGroup, FormControl, ɵNgNoValidate } from '@angular/forms';
import { FormControl, FormGroup, FormGroupDirective } from '@angular/forms';
import { fromEvent } from 'rxjs';
import { takeUntilDestroy } from '../utils';
import { debounceTime, filter } from 'rxjs/operators';
import { takeUntilDestroy } from '../utils';
type Controls = { [key: string]: FormControl } | FormGroup[];
@ -24,6 +23,11 @@ export class FormSubmitDirective implements OnInit, OnDestroy {
@Input()
notValidateOnSubmit: string | boolean;
@Output()
ngSubmit = new EventEmitter();
executedNgSubmit: boolean = false;
constructor(
@Self() private formGroupDirective: FormGroupDirective,
private host: ElementRef<HTMLFormElement>,
@ -31,6 +35,11 @@ export class FormSubmitDirective implements OnInit, OnDestroy {
) {}
ngOnInit() {
this.formGroupDirective.ngSubmit.pipe(takeUntilDestroy(this)).subscribe(() => {
this.markAsDirty();
this.executedNgSubmit = true;
});
fromEvent(this.host.nativeElement as HTMLElement, 'keyup')
.pipe(
debounceTime(200),
@ -38,7 +47,11 @@ export class FormSubmitDirective implements OnInit, OnDestroy {
takeUntilDestroy(this),
)
.subscribe(() => {
this.host.nativeElement.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true }));
if (!this.executedNgSubmit) {
this.host.nativeElement.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true }));
}
this.executedNgSubmit = false;
});
fromEvent(this.host.nativeElement, 'submit')
@ -47,16 +60,22 @@ export class FormSubmitDirective implements OnInit, OnDestroy {
filter(() => !this.notValidateOnSubmit && typeof this.notValidateOnSubmit !== 'string'),
)
.subscribe(() => {
const { form } = this.formGroupDirective;
setDirty(form.controls as { [key: string]: FormControl });
form.markAsDirty();
this.cdRef.detectChanges();
if (!this.executedNgSubmit) {
this.markAsDirty();
}
});
}
ngOnDestroy(): void {}
markAsDirty() {
const { form } = this.formGroupDirective;
setDirty(form.controls as { [key: string]: FormControl });
form.markAsDirty();
this.cdRef.detectChanges();
}
}
function setDirty(controls: Controls) {

2
npm/ng-packs/packages/core/src/lib/models/rest.ts

@ -2,7 +2,7 @@ import { HttpHeaders, HttpParams } from '@angular/common/http';
export namespace Rest {
export interface Config {
throwErr?: boolean;
skipHandleError?: boolean;
observe?: Observe;
}

4
npm/ng-packs/packages/core/src/lib/services/profile.service.ts

@ -28,13 +28,13 @@ export class ProfileService {
return this.rest.request<Profile.Response, Profile.Response>(request);
}
changePassword(body: Profile.ChangePasswordRequest, throwErr: boolean = false): Observable<null> {
changePassword(body: Profile.ChangePasswordRequest, skipHandleError: boolean = false): Observable<null> {
const request: Rest.Request<Profile.ChangePasswordRequest> = {
method: 'POST',
url: '/api/identity/my-profile/change-password',
body,
};
return this.rest.request<Profile.ChangePasswordRequest, null>(request, { throwErr });
return this.rest.request<Profile.ChangePasswordRequest, null>(request, { skipHandleError });
}
}

6
npm/ng-packs/packages/core/src/lib/services/rest.service.ts

@ -16,17 +16,17 @@ export class RestService {
handleError(err: any): Observable<any> {
this.store.dispatch(new RestOccurError(err));
console.error(err);
return NEVER;
return throwError(err);
}
request<T, R>(request: HttpRequest<T> | Rest.Request<T>, config: Rest.Config = {}, api?: string): Observable<R> {
const { observe = Rest.Observe.Body, throwErr } = config;
const { observe = Rest.Observe.Body, skipHandleError } = config;
const url = api || this.store.selectSnapshot(ConfigState.getApiUrl()) + request.url;
const { method, ...options } = request;
return this.http.request<T>(method, url, { observe, ...options } as any).pipe(
observe === Rest.Observe.Body ? take(1) : null,
catchError(err => {
if (throwErr) {
if (skipHandleError) {
return throwError(err);
}

8
npm/ng-packs/packages/theme-shared/src/lib/components/confirmation/confirmation.component.ts

@ -27,7 +27,13 @@ import { Toaster } from '../../models/toaster';
<button *ngIf="!message.hideCancelBtn" type="button" class="btn btn-secondary" (click)="close(reject)">
{{ message.cancelCopy || 'AbpIdentity::Cancel' | abpLocalization }}
</button>
<button *ngIf="!message.hideYesBtn" type="button" class="btn btn-secondary" (click)="close(confirm)">
<button
*ngIf="!message.hideYesBtn"
type="button"
class="btn btn-secondary"
(click)="close(confirm)"
autofocus
>
<span>{{ message.yesCopy || 'AbpIdentity::Yes' | abpLocalization }}</span>
</button>
</div>

Loading…
Cancel
Save