import { Component , OnInit } from '@angular/core' ; import { ApiService } from 'src/app/Service/api.service' ; import { Product } from 'src/app/Model/product' ; import { HttpEventType , HttpResponse } from '@angular/common/http' ; import { NavigationExtras , Router } from '@angular/router' ; import { Route } from '@angular/compiler/src/core' ; @ Component ( { selector : 'app-admin' , templateUrl : './admin.component.html' , styleUrls : [ './admin.component.css' ] } ) export class AdminComponent implements OnInit { products : Product [ ] = [ ] ; fileToUpload : File = null ; showAdd = false ; auth : string ; constructor ( private api , private router ) { } imageUrl : string = "/assets/img/noimage.png" ; ngOnInit ( ) { if ( this . api . isAuthenticated ) { this . auth = this . api . getToken ( ) ; this . api . getProducts ( ) . subscribe ( res => { this . products = res . oblist ; } ) ; } } handleFileInput ( file ) { this . fileToUpload = file . item ( 0 ) ; var reader = new FileReader ( ) ; reader . onload = ( event ) => { this . imageUrl = event . target . result ; } ; reader . readAsDataURL ( this . fileToUpload ) ; } show ( ) { this . showAdd = true ; } hide ( ) { this . showAdd = false ; } addProd ( desc , quan , price , prodname , image ) { this . api . addProduct ( desc . value , quan . value , price . value , prodname . value , this . fileToUpload ) . subscribe ( res => { this . products = res . oblist ; } ) ; } delProd ( prodid ) { this . api . deleteProduct ( prodid . value ) . subscribe ( res => { this . products = res . oblist ; this . ngOnInit ( ) ; } ) ; } edit ( prodid ) { let navigationExtras = { queryParams : { "user" : prodid . value } } ; this . router . navigate ( [ "admin/edit" ] , navigationExtras ) ; } }