Creation of side-menu.

This commit is contained in:
Florian THIERRY
2024-04-02 16:18:03 +02:00
parent 0900df463a
commit c54e1c57d7
13 changed files with 258 additions and 3 deletions

View File

@@ -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'));
}
}

View File

@@ -0,0 +1,5 @@
export interface Category {
id: string;
name: string;
subCategories: Category[];
}

View 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();
}
}