import { Component } from "@angular/core" ; import { AlertController , ModalController , NavParams } from "@ionic/angular" ; import { TranslateService } from "@ngx-translate/core" ; import lodash from "lodash" ; import { StoredNetwork } from "@/models/stored-network" ; import { ToastProvider } from "@/services/toast/toast" ; import { UserDataService } from "@/services/user-data/user-data.interface" ; export enum EditNetworkAction { Update , Delete } export interface EditNetworkResult { networkId : string ; action : EditNetworkAction ; } @ Component ( { selector : "customNetworkEdit" , templateUrl : "custom-network-edit.html" , } ) export class CustomNetworkEditModal { public network : StoredNetwork ; public networkId : string ; public isDefault : boolean ; public constructor ( public navParams , private modalCtrl , private alertCtrl , private userDataService , private translateService , private toastProvider ) { const defaultNetworksNames = this . userDataService . defaultNetworks . map ( ( item ) => item . name ) ; this . network = this . navParams . get ( "network" ) || new StoredNetwork ( ) ; this . networkId = this . navParams . get ( "id" ) ; this . isDefault = defaultNetworksNames . includes ( this . network . name ) ; } dismiss ( result ? ) { this . modalCtrl . dismiss ( result ) ; } public save ( ) { this . userDataService . addOrUpdateNetwork ( this . network , this . networkId ) . subscribe ( ( network ) => this . dismiss ( { action : EditNetworkAction . Update , networkId : network . id , } ) ) ; } public prepareDelete ( ) { if ( lodash . some ( this . userDataService . profiles , ( p ) => p . networkId === this . networkId ) ) { this . toastProvider . error ( "CUSTOM_NETWORK.DELETE_FAIL_NOT_EMPTY" ) ; return ; } this . translateService . get ( [ "CUSTOM_NETWORK.CONFIRM_DELETE" , "NO" , "YES" ] ) . subscribe ( async ( translations ) => { const alert = await this . alertCtrl . create ( { header : translations [ "CUSTOM_NETWORK.CONFIRM_DELETE" ] , buttons : [ { text : translations . NO , role : "cancel" , handler : ( ) => { } , } , { text : translations . YES , handler : ( ) => this . delete ( ) , } , ] , } ) ; alert . present ( ) ; } ) ; } private delete ( ) { this . userDataService . removeNetworkById ( this . networkId ) . subscribe ( ( ) => this . dismiss ( { action : EditNetworkAction . Delete , networkId : this . networkId , } ) ) ; } }