Sending Angular forms to various endpoints

On a page, I have a form consisting of three different groups

this.form = this.formBuilder.group({
  profile: this.formBuilder.group({
    name: [''],
    description: [''],   
  }),

  members: this.formBuilder.array([
    this.formBuilder.group({
      name: [''],
      experience: [''],
    }),
  ]),

  drinks: this.formBuilder.array([
    this.formBuilder.group({
      name: [''],
      description: [''],
    }),
  ]),
});

In my specific case, I need to send these three form groups to three separate endpoints.

sendForm() {  
    const team = this.form.controls.profile.value;
    const members = this.form.controls.members.value; 
    const drinks = this.form.controls.drinks.value);

    this.teamService.sendTeam(team).subscribe((response) => {
        console.log(response);
    });
    this.teamService.sendMembers(members).subscribe((response) => {
        console.log(response);
    });
    this.teamService.sendDrinks(drinks).subscribe((response) => {
        console.log(response);
    });
}

I am seeking advice on the best practices for handling such requests. While using three subscribes may not be the most efficient method, it is currently the simplest one. Appreciate any guidance you can provide.

Answer №1

 import { combineLatest } from 'rxjs';
 

 submitFormData() {
          const user = this.form.controls.profile.value;
          const attendees = this.form.controls.members.value; 
          const beverages = this.form.controls.drinks.value);
    
        combineLatest(
          this.teamService.submitUser(user),
          this.teamService.submitAttendees(attendees),
          this.teamService.submitBeverages(beverages),
        ).subscribe(([user, attendees, beverages]) => {
          
         // perform additional tasks
          
        })
      }

Answer №2

let teamData$ = this.teamService.sendTeamData(team);
let memberData$ = this.teamService.sendMemberData(members);
let serviceData$ = this.teamService.sendDrinksData(drinks);

combineLatest(teamData$, memberData$, serviceData$)
       .subscribe(([teamResponse, memberResponse, serviceResponse]) => { 
})

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

Angular 4 validation triggering the highlighting of control labels

In the process of implementing validation in my Angular 4 application, I have encountered an issue that needs some tweaking. The validation itself is functioning correctly, but I would like to modify the way error messages are displayed. Currently, when ...

Learn how to transform an object into an array consisting of multiple objects in TypeScript

The car's details are stored as: var car = {model: 'Rav4', Brand: 'Tayota'} I need to convert this information to an array format like [{model: 'Rav4', Brand: 'Tayota'}] ...

Simplify a function by lowering its cyclomatic complexity

This particular function is designed to determine whether a specific cell on a scrabble board qualifies as a double letter bonus spot. With a cyclomatic complexity of 23, it exceeds the recommended threshold of 20. Despite this, I am unsure of an alterna ...

Unable to access passed parameters from deep links in React Navigation V6

I'm currently working on setting up a simple linking logic to open an app via an invitation link. The link format would be something like this: [scheme]://auth/[invitaion-code] To achieve this, I have set up the following linking object to pass to th ...

Angular default route with parameters

Is it possible to set a default route with parameters in Angular, such as www.test.com/landing-page?uid=123&mode=front&sid=de8d4 const routes: Routes = [ { path: '', redirectTo: 'landing-page', pathMatch: 'full' }, ...

How about: "Are you utilizing the recoil selector for sorting through component

After diving into numerous examples, documentation, and videos about using selectors with recoil, I still can't seem to wrap my head around it. Maybe it's just one of those off days because it shouldn't be this difficult. Here's where ...

Running headless Chrome with Protractor on Windows platform is presenting difficulties

While there is a wealth of documentation available on headless chrome automated testing, information specifically for Windows users seems to be lacking. Furthermore, details on utilizing headless chrome for end-to-end automated testing in a fully develope ...

What is the correct method for importing a Node module into Angular using TypeScript or AngularCLI?

As I integrate some "legacy" (non-typescript) JavaScript libraries into my Angular single page application. Usually, I simply include a CDN link in the index.html file like this: <script src="//cdnjs.cloudflare.com/ajax/libs/pako/1.0.6/pako.min.js"> ...

Save the current page URL from Angular app into a MongoDB database

I'm currently working on an Angular project and trying to find a way to easily copy the current page URL and store it in MongoDB. Here is the URL: http://localhost:4201/contact?pincode=4343&devicetype=desktop&country=india Any suggestions wo ...

Troubleshooting an Azure Web App deployment of a TypeScript Node.js application - encountering a 'service unavailable' message

I recently deployed a react-redux app on Azure using the 'Azure App Services' extension in VSCode. The project was based on the code from this repository: https://github.com/rokoroku/react-redux-typescript-boilerplate Unfortunately, when I try t ...

Ionic 4's http.get.subscribe method fails to retain the retrieved value

I'm aware this might be a repeated question, but I haven't come across a straightforward answer yet, so here it goes. Below is the code snippet in question: fetchData() { let dataArray: Array<any> = [, , ,]; this.prepareDataReque ...

I want to remove an object from my Django Rest Framework (DRF) database using Angular 13

After receiving the id that needs to be deleted, I noticed that the last line of code in service.ts for the delete method is not being executed. The files and code snippets I utilized are: COMPONENT.HTML <li *ngFor="let source of sources$ | async ...

Issue with Angular-CLI freezing during the build process

After transitioning to angular-cli, I encountered an issue with production deployment. While everything runs smoothly on my local Windows 10 PC, the building process freezes at 10% when attempting to build on a CentOS server. Operating System: CentOS 6.6 ...

Switch up row values in an array and transform them into an object using SheetJS

I am struggling to format an array where each "Working Day" is represented as an object with specific details like index and start/end date. I need help manipulating the JSON data to achieve the desired structure. The package I'm currently using is: ...

Seeking a basic feature that can create rectangular shapes and facilitate their movement on the screen within an Angular(8) framework

Looking for an Angular Component that can draw rectangles on the screen, allowing users to move and resize them. I initially tried manipulating div elements but suspect there may already be a component that exists for this purpose. Below is an example of w ...

Replacing text with new content when text is stored in a separate file

After organizing all the strings from my code, I compiled them into a file named constants.ts. export class Constants { public static API_URL = '/api/'; public static CREATE_CHILD_API_URL = Constants.API_URL + '%s' + '/create- ...

Using Pipes to Truncate Text in Angular 4 with PrimeNg

Is there a way to limit the output of a data-table to 150 characters? Here is the pipe I am using: transform(value: string, args: string[]): string { const limit = args.length > 0 ? parseInt(args[0], 10) : 20; const trail = args.length > 1 ...

Steps for deleting an image from a component in Angular before getting a new one from API

In my application, there is a child component responsible for fetching and displaying images from an API on the template. The parent component consists of a list of items, and when a user selects an item from the list, a request is made to the API to retri ...

The reason behind my struggle with mapping API responses to an Interface in Angular

Hello everyone, I am currently working on mapping some API responses from The Muse API to an APIResponseInterface that I have created. Here's a snippet of my code: Service that sends the request: getJobs(page: number): Observable<APIResponseInterf ...

The error message encountered while using webpack with TypeScript and React is: "Unexpected token React.Component

I am currently working on setting up a project using webpack, typescript, and react. I have implemented babel to transpile my typscript/react code. However, when starting the development server, I encountered the following error: Module parse failed: Un ...