Skip to content

Commit ff7c0ee

Browse files
committed
Show all categories in index
1 parent 7a76276 commit ff7c0ee

File tree

3 files changed

+45
-35
lines changed

3 files changed

+45
-35
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ <h3 class="text-center text-light">Haz tuyo lo de los demás…</h3>
1616
<h2 class="text-center text-light">Todas las categorías que imaginas</h2>
1717
<div class="container">
1818
<div class="full-width container-category">
19-
<!-- {{#Categories}}
20-
<a href="/commercial/{{ID_CATEGORY}}" id="categori-{{ID_CATEGORY}}">
21-
<i aria-hidden="true" class="fa fa-{{ICON}}"></i> -->
22-
<!-- <span>{{TITLE}}</span> -->
23-
<!-- </a> -->
24-
<!-- {{/Categories}} -->
19+
<a *ngFor="let category of categories" href="/commercial/{{category.id_CATEGORY}}" id="categori-{{category.id_CATEGORY}}">
20+
<i aria-hidden="true" class="fa fa-{{category.icon}}"></i>
21+
<span>{{category.title}}</span>
22+
</a>
2523
</div>
2624
</div>
2725
</section>
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import {Component, OnInit} from '@angular/core';
2+
import {Category} from '../../models/category.model';
3+
import {Router} from '@angular/router';
4+
import {CategoryService} from '../../services/category.service';
25

36
@Component({
47
selector: 'index',
58
templateUrl: './index.component.html'
69
})
710
export class IndexComponent implements OnInit {
11+
12+
categories: Category[];
13+
14+
constructor(private categoryService: CategoryService) {
15+
}
16+
817
ngOnInit(): void {
9-
// Cargar categorías
18+
this.categoryService.getCategories().subscribe(
19+
category => this.categories = category,
20+
error => console.log(error)
21+
);
1022
}
1123

1224
}

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,60 +14,60 @@ export class CategoryService {
1414
constructor(private httpClient: HttpClient, private router: Router) {
1515
}
1616

17+
static handleError(error: any): Observable<never> {
18+
console.log('ERROR:');
19+
console.error(error);
20+
return throwError('Server error (' + error.status + '): ' + error.text());
21+
}
22+
1723
getCategories(): Observable<Category[]> {
18-
return this.httpClient.get(BASE_URL).pipe(
19-
catchError(error => this.handleError(error))
24+
return this.httpClient.get(BASE_URL + 'categories').pipe(
25+
catchError(error => CategoryService.handleError(error))
2026
) as Observable<Category[]>;
2127
}
2228

2329
getCategory(id: number | string): Observable<Category> {
24-
return this.httpClient.get(BASE_URL + id).pipe(
25-
catchError(error => this.handleError(error))
30+
return this.httpClient.get(BASE_URL + 'categories/' + id).pipe(
31+
catchError(error => CategoryService.handleError(error))
2632
) as Observable<Category>;
2733
}
2834

29-
addCategory(title: string, description: string, icon: string) {
35+
addCategory(title: string, description: string, icon: string): Observable<any> {
3036

31-
this.httpClient.post(BASE_URL + 'admin/categories', {title, description, icon}, {withCredentials: true})
37+
/*this.httpClient.post(BASE_URL + 'admin/categories', {title, description, icon}, {withCredentials: true})
3238
.subscribe(
3339
(response) => this.router.navigate(['profile']),
3440
(error) => alert('Usuario ya existe, inicie sesión')
35-
);
36-
/*return this.httpClient.post(BASE_URL + 'admin/categories', {title, description, icon})
37-
.pipe(
38-
catchError(error => this.handleError(error))
3941
);*/
42+
return this.httpClient.post(BASE_URL + 'admin/categories', {title, description, icon}, {withCredentials: true})
43+
.pipe(
44+
catchError(error => CategoryService.handleError(error))
45+
);
4046
}
4147

42-
setCategoryImage(category: Category, formData: FormData) {
43-
return this.httpClient.post(BASE_URL + category.id_CATEGORY + '/image', formData)
48+
setCategoryImage(category: Category, formData: FormData): Observable<any> {
49+
return this.httpClient.post(BASE_URL + 'admin/categories/' + category.id_CATEGORY + '/image', formData)
4450
.pipe(
45-
catchError(error => this.handleError(error))
51+
catchError(error => CategoryService.handleError(error))
4652
);
4753
}
4854

49-
deleteCategoryImage(category: Category) {
50-
return this.httpClient.delete(BASE_URL + category.id_CATEGORY + '/image')
55+
deleteCategoryImage(category: Category): Observable<any> {
56+
return this.httpClient.delete(BASE_URL + 'admin/categories/' + category.id_CATEGORY + '/image')
5157
.pipe(
52-
catchError(error => this.handleError(error))
58+
catchError(error => CategoryService.handleError(error))
5359
);
5460
}
5561

56-
deleteCategory(category: Category) {
57-
return this.httpClient.delete(BASE_URL + category.id_CATEGORY).pipe(
58-
catchError(error => this.handleError(error))
62+
deleteCategory(category: Category): Observable<any> {
63+
return this.httpClient.delete(BASE_URL + 'admin/categories/' + category.id_CATEGORY).pipe(
64+
catchError(error => CategoryService.handleError(error))
5965
);
6066
}
6167

62-
updateCategory(category: Category) {
63-
return this.httpClient.put(BASE_URL + category.id_CATEGORY, category).pipe(
64-
catchError(error => this.handleError(error))
68+
updateCategory(category: Category): Observable<any> {
69+
return this.httpClient.put(BASE_URL + 'admin/categories/' + category.id_CATEGORY, category).pipe(
70+
catchError(error => CategoryService.handleError(error))
6571
);
6672
}
67-
68-
private handleError(error: any) {
69-
console.log('ERROR:');
70-
console.error(error);
71-
return throwError('Server error (' + error.status + '): ' + error.text());
72-
}
7373
}

0 commit comments

Comments
 (0)