Skip to content

Commit

Permalink
set loader optional
Browse files Browse the repository at this point in the history
  • Loading branch information
vojvodicn23 committed Sep 15, 2024
1 parent 90b69f8 commit 240005e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ export class AppComponent implements OnInit {
ngOnInit(): void {
this.http.get<any[]>(this.apiUrl).pipe(
handle(
(loading) => {
this.loading = loading;
console.log(loading);
},
(response) => {
this.response = response;
console.log(response);
},
(loading) => { // OPTIONAL - loading indicator
this.loading = loading;
console.log(loading);
},
(error) => { // OPTIONAL - custom error handler
console.log(error);
},
Expand Down Expand Up @@ -80,12 +80,12 @@ The handle function manages HTTP requests with loading state, error handling and

Parameters:

- loadingSetter: (loading: boolean) => void
Function to set the loading state (e.g., this.loading = loading).

- dataSetter: (response: T) => void
Function to set the data when the request succeeds (e.g., this.data = response).

- loadingSetter?: (loading: boolean) => void
Function to set the loading state (e.g., this.loading = loading).

- errorHandler?: (error: HttpErrorResponse) => void
Optional function to handle errors (e.g., console.error(error)).

Expand Down
14 changes: 7 additions & 7 deletions projects/angular-http-handler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ export class AppComponent implements OnInit {
ngOnInit(): void {
this.http.get<any[]>(this.apiUrl).pipe(
handle(
(loading) => {
this.loading = loading;
console.log(loading);
},
(response) => {
this.response = response;
console.log(response);
},
(loading) => { // OPTIONAL - loading indicator
this.loading = loading;
console.log(loading);
},
(error) => { // OPTIONAL - custom error handler
console.log(error);
},
Expand Down Expand Up @@ -80,12 +80,12 @@ The handle function manages HTTP requests with loading state, error handling and

Parameters:

- loadingSetter: (loading: boolean) => void
Function to set the loading state (e.g., this.loading = loading).

- dataSetter: (response: T) => void
Function to set the data when the request succeeds (e.g., this.data = response).

- loadingSetter?: (loading: boolean) => void
Function to set the loading state (e.g., this.loading = loading).

- errorHandler?: (error: HttpErrorResponse) => void
Optional function to handle errors (e.g., console.error(error)).

Expand Down
2 changes: 1 addition & 1 deletion projects/angular-http-handler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-http-handler",
"version": "0.0.1",
"version": "0.0.2",
"description": "This is the http handler utility for Angular http requests",
"keywords": ["http", "angular", "loading", "error-handler"],
"author": {
Expand Down
14 changes: 11 additions & 3 deletions projects/angular-http-handler/src/lib/http-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import { HttpErrorResponse } from "@angular/common/http";
import { catchError, finalize, Observable, of, retry, tap } from "rxjs";

export function handle<T>(
loadingSetter: (loading: boolean) => void,
dataSetter: (response: T) => void,
loadingSetter?: (loading: boolean) => void,
errorHandler?: (error: HttpErrorResponse) => void,
retryCount: number = 0,
retryDelay?: number,
): (source$: Observable<T>) => Observable<T> {
return (source$: Observable<T>) =>
source$.pipe(
tap(() => loadingSetter(true)),
tap(() => {
if(loadingSetter){
loadingSetter(true);
}
}),
retry({
count: retryCount,
delay: retryDelay,
Expand All @@ -33,6 +37,10 @@ export function handle<T>(
dataSetter(fallback);
return of(fallback);
}),
finalize(() => loadingSetter(false))
finalize(() => {
if(loadingSetter){
loadingSetter(false);
}
})
);
}
8 changes: 4 additions & 4 deletions projects/http-handler-example/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ export class AppComponent implements OnInit {
ngOnInit(): void {
this.http.get<any[]>(this.apiUrl).pipe(
handle(
(loading) => {
this.loading = loading;
console.log(loading)
},
(response) => {
this.response = response;
console.log(response)
},
(loading) => { // OPTIONAL - loader indicator
this.loading = loading;
console.log(loading)
},
(e) => console.log(e, 'custom'), // OPTIONAL - custom error handler
2, // OPTIONAL - retry count
1000, // OTIONAL - retry delay
Expand Down

0 comments on commit 240005e

Please sign in to comment.