import { Component, ViewChild, Injector, Output, EventEmitter} from '@angular/core'; import { ModalDirective } from 'ngx-bootstrap'; import { finalize } from 'rxjs/operators'; import { PostalCodesServiceProxy, CreateOrEditPostalCodeDto } from '@shared/service-proxies/service-proxies'; import { AppComponentBase } from '@shared/common/app-component-base'; import * as moment from 'moment'; import * as countries from '@app/sprintship/countries.json'; @Component({ selector: 'createOrEditPostalCodeModal', templateUrl: './create-or-edit-postalCode-modal.component.html' }) export class CreateOrEditPostalCodeModalComponent extends AppComponentBase { @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective; @Output() modalSave: EventEmitter = new EventEmitter(); active = false; saving = false; postalCode: CreateOrEditPostalCodeDto = new CreateOrEditPostalCodeDto(); constructor( injector: Injector, private _postalCodesServiceProxy: PostalCodesServiceProxy ) { super(injector); } onShown(): void { // $('#CountryJson').select2(); $('#CountryJson').select2(); var $select=$('#CountryJson'); if(this.postalCode.country == null) { $select.html('') for (var i = 0 ; i< countries.default.length; i++){ $select.append('') } } else{ $select.html('') for (var i = 0 ; i< countries.default.length; i++){ if(this.postalCode.country == countries.default[i]['text']){ $select.append('') } else{ $select.append('') } } } // $.getJSON(countries, function(data){ // console.log(data) // }); } show(postalCodeId?: number): void { if (!postalCodeId) { this.postalCode = new CreateOrEditPostalCodeDto(); this.postalCode.id = postalCodeId; this.active = true; this.modal.show(); } else { this._postalCodesServiceProxy.getPostalCodeForEdit(postalCodeId).subscribe(result => { this.postalCode = result.postalCode; this.active = true; this.modal.show(); }); } } save(): void { this.saving = true; this.postalCode.country= ( document.getElementById("CountryJson")).value this._postalCodesServiceProxy.createOrEdit(this.postalCode) .pipe(finalize(() => { this.saving = false;})) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); } close(): void { this.active = false; this.modal.hide(); } }