Developing maintenance logic in Angular to control subsequent API requests

In our Angular 9 application, we have various components, some of which have parent-child relationships while others are independent. We begin by making an initial API call that returns a true or false flag value. Depending on this value, we decide whether to proceed with further calls or halt the execution.

homecomponent.html :

<div>
//header is child component
<app-header>

       </app-header>
.......
......
</div>


homecomponent.ts:

export class HomeComponent implements OnInit {

ngOnInit(): void {
this.getPageConent();
}

getPageConent() {
// below service will make the http call

    this.dataService.GetPovertyPageStaticContent('home').subscribe((result: any) => {
// based upon the flag execute further or stop execution
 });
  }
}


headercomponent.ts:

export class HeaderComponent implements OnInit {

ngOnInit(): void {
 this.getContents();
}
 getContents() {
  // Another API call to get page data
    this.dataService.GetPovertyPageStaticContent('pageheader').subscribe((result: any) => {

     //do some operation
    });
  }
}

We have multiple components interconnected in a similar manner. Our goal is to restrict additional API calls within the application based on the outcome of the initial API call.

Answer №1

Do you think that will suffice?

this.dataService
  .FetchPovertyPageInfo('home')
  .pipe(filter(value => !!value))
  .subscribe(...)

If the outcome is negative, it will halt the subscription process.

Answer №2

Based on the information provided in my previous response, it appears that what you need is an interceptor.

More detailed documentation can be found here

To achieve this, you will need to establish a service responsible for maintaining your condition consistently (as a singleton). The interceptor will then handle requests to the API when the condition is fulfilled. If the condition is not met, the interceptor can return an EMPTY observable (which completes without emitting any values).

Although I recommend implementing this logic on your API rather than the client app, using an interceptor seems like the most suitable approach based on your requirements.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues with the visibility of inlined styles within the Angular component library

After developing a custom library with components for an Angular 4 application, I encountered an issue where the CSS styling from the components was not being applied when using them in an application. Although the functionality worked fine, the visual asp ...

The slideshow fails to show correctly after being loaded from an external JavaScript file

Utilizing standard code, I have set up a Slideshow at the top of a website: HTML: <body id="Top" onload="showSlides()"> ... <div id="slides"> <div id="slide1" class="slides fade"></div> <div id="s ...

What is the best way to make mouse and touch events trigger responses in Angular versions 9 and above?

I've been searching high and low for a library or tried-and-true method to handle common user events reactively, but unfortunately my quest has come up empty. After some digging, I stumbled upon what seemed like a solid solution: https://github.com/t ...

Disable the ability to click on the indicators in the Bootstrap carousel

Seeking assistance with customizing tweeters bootstrap Carousel functionality. I have removed the controls and now want to switch slides according to my own logic while displaying slide indicators. How can this be achieved? Preferably seeking a solution ...

"Exploring the differences between normalization structures and observable entities in ngrx

I'm currently grappling with the concept of "entity arrays" in my ngrx Store. Let's say I have a collection of PlanDTO retrieved from my api server. Based on the research I've done, it seems necessary to set up a kind of "table" to store th ...

"Learn the technique of adding a new data attribute before every element in a step-by-step

I am currently working with the following HTML code: <div id="elem"> <div data-foo="aaa"></div> <div data-foo="aaa"></div> <div data-foo="aaa"></div> <div data-foo="bbb"></div> < ...

Verification of the data entered in the input box

I am looking to develop an Input Box where users can enter the private details of a person. The first character must be either A or E, and the rest can be alphanumeric with no special characters allowed. I need to implement validation on the client side to ...

How to retrieve the image source from a block using jQuery

Currently, I am working on developing a slider for my webpage using jquery's "Cycle" plugin. However, I have encountered an issue with accessing the image sources used in the slider. To provide more context, here is a snippet of code from the 'he ...

Ordering an array using Typescript within React's useEffect()

Currently, I am facing a TypeScript challenge with sorting an array of movie objects set in useEffect so that they are displayed alphabetically. While my ultimate goal is to implement various sorting functionalities based on different properties in the fut ...

An AJAX request will only occur if there is an alert triggered on a particular computer

Here's an interesting issue I encountered with my company's CMS newsletter system. It seems that the AJAX call to send an email works flawlessly in all modern browsers and operating systems, except for one client. This particular client is using ...

The element type is not valid: it should be a string for built-in components or a class/function for composite components, but it is currently an object in a React project

In the process of developing a React app to explore MUI capabilities, I encountered an error in my browser: The issue reported is: Element type is invalid - expected a string (for built-in components) or a class/function (for composite components), but rec ...

Hosted-Git-Info Regular Expression Denial of Service attack

I am facing a vulnerability in my Ionic/Angular app. The issue suggests updating the version of hosted-git-info. However, I cannot find this package in my package.json file. Can you provide guidance on how to resolve this? https://www.npmjs.com/advisorie ...

Cleaning arrays in Angular 2 forms: A step-by-step guide

Need some assistance. I'm developing an app in Angular2(4) which includes a Form with 2 select boxes and 1 checkbox. My goal is to establish a dependency between these boxes using the following array: dataJSON = [ { productName: 'Produ ...

Modifying the maximum value of a number field attribute in jQuery after a successful action

As I continue to learn jQuery, I encountered a situation with the following form: <form class="simple-checkout" enctype="multipart/form-data" method="POST" action="<?php echo admin_url('admin-ajax.php'); ?>"> <input type="hidd ...

Experiencing Issues with File Downloading on Express Server with Axios and Js-File-Download Library

I developed a feature on my express server that allows users to download a file easily. app.post("/download", (req, res) => { let file_name = req.body.name; res.download(path.join(__dirname, `files/${file_name}.mp3`), (err) => { ...

What is the best way to link this to a function in AngularIO's Observable::subscribe method?

Many examples use the Observable.subscribe() function in AngularIO. However, I have only seen anonymous functions being used like this: bar().subscribe(data => this.data = data, ...); When I try to use a function from the same class like this: update ...

The Angular route functions flawlessly in the development environment, but encounters issues when deployed to

I have a project built with Angular 2, During development on localhost, everything runs smoothly. However, once I build a production version using (npm run build: prod) and navigate to the route on the server, I encounter a 404 error indicating that the r ...

Generating various API calls and delivering them to a template (Express + Node.js + Facebook open graph)

I am currently developing a unique Express Node.js application that utilizes the extraordinary capabilities of this remarkable Facebook SDK. Allow me to present my existing route for the root: app.get('/', Facebook.loginRequired(), function (req ...

What is the best way to change an http call in a controller to a Service/factory pattern that accepts a parameter?

Currently, I have a controller making use of http.get, http.push and http.post methods within my AngularJS app. During my learning journey with AngularJS, I've discovered that it's more efficient to place http.get calls in service files. While I ...

Tips for creating a plug-in plugin and applying the necessary parameters

(function( $ ){ var functions = { init : function( options ) { var settings = $.extend({ //Declaring default settings that can be overridden in the plugin call code: 7, listHe ...