1

I have in my parents component a list of dates. A button opens a modal with a form that allows me to add a new object to the list. As soon as I clicked the save button, the modal closes. However, my list is not updated. Only when I reload the page the object appears in the list.

How do I make it so that when I click the save button, the list in the parent component is automatically updated?

Of course I have looked at all the tutorials and help pages but they mostly use the Bootstrap modal which I don't use.

My parent component.ts:

 import { Component, OnInit } from '@angular/core';
import { lablesHeadlines, lablesConfigAnalytics } from 'src/environments/lables';
import { MapSettingsClientServiceService } from 'src/app/services/map-settings-client-service.service';
import { AnalyticsIndices } from 'src/build/openapi';
import { ActivatedRoute, Router } from '@angular/router';
import { HttpClient} from '@angular/common/http';
import { ModalController } from '@ionic/angular';
import { ModalAnalyticIndicesComponent } from '../../modals/modal-analytic-indices/modal-analytic-indices.component';
import { Subscription } from 'rxjs';
import { ModalAnalyticIndicesPostComponent } from '../../modals/modal-analytic-indices-post/modal-analytic-indices-post.component';
import { switchMap } from 'rxjs/operators';


@Component({
  selector: 'app-config-analytics-value',
  templateUrl: './config-analytics-value.page.html',
  styleUrls: ['./config-analytics-value.page.scss'],
})
export class ConfigAnalyticsValuePage implements OnInit {

  headlines = lablesHeadlines;
  lablesConfigAnalytics = lablesConfigAnalytics;
  analyticsIndicesValue: AnalyticsIndices[];
  indexNummer: number;
  indexName: string;
  analyticsIndices: AnalyticsIndices;
  status;
  errorMessage;
  // eslint-disable-next-line @typescript-eslint/member-ordering
  deleteOperationSuccessfulSubscription: Subscription;

  constructor(private configClientService: MapSettingsClientServiceService,
    private route: ActivatedRoute, private router: Router, private http: HttpClient, private modalCtrl: ModalController) { }

  ngOnInit() {
    this.route.paramMap.subscribe(params => {
      this.indexNummer = parseInt(params.get('indexNummer'), 10);
      this.indexName = params.get('indexName');

    });
    this.configClientService.getAllAnalyticIndicesByIndexNummerAndIndexName(this.indexNummer,this.indexName).subscribe(
      (response) => {
        this.analyticsIndicesValue = response;
        return response;
      });

  }
  openAnalyticIndices(){
    this.router.navigate(['/config-analytics']);
  }
  openSettings(){
    this.router.navigate(['/settings']);
  }
  async openModal(value: any) {
    console.log('open modal');
    const modal = await this.modalCtrl.create({
      component: ModalAnalyticIndicesComponent,
      componentProps: {
        id: value.id,
        indexNummer: value.indexNummer,
        indexName: value.indexName,
        minVal: value.minVal,
        maxVal: value.maxVal,
        indexWert: value.indexWert

      }
    });
    await modal.present();
  }
  async openModalPost() {
    console.log('open modal');
    const modal = await this.modalCtrl.create({
      component: ModalAnalyticIndicesPostComponent,
    });
    return await modal.present();
  }
  deleteValue(id: number){
    this.deleteOperationSuccessfulSubscription =
    this.configClientService.deleteAnalyticIndiceById(id).pipe(
       switchMap(async () => this.getNewData())
    )
    .subscribe(()=> this.getNewData());
  }
  getNewData(){
    this.configClientService.getAllAnalyticIndicesByIndexNummerAndIndexName(this.indexNummer,this.indexName).subscribe(
      (response) => {
        this.analyticsIndicesValue = response;
        return response;
      });
  }
}

My Modal component.ts

import { Component, Input, OnInit } from '@angular/core';
import { lablesHeadlines } from 'src/environments/lables';
import { MapSettingsClientServiceService } from 'src/app/services/map-settings-client-service.service';
import { AnalyticsIndices } from 'src/build/openapi';

@Component({
  selector: 'app-modal-analytic-indices',
  templateUrl: './modal-analytic-indices.component.html',
  styleUrls: ['./modal-analytic-indices.component.scss'],
})
export class ModalAnalyticIndicesComponent implements OnInit {

  headlines = lablesHeadlines;
  id;
  indexNummer;
  indexName;
  minVal;
  maxVal;
  indexWert;
  analyticsIndices: AnalyticsIndices;
  stringifiedData: any;
  // eslint-disable-next-line @typescript-eslint/member-ordering
  @Input() inputData: any;

  constructor(private configClientService: MapSettingsClientServiceService ) {}

  ngOnInit() {
    console.log(this.id);
  }

  onSubmit(data: {id: number; indexNummer: number; indexName: string; indexWert: number; maxVal: number; minVal: number}) {
    data.id= this.id;
    console.warn(data);
    this.stringifiedData = JSON.stringify(data);
    this.configClientService.putAnalyticIndiceById(this.id,this.stringifiedData).subscribe((result)=>{
      console.warn(result);
    });
  }

}

My savebutton in my modal.html

<ion-header>
  <ion-toolbar>
    <ion-title>
      Bearbeiten der Indizes
    </ion-title>
  </ion-toolbar>
</ion-header>
<ion-content>
  <form #addAnalyticIndicesForm="ngForm" (ngSubmit)="onSubmit(addAnalyticIndicesForm.value)">
    <ion-list>
      <ion-item>
        <ion-col size="3">
          <ion-label>IndexNummer:</ion-label>
        </ion-col>
        <ion-col size="4">
          <ion-input name="indexNummer" type="number" ngModel placeholder={{indexNummer}}></ion-input>
        </ion-col>
      </ion-item>
      <ion-item>
        <ion-col size="3">
          <ion-label>IndexName:</ion-label>
        </ion-col>
        <ion-col size="9">
          <ion-input name="indexName" type="text" ngModel placeholder={{indexName}}></ion-input>
        </ion-col>
      </ion-item>
      <ion-item>
        <ion-col size="3">
          <ion-label>MinVal:</ion-label>
        </ion-col>
        <ion-col size="4">
          <ion-input name="minVal" type="number" ngModel placeholder={{minVal}}></ion-input>
        </ion-col>
      </ion-item>
      <ion-item>
        <ion-col size="3">
          <ion-label>MaxVal:</ion-label>
        </ion-col>
        <ion-col size="4">
          <ion-input name="maxVal" type="number" ngModel placeholder={{maxVal}}></ion-input>
        </ion-col>
      </ion-item>
      <ion-item>
        <ion-col size="3">
          <ion-label>IndexWert:</ion-label>
        </ion-col>
        <ion-col size="4">
          <ion-input name="indexWert" type="number" ngModel placeholder={{indexWert}}></ion-input>
        </ion-col>
      </ion-item>
    </ion-list>

    <ion-button type="submit" expand="block" (click)="modal.dismiss()">Save</ion-button>
  </form>
  <ion-button expand="block" color="danger" (click)="modal.dismiss()">Cancel</ion-button>
</ion-content>


Doncarlito87
  • 359
  • 1
  • 8
  • 25

2 Answers2

1

You can refresh the data from the server or update the local data with the value from the modal when the modal is closed.

Also, do not subscribe in a subscribe in delete value

deleteValue(id: number){
   this.deleteOperationSuccessfulSubscription = 
   this.configClientService.deleteAnalyticIndiceById(id).pipe(
      switchMap(()=> getNewData())
   )
   .subscribe(()=> this.getNewData());
}
Alexander
  • 3,019
  • 1
  • 20
  • 24
  • Thanks for your quick reply. When I add the pipe then it asks for switchMap. So how can I add switchMap? To the upper point: This is of course exactly what I want. Only for this I lack a little bit the practice to implement this. How can I update the data as soon as I close my modal? – Doncarlito87 Jun 24 '22 at 09:54
  • Do you mean to import switchMap from rxjs/operators? For the dialog what dialog are you using? It seems like this.modalCtrl.create is returning a promise instead of an observable? – Alexander Jun 24 '22 at 11:18
  • Okay the delete function I have rebuilt. I did not know that the switchMap variable is an import from rxjs. Thanks for that. Unfortunately, I can no longer find the tutorial from which I have the modal implementation. But it is the ModalController from @ionic/angular I can upload the whole code. Maybe it will help a little more. – Doncarlito87 Jun 24 '22 at 14:19
0

I found the solution in this stackoverflow ticket: https://stackoverflow.com/a/52785908/12440701

If you work with the ModalController of @ionic/angular then the ModalController has a method called onDidDismiss() which you can use for this.

async openModalPost() {
    console.log('open modal');
    const modal = await this.modalCtrl.create({
      component: ModalAnalyticIndicesPostComponent,
    });
    modal.onDidDismiss()
    .then(() => {
      this.getNewData();
    });

    return await modal.present();
  }
Doncarlito87
  • 359
  • 1
  • 8
  • 25