Advertisement
Guest User

categoryproject-form.component.ts

a guest
May 13th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit, OnDestroy } from '@angular/core';
  2. import { Router, ActivatedRoute } from '@angular/router';
  3. import { NgForm } from '@angular/forms';
  4.  
  5. import { CategoryprojectDataService} from '../../model/categoryproject-data.service';
  6. import { categoryproject } from '../../model/categoryproject';
  7.  
  8.  
  9. @Component({
  10.   templateUrl: './categoryproject-form.component.html',
  11. })
  12. export class CategoryprojectformComponent implements OnInit, OnDestroy {
  13.  
  14.     private _mode = '';
  15.  
  16.     private _id:number;
  17.     private _parameters:any;
  18.     private _category:categoryproject;
  19.  
  20.     private _errorMessage:string;
  21.     private _submitted:boolean = false;
  22.  
  23.     constructor(private _categoryDataService:CategoryprojectDataService,
  24.                 private _router:Router,
  25.                 private _activatedRoute:ActivatedRoute) {}
  26.  
  27.     private _resetCategory(){
  28.         this._category = new categoryproject();
  29.         this._category.project_category_name = '';
  30.     }
  31.  
  32.     public ngOnInit() {
  33.         this._resetCategory();
  34.         this._parameters = this._activatedRoute.params.subscribe(params => {
  35.             if(typeof params['id'] !== "undefined") {
  36.                 this._id = Number.parseInt(params['id']);
  37.                 this._categoryDataService.getCategoryById(this._id)
  38.                     .subscribe(
  39.                         response => {
  40.                             this._category = response;
  41.                             this._mode = 'update';
  42.                         },
  43.                         error => {
  44.                             this._errorMessage = error.data;
  45.                         }
  46.                     );
  47.             } else {
  48.                 this._mode = 'create';
  49.             }
  50.         });
  51.     }
  52.  
  53.     public ngOnDestroy() {
  54.         this._parameters.unsubscribe();
  55.         this._category = new categoryproject();
  56.     }
  57.  
  58.     public onSubmit(form: NgForm) {
  59.         this._submitted = true;
  60.         if(this._mode == 'create') {
  61.             console.log("ini data form "+form);
  62.             this._categoryDataService.addCategory(form.value)
  63.                 .subscribe(
  64.                     result => {
  65.                         if(result.status) {
  66.                             console.log(result.status);
  67.                             this._router.navigate(['/categoryproject']);
  68.                         } else {
  69.                             console.log(result.status);
  70.                             this._submitted = false;
  71.                         }
  72.                     },
  73.                     error => {
  74.                         this._submitted = false;
  75.                         this._errorMessage = error.data;
  76.                     }
  77.                 );
  78.         } else if(this._mode == 'update') {
  79.             this._categoryDataService.updateCategoryById(this._category)
  80.                 .subscribe(
  81.                     result => {
  82.                         if(result.status) {
  83.                             this._router.navigate(['/categoryproject']);
  84.                         } else {
  85.                             this._submitted = false;
  86.                         }
  87.                     },
  88.                     error => {
  89.                         this._submitted = false;
  90.                         this._errorMessage = error.data;
  91.                     }
  92.                 );
  93.         }
  94.     }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement