Skip to content

Instantly share code, notes, and snippets.

@edmilsonlani
Created March 17, 2017 21:49
Show Gist options
  • Save edmilsonlani/67a2f1a4ba33f85d640d734edcb4579a to your computer and use it in GitHub Desktop.
Save edmilsonlani/67a2f1a4ba33f85d640d734edcb4579a to your computer and use it in GitHub Desktop.
search
import { Injectable } from '@angular/core';
import { Response, Http, Headers, RequestOptions } from '@angular/http';
import { Error } from '../models/error';
import { environment } from '../../../environments/environment';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class HttpClient {
public LOCAL_STORAGE: string = 'authorization';
constructor(private http: Http) {}
createHeader(headers: Headers) {
headers.append('Content-Type', 'application/json');
headers.append('Access-Control-Allow-Origin', '*');
if (localStorage[this.LOCAL_STORAGE])
headers.append('Authorization', 'Bearer ' + localStorage[this.LOCAL_STORAGE]);
}
endPointUrl(path: string) {
return environment.endpoint + path;
}
get(url) {
let headers = new Headers();
this.createHeader(headers);
let options = new RequestOptions({ headers: headers });
return this.http.get(this.endPointUrl(url), options)
.map(this.mapData)
.catch(this.throwError);;
}
post(url, data) {
let headers = new Headers();
this.createHeader(headers);
let options = new RequestOptions({ headers: headers });
return this.http.post(this.endPointUrl(url), JSON.stringify(data), options)
.map(this.mapData)
.catch(this.throwError);
}
mapData(response: Response) {
let data = response.json();
return data || {};
}
throwError(response: any) {
console.log(response);
let error = new Error(response.status, response.message);
return Observable.throw(error);
}
}
import { Component, OnInit } from '@angular/core';
import { environment } from '../../environments/environment';
import { SearchService } from './search.service';
@Component({
selector: 'app-search',
templateUrl: './search.component.html'
})
export class SearchComponent implements OnInit {
types = [
{ title: 'Código do cupom', apiUrl: 'search/bycode/', externalUrl: `${environment.legacyUrl}Coupon/Manage/`, autocomplete: false },
{ title: 'Nome', apiUrl: 'search/bydealname/', externalUrl: `${environment.legacyUrl}Report/DealStatistics/`, autocomplete: true },
{ title: 'ID Unificado', apiUrl: 'search/bydealid/', externalUrl: `${environment.legacyUrl}Coupon/Validate/`, autocomplete: false },
{ title: 'Nº do Contrato', apiUrl: 'search/bycontract/', externalUrl: `${environment.legacyUrl}Deal/Contract/`, autocomplete: false },
{ title: 'OF', apiUrl: 'search/byof/', externalUrl: `${environment.legacyUrl}Search/OF/`, autocomplete: false }
];
selectedType: any = {};
term: string;
errorMessage: string;
constructor(private searchService: SearchService) { }
ngOnInit() {
this.selectedType = this.types[0];
}
selectType(type) {
this.selectedType = type;
}
search() {
this.searchService.search(this.selectedType.apiUrl, this.term)
.subscribe(
response => {
if (response)
window.location.href = `${this.selectedType.externalUrl}${this.term}`;
else
this.errorMessage = `${this.selectedType.title} não encontrado.`
},
error => {
this.errorMessage = `${this.selectedType.title} não encontrado.`
}
)
}
}
import { Injectable } from '@angular/core';
import { Http, RequestOptions, Headers, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '../core/clients/http-client';
@Injectable()
export class SearchService {
constructor(private http: Http, private httpClient: HttpClient) { }
search(apiUrl: string, term: string) {
return this.httpClient.get(apiUrl + term);
}
searchAutocomplete(apiUrl: string) {
return this.httpClient.get(apiUrl);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment