TrackingSellerComponent.ts
import { Component, OnInit, trigger, transition, style, animate } from '@angular/core';
import { TrackingSellerService } from './tracking-seller.service';
import * as _ from 'lodash';
import { Http } from '@angular/http';
import { ConfirmationPopoverModule } from 'angular-confirmation-popover';
@Component({
selector: 'app-tracking-seller',
animations: [
trigger(
'enterAnimation', [
transition(':enter', [
style({transform: 'translateX(100%)', opacity: 0}),
animate('500ms', style({transform: 'translateX(0)', opacity: 1}))
]),
transition(':leave', [
style({transform: 'translateX(0)', opacity: 1}),
animate('500ms', style({transform: 'translateX(100%)', opacity: 0}))
])
]
)
],
templateUrl: './tracking-seller.component.html',
styleUrls: ['./tracking-seller.component.css']
})
export class TrackingSellerComponent implements OnInit {
service: TrackingSellerService;
sellers: TrackingSellerComponent[] = [];
currentPage: TrackingSellerComponent[] = [];
last_page: Object[];
id: string;
confirmClicked: boolean = false;
cancelClicked: boolean = false;
loading: boolean = false;
constructor(service: TrackingSellerService, http: Http) {
this.loading = true;
this.service = service;
this.service
.lista()
.subscribe(sellers => {
this.sellers = sellers;
this.currentPage = sellers;
this.loading = false;
}, erro => console.log(erro));
http
.get('http://198.199.70.169/api/v1/paginate/all_sellers')
.map(res => res.json().last_page)
.subscribe(data => {
this.last_page = data;
this.last_page =_.range(1, data + 1);
});
}
ngOnInit() {
}
navigate(ev, page) {
ev.preventDefault();
this.loading = true;
this.service
.paginate(page)
.subscribe(sellers => {
this.sellers = sellers;
this.loading = false;
}, erro => console.log(erro));
}
nextPrev(ev, page, last_page) {
ev.preventDefault();
if(page > last_page){
return false;
}
this.service
.paginate(page)
.subscribe(sellers => {
this.sellers = sellers;
}, erro => console.log(erro));
}
onConfirmDelete(seller, currentPage) {
this.loading = true;
this.service
.delete(seller)
.subscribe(() => {
this.service
.paginate(currentPage)
.subscribe(sellers => {
this.sellers = sellers;
this.loading = false;
}, erro => console.log(erro));
},
erro => {
console.log(erro);
});
}
}
TrackingSellerService.ts
import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import { Observable } from 'rxjs';
import { TrackingSellerComponent } from './tracking-seller.component';
@Injectable()
export class TrackingSellerService {
http: Http;
headers: Headers;
urlPaginate: string = 'http://198.199.70.169/api/v1/paginate/all_sellers';
url: string = 'http://198.199.70.169/api/v1/sellers';
constructor(http: Http) {
this.http = http;
this.headers = new Headers();
this.headers.append('Content-Type', 'application/json');
}
lista(): Observable<TrackingSellerComponent[]> {
return this.http
.get(this.urlPaginate)
.map(res => res.json());
}
paginate(page: string): Observable<TrackingSellerComponent[]> {
return this.http
.get(this.urlPaginate + '?page=' + page)
.map(res => res.json());
}
delete(seller: TrackingSellerComponent): Observable<Response> {
return this.http.delete(this.url + '/' + seller.id);
}
update(seller: any): Observable<TrackingSellerComponent> {
return this.http
.put(this.url + '/' + seller.id, JSON.stringify(seller), { headers: this.headers })
.map(res => res.json());
}
searchById(id: string): Observable<any> {
return this.http
.get(this.url + '/' + id)
.map(res => res.json());
}
}
Ali no searchById coloquei como any para teste, mas estava como TrackingSellerComponent
RegisterSellerComponent.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { TrackingSellerService } from './../tracking-seller/tracking-seller.service';
import { TrackingSellerComponent } from './../tracking-seller/tracking-seller.component';
@Component({
selector: 'app-register-seller',
templateUrl: './register-seller.component.html',
styleUrls: ['./register-seller.component.css']
})
export class RegisterSellerComponent implements OnInit {
seller: any;
route: ActivatedRoute;
router: Router;
service: TrackingSellerService;
constructor(
service: TrackingSellerService,
route: ActivatedRoute
) {
this.route = route;
this.service = service;
this.route.params.subscribe(params => {
let id = params['id'];
console.log(id);
if (id) {
this.service
.searchById(id)
.subscribe(seller => {
this.seller = seller;
console.log(this.seller);
},
erro => console.log(erro));
}
});
}
ngOnInit() {
}
}
Também coloquei o seller como any para teste.