Skip to content

Commit

Permalink
update loading service & api interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
ntqdinh-axonivy committed Dec 27, 2024
1 parent 1f3bcc9 commit 5c19066
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
6 changes: 5 additions & 1 deletion marketplace-ui/src/app/core/interceptors/api.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ export const LoadingComponent = new HttpContextToken<string>(() => '');

export const apiInterceptor: HttpInterceptorFn = (req, next) => {
const router = inject(Router);

const loadingService = inject(LoadingService);

if (req.url.includes('i18n')) {
return next(req);
}

if (req.context.get(LoadingComponent)) {
loadingService.showLoading(req.context.get(LoadingComponent));
}

let requestURL = req.url;
const apiURL = environment.apiUrl;
if (!requestURL.includes(apiURL)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ export class ProductDetailVersionActionComponent implements AfterViewInit {
}

getVersionWithArtifact(ignoreRouteVersion = false) {
this.loadingService.showLoading(LoadingComponentId.PRODUCT_VERSION);
this.sanitizeDataBeforeFetching();
this.productService
.sendRequestToProductDetailVersionAPI(
Expand All @@ -203,7 +202,6 @@ export class ProductDetailVersionActionComponent implements AfterViewInit {
this.getVersionFromRoute(ignoreRouteVersion) ?? this.versions()[0]
);
}
this.loadingService.hideLoading(LoadingComponentId.PRODUCT_VERSION);
});
}

Expand Down
9 changes: 7 additions & 2 deletions marketplace-ui/src/app/modules/product/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export class ProductService {
loadingService = inject(LoadingService);

findProductsByCriteria(criteria: Criteria): Observable<ProductApiResponse> {
this.loadingService.showLoading(LoadingComponentId.LANDING_PAGE);
let requestParams = new HttpParams();
let requestURL = API_URI.PRODUCT;
if (criteria.nextPageHref) {
Expand Down Expand Up @@ -82,7 +81,13 @@ export class ProductService {
const params = new HttpParams()
.append('designerVersion', designerVersion)
.append('isShowDevVersion', showDevVersion);
return this.httpClient.get<VersionData[]>(url, { params });
return this.httpClient.get<VersionData[]>(url, {
params,
context: new HttpContext().set(
LoadingComponent,
LoadingComponentId.PRODUCT_VERSION
)
});
}

sendRequestToUpdateInstallationCount(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { SecurityMonitorService } from './security-monitor.service';
import { ProductSecurityInfo } from '../../shared/models/product-security-info-model';
import { GITHUB_MARKET_ORG_URL, REPO_PAGE_PATHS, SECURITY_MONITOR_MESSAGES, SECURITY_MONITOR_SESSION_KEYS, TIME_UNITS, UNAUTHORIZED } from '../../shared/constants/common.constant';
import { LoadingComponentId } from '../../shared/enums/loading-component-id';
import { LoadingService } from '../../core/services/loading/loading.service';
import { finalize } from 'rxjs';
import { LoadingSpinnerComponent } from '../../shared/components/loading-spinner/loading-spinner.component';

@Component({
Expand All @@ -25,7 +23,6 @@ export class SecurityMonitorComponent {
errorMessage = '';
repos: ProductSecurityInfo[] = [];
protected LoadingComponentId = LoadingComponentId;
loadingService = inject(LoadingService);
private readonly securityMonitorService = inject(SecurityMonitorService);

ngOnInit(): void {
Expand Down Expand Up @@ -62,14 +59,8 @@ export class SecurityMonitorComponent {
}

private fetchSecurityDetails(): void {
this.loadingService.showLoading(LoadingComponentId.SECURITY_MONITOR);
this.securityMonitorService
.getSecurityDetails(this.token)
.pipe(
finalize(() =>
this.loadingService.hideLoading(LoadingComponentId.SECURITY_MONITOR)
)
)
.subscribe({
next: data => this.handleSuccess(data),
error: (err: HttpErrorResponse) => this.handleError(err)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { HttpClient, HttpContext, HttpHeaders } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from '../../../environments/environment';
import { ProductSecurityInfo } from '../../shared/models/product-security-info-model';
import { LoadingComponent } from '../../core/interceptors/api.interceptor';
import { LoadingComponentId } from '../../shared/enums/loading-component-id';

@Injectable({
providedIn: 'root'
})
export class SecurityMonitorService {

private readonly apiUrl = environment.apiUrl + '/api/security-monitor';
private readonly http = inject(HttpClient);

getSecurityDetails(token: string): Observable<ProductSecurityInfo[]> {
const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
return this.http.get<ProductSecurityInfo[]>(this.apiUrl, { headers });
return this.http.get<ProductSecurityInfo[]>(this.apiUrl, {
headers,
context: new HttpContext().set(
LoadingComponent,
LoadingComponentId.SECURITY_MONITOR
)
});
}
}

0 comments on commit 5c19066

Please sign in to comment.