feature(core): add loader actions & dispatch from interceptor

pull/1562/head
mehmet-erim 6 years ago
parent de58b9882c
commit a2d00356ac

@ -1,4 +1,5 @@
export * from './config.actions';
export * from './loader.actions';
export * from './profile.actions';
export * from './rest.actions';
export * from './session.actions';

@ -0,0 +1,11 @@
import { HttpRequest } from '@angular/common/http';
export class LoaderStart {
static readonly type = '[Loader] Start';
constructor(public payload: HttpRequest<any>) {}
}
export class LoaderStop {
static readonly type = '[Loader] Stop';
constructor(public payload: HttpRequest<any>) {}
}

@ -3,13 +3,18 @@ import { HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'
import { OAuthService } from 'angular-oauth2-oidc';
import { Store } from '@ngxs/store';
import { SessionState } from '../states';
import { LoaderStart, LoaderStop } from '../actions/loader.actions';
import { finalize } from 'rxjs/operators';
@Injectable()
export class ApiInterceptor implements HttpInterceptor {
constructor(private oAuthService: OAuthService, private store: Store) {}
intercept(request: HttpRequest<any>, next: HttpHandler) {
this.store.dispatch(new LoaderStart(request));
const headers = {} as any;
const token = this.oAuthService.getAccessToken();
if (!request.headers.has('Authorization') && token) {
headers['Authorization'] = `Bearer ${token}`;
@ -20,10 +25,12 @@ export class ApiInterceptor implements HttpInterceptor {
headers['Accept-Language'] = lang;
}
return next.handle(
request.clone({
setHeaders: headers,
}),
);
return next
.handle(
request.clone({
setHeaders: headers,
}),
)
.pipe(finalize(() => this.store.dispatch(new LoaderStop(request))));
}
}

Loading…
Cancel
Save