import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import {finalize, Observable} from 'rxjs';

import {LoadingService} from "@app/_services/loading.service";

@Injectable()
export class LoadingInterceptor implements HttpInterceptor {
    constructor(
        private loadingService: LoadingService
    ) { }

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        this.loadingService.setLoading(true);
        return next.handle(request).pipe(
            finalize(() => {
                this.loadingService.setLoading(false);
            })
        );
    }
}