Creation of side-menu.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable, inject } from '@angular/core';
|
||||
import { lastValueFrom } from 'rxjs';
|
||||
import { Category } from './model/category';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class CategoryRestService {
|
||||
private httpClient = inject(HttpClient);
|
||||
|
||||
getCategories(): Promise<Category[]> {
|
||||
return lastValueFrom(this.httpClient.get<Category[]>('/api/categories'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export interface Category {
|
||||
id: string;
|
||||
name: string;
|
||||
subCategories: Category[];
|
||||
}
|
||||
25
frontend/src/app/core/service/category.service.ts
Normal file
25
frontend/src/app/core/service/category.service.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { Injectable, inject } from '@angular/core';
|
||||
import { CategoryRestService } from '../rest-services/category/category.rest-service';
|
||||
import { BehaviorSubject, Observable } from 'rxjs';
|
||||
import { Category } from '../rest-services/category/model/category';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class CategoryService {
|
||||
private categoryRestService = inject(CategoryRestService);
|
||||
private categoriesSubject = new BehaviorSubject<Category[]>([]);
|
||||
|
||||
private get categories(): Category[] {
|
||||
return this.categoriesSubject.value;
|
||||
}
|
||||
|
||||
get categories$(): Observable<Category[]> {
|
||||
if (!this.categories?.length) {
|
||||
this.categoryRestService.getCategories()
|
||||
.then(categories => this.categoriesSubject.next(categories))
|
||||
.catch(error => console.error('An error occured while loading categories.', error));
|
||||
}
|
||||
return this.categoriesSubject.asObservable();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user