Skip to content

Commit 18bd435

Browse files
committed
Search form working with city and query
1 parent 9c19c2f commit 18bd435

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

angular/wallypop/.idea/workspace.xml

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

angular/wallypop/src/app/components/articles/commercial.component.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,43 @@ export class CommercialComponent implements OnInit {
1616
categories: Category[];
1717
category: Category;
1818
idCategory: number;
19+
filtered: boolean;
20+
query: string;
21+
city: string;
22+
noArticles = false;
23+
// tslint:disable-next-line:max-line-length
1924
constructor(private articleService: ArticleService, private categoryService: CategoryService, private loginService: LoginService, private routing: ActivatedRoute) {
2025
this.idCategory = -1;
26+
this.filtered = false;
2127
}
2228

2329
ngOnInit(): void {
2430
this.getCategories();
25-
2631
this.idCategory = this.routing.snapshot.params.id;
2732
if (this.idCategory !== undefined) {
2833
this.getArticlesFromCategory(this.idCategory);
2934
} else {
30-
this.getAllArticles();
35+
this.queryParams();
36+
if (this.filtered) {
37+
this.getFilteredArticles(this.query, this.city);
38+
} else {
39+
this.getAllArticles();
40+
}
3141
}
3242
}
3343

44+
queryParams(): void {
45+
this.routing.queryParams.subscribe(params => {
46+
if (params.query !== undefined && params.city !== undefined) {
47+
this.filtered = true;
48+
this.query = params.query;
49+
this.city = params.city;
50+
} else {
51+
this.filtered = false;
52+
}
53+
});
54+
}
55+
3456
getCategories(): void {
3557
this.categoryService.getCategories().subscribe(
3658
category => this.categories = category,
@@ -52,4 +74,11 @@ export class CommercialComponent implements OnInit {
5274
);
5375
}
5476

77+
getFilteredArticles(query: string, city: string): void {
78+
this.articleService.getFilteredArticles(query, city).subscribe(
79+
article => this.articles = article,
80+
error => console.log(error)
81+
);
82+
}
83+
5584
}

angular/wallypop/src/app/components/search/search.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="container">
22
<div class="row">
33
<div class="col-xs-12">
4-
<form action="/search" class="form-inline text-center" enctype="text/plain" method="GET">
4+
<form ngNoForm action="/commercial" class="form-inline text-center" enctype="text/plain" method="GET">
55
<div class="form-group">
66
<input class="form-control input-lg" name="query" placeholder="Estoy buscando..." type="text">
77
<input class="form-control input-lg" name="city" placeholder="Ciudad, Provincia..." type="text"/>

angular/wallypop/src/app/services/article.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,9 @@ export class ArticleService {
8686
return throwError('Server error (' + error.status + '): ' + error.text());
8787
}
8888

89+
getFilteredArticles(query: string, city: string): Observable<Article[]> {
90+
return this.httpClient.get(BASE_URL + 'articles/search?query=' + query + '&city=' + city).pipe(
91+
catchError(error => this.handleError(error))
92+
) as Observable<Article[]>;
93+
}
8994
}

0 commit comments

Comments
 (0)