Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

where to put toaster on http service calls typescript

 testApi(viewData: any): Observable<any> {

    return this.http
      .delete<any[]>(URL, viewData)
      .pipe(
        map((res: any) => {
		  // add snackbar or toast function here;
          return res;
        })
      )
      .pipe(
        catchError((error: any) => {
          // add snackbar or toast function here;
          return throwError(error);
        })
      );
  }
Comment

where to put toaster on http service calls typescript

@Injectable()
export class HttpInterceptor implements HttpInterceptor {
constructor(public toasterService: ToastrService) {}

intercept(
    req: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {

    return next.handle(req).pipe(
        tap(evt => {
            if (evt instanceof HttpResponse) {
                if(evt.body && evt.body.success)
                    this.toasterService.success(evt.body.success.message, evt.body.success.title, { positionClass: 'toast-bottom-center' });
            }
        }),
        catchError((err: any) => {
            if(err instanceof HttpErrorResponse) {
                try {
                    this.toasterService.error(err.error.message, err.error.title, { positionClass: 'toast-bottom-center' });
                } catch(e) {
                    this.toasterService.error('An error occurred', '', { positionClass: 'toast-bottom-center' });
                }
                //log error 
            }
            return of(err);
        }));
  }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: you can initiate objects from a 
Typescript :: How to separate two similar names from two lists in Python 
Typescript :: tiqets endpoints 
Typescript :: unity destroy all objects with tag 
Typescript :: how to setup netflix workflow worker 
Typescript :: windows 10 iso 
Cpp :: fast io 
Cpp :: regex match all between parentheses 
Cpp :: print set c++ 
Cpp :: excel vba delete worksheet if exists 
Cpp :: cpp print vector 
Cpp :: shuffle vector c++ 
Cpp :: c++ file is empty 
Cpp :: how to print a decimal number upto 6 places of decimal in c++ 
Cpp :: rng c++ 
Cpp :: how to cehck if list has element c++ 
Cpp :: power function in O(log(n)) time c++ 
Cpp :: c++ set console title 
Cpp :: uri online judge 1930 solution in c++ 
Cpp :: c++ get input without loop 
Cpp :: how to interface c++ in haxe 
Cpp :: for loop reverse C++ 
Cpp :: c++ nodiscard 
Cpp :: c++ srand() 
Cpp :: xmake set binary name 
Cpp :: c++ std::copy to cout 
Cpp :: how to free the vector c++ 
Cpp :: remove at index vector c++ 
Cpp :: create a dictionary cpp 
Cpp :: cpp mst 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =