Prevent users from pressing the "next" button until all input fields have been updated

With over 30 inputs on a single page, each input triggers a callback on ng-blur (when focus is lost) that calls a remote API using the $http AngularJS service. These API requests run asynchronously in parallel, allowing users to edit inputs multiple times with each ng-blur event sending data to the server.

At the bottom of the page, there is a "next" button that users press when they are finished editing the inputs. How can we prevent users from pressing the "next" button until all API PUT promises have been completed? Additionally, what should be done if some HTTP requests fail due to a server error (e.g., 500 status code)?

Answer №1

To enable a button based on a flag in the component:

In your view, add the following code:

<button type="submit" [disabled]="!finished">Add</button>

Within your component, set the initial value of the flag:

public finished = false;

Within a Promise:

   let promise1 = Promise.resolve(3);
   let promise2 = 42;
   let promise3 = new Promise(function(resolve, reject) {
      setTimeout(resolve, 100, 'foo');
    });

Promise.all([promise1,promise2,...]).then((...)=> {
  finished = true; // enabling the button
})

For more information, check here

Alternatively, you can use separate flags for each Promise:

public promiseF1 = false;
public promiseF2 = false;
public promiseF3 = false;
etc...

Set each flag to false at the beginning and update it to true upon completion of the respective call.

Then, in your HTML:

<button type="submit"[disabled]="!promiseF1 && !promiseF2 && !promiseF3 && etc...">
 Add
</button>

Answer №2

Ensure that all input fields have the "required" parameter filled in to guarantee that responses from multiple promises are complete before activating the submit button.

Validate the form upon clicking the submit button.

<form  #form="ngForm">
    //Include required parameters in your input fields

  <button type="submit" [disabled]="form.invalid">Next</button>

</form>

Answer №3

To address this issue, I devised a solution inspired by semaphores. Starting with the semaphore counter at zero, I increment it whenever a promise is initialized and decrement it upon resolution or rejection (using .finally()). When the counter reaches zero, the "next" button becomes accessible to the user.

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

Array.from does not create a deep copy

I'm in the process of creating my own Battleship game, and I've been researching for about 10 hours but haven't been able to crack it yet. The issue I'm facing is related to making a deep copy using Array.from; whenever I modify the pl ...

Guidelines for utilizing basepath for specific pages in NextJS

I'm currently working on a NextJS application using version 13 and I have a question regarding setting the basepath for specific pages only. For example: "/auth/*" --> should not display the basepath "/remaining-pages" --& ...

Tips on viewing class object values within the `useEffect` hook

App.js import React, { useRef, useEffect } from "react"; import Token from "./Token"; export default function App() { const tokenRef = useRef(new Token()); useEffect(() => { console.log("current index of token: ", ...

Steps for adding a thought bubble over an image using CSS

Currently, I am working on a project where users can upload an image and based on that image, a thought bubble or speech bubble is placed on top. I need to make sure the placement is just right, but my main priority is getting a functional version up and ...

Creating multiple AJAX contact forms can be achieved by modifying the code to accommodate multiple forms on a single page. Here's

My one-page website features 15 identical contact forms all with the same ID, created using basic PHP. Unfortunately, I am facing an issue where AJAX is only working for the first form. When submitting any other form, it simply opens a white page with a "T ...

Firing a custom jQuery event when a page loaded via AJAX is complete, ensuring it is triggered

I am facing an issue with a particular scenario. There is a page that contains a script to load a child page and log a custom event, which is triggered in a Subform. --Index.html-- <body> <input type="button" class="clickable" value="Load child ...

Exploring the method of retrieving nested JSON objects in Angular

When my API sends back a JSON response, my Angular application is able to capture it using an Interface. The structure of the JSON response appears as follows: { "release_date":"2012-03-14", "genre_relation": ...

Is there a way for CasperJS to access the underlying PhantomJS objects and references?

Currently, I am in the process of transitioning a script from PhantomJS to CasperJS, and I am curious if Casper offers any references to the Phantom objects it utilizes internally. It is important to note that Phantom provides certain functionalities that ...

Updating elements in an array based on a specified threshold value, all done without the use of conditional if statements in Javascript

I am exploring ways to efficiently solve this particular problem. Within my dataset is an extensive array of integers: [170,158,147,139,134,132,133,136,141,.....] I have predetermined threshold values of 132 and 137. My goal is to adjust any numbers in ...

Collapse the Bootstrap Menu upon selecting a Link

I am working on a side menu with an offcanvas display feature. The current functionality is such that when the button is clicked, the canvas pushes left and opens the menu. However, I want the menu to collapse and close automatically when a link is selecte ...

issue involving extension that interrupts downloads

Trying to develop a browser extension that can intercept downloads and automatically rename them. manifest.json: { "name": " Book Renamer", "description": "Automatically rename downloaded ebooks from gutenberg.or ...

What is the best way to remove an item from a mongoose schema and then update the document or schema?

I'm encountering an issue with deleting items from an array that is nested inside the User schema. When attempting to save the document, I receive the following error message? Here is the error: TypeError: User.save is not a function at C:\U ...

Passing along a request using Node.js

I am facing an issue in my project where I need to redirect requests received by a nodejs endpoint to a .NET 7 web API endpoint. The nodejs endpoint is triggered by an external party and it receives the request as expected. However, there seems to be a pro ...

The start screen fails to display when clicking the restart button

I am struggling to get the restart button to display the start screen again. Even though I have called the restart function within the clearColors function, which should show the start screen, it hasn't been effective so far. Initially, in the fadeOut ...

How can you protect against XSS when using React server-side rendering and window.initialState?

When passing my initial state to React, I do it like this: window.__INITIAL_STATE__= ${JSON.stringify(initialState)} I have not implemented any protection against XSS. For example, if user-submitted content contains the following string: "<script>* ...

Retrieving the text of a selected option by its value

I need to create a menu that returns text based on a given value. For example, if the value is 0, I want it to return “zero”, and if it's 1, then “one”, and so on. I don't need to know which option is selected or auto-select anything, I j ...

The context of the Nuxt.js3 plugin loses its definition

Currently, I am working on a project that involves Nuxt.js3 and supabase integration. In my plugins/supabase.server.js file (I am still unsure whether using server or client is the best approach), I want to call "supabase = createClient(~~)" from index.vu ...

Packing third-party npm modules with Webpack for seamless integration

Description I am currently working on a project that involves nodejs, TypeScript, and express. The source files with a *.ts extension are being bundled using webpack, while the node_modules folder is excluded using webpack-node-externals. However, when I ...

The React functional component fails to update when triggered by a parent component's setState method

My React component is utilizing apollo to fetch data through graphql class PopUpForm extends React.Component { constructor () { super() this.state = { shoptitle: "UpdateMe", popupbodyDesc: "UpdateMe" } } re ...

Utilizing Django's built-in widgets for localization

When utilizing Django's built-in widgets such as AdminSplitDateTime, the issue arises where the strings used in this widget are not localized like they are in JavaScript. What is the solution to address this localization discrepancy? ...