import {Component, ViewChild} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; import {NgForm} from '@angular/forms'; import {Subscription} from 'rxjs/Subscription'; import {Campaign} from '../../../models/model-interfaces'; import {CampaignService} from '../../../services/logs/campaign.service'; @Component({ selector: 'campaign-submit', templateUrl: './campaign-submit.component.html', styleUrls: ['./campaign-submit.component.css', '../../../style/entry-form.css', '../../../style/overview.css'] }) export class CampaignSubmitComponent { campaign: Campaign = {}; showErrorLabel = false; error; subscription: Subscription; @ViewChild(NgForm) form: NgForm; constructor(private route: ActivatedRoute, private router: Router, private campaignService: CampaignService) { this.subscription = this.route.params .map(params => params['id']) .filter(id => id !== undefined) .flatMap(id => this.campaignService.getCampaign(id)) .subscribe(campaign => { this.campaign = campaign; }); } saveCampaign() { this.campaignService.submitCampaign(this.campaign) .subscribe(campaign => { let redirectSuccessUrl = '../overview/'; if (this.campaign._id) { redirectSuccessUrl = '../' + redirectSuccessUrl; } this.router.navigate([redirectSuccessUrl + campaign._id], {relativeTo: this.route}); }, error => { this.error = error._body.error.message; this.showErrorLabel = true; }); } cancel() { this.router.navigate(['..'], {relativeTo: this.route}); return false; } }