Compare commits
1 Commits
main
...
936127aa09
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
936127aa09 |
@@ -1,6 +1,6 @@
|
|||||||
@for (category of categories(); track category.id) {
|
@for (category of categories$ | async; track category) {
|
||||||
<div class="category {{category.isOpenned ? 'openned' : ''}}">
|
<div class="category {{category.isOpenned ? 'openned' : ''}}">
|
||||||
<div id="category-{{category.id}}" class="category-header" (click)="setOpened(category)">
|
<div id="category-{{category.id}}" class="category-header" (click)="setOpenned(category)">
|
||||||
{{ category.name }}
|
{{ category.name }}
|
||||||
<mat-icon>chevron_right</mat-icon>
|
<mat-icon>chevron_right</mat-icon>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import {CommonModule} from "@angular/common";
|
import {CommonModule} from "@angular/common";
|
||||||
import {Component, inject, OnInit, output, Signal} from "@angular/core";
|
import {Component, EventEmitter, inject, OnInit, Output} from "@angular/core";
|
||||||
import {MatIconModule} from "@angular/material/icon";
|
import {MatIconModule} from "@angular/material/icon";
|
||||||
import {DisplayableCategory, SideMenuService} from "../side-menu.service";
|
import {DisplayableCategory, SideMenuService} from "../side-menu.service";
|
||||||
|
import {Observable} from "rxjs";
|
||||||
import {RouterModule} from "@angular/router";
|
import {RouterModule} from "@angular/router";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@@ -15,44 +16,45 @@ import {RouterModule} from "@angular/router";
|
|||||||
styleUrl: './categories-menu.component.scss'
|
styleUrl: './categories-menu.component.scss'
|
||||||
})
|
})
|
||||||
export class CategoriesMenuComponent implements OnInit {
|
export class CategoriesMenuComponent implements OnInit {
|
||||||
readonly #sideMenuService = inject(SideMenuService);
|
private sideMenuService = inject(SideMenuService);
|
||||||
categoryClicked = output<void>();
|
@Output()
|
||||||
|
categoryClicked = new EventEmitter<void>();
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.#sideMenuService.loadCategories();
|
this.sideMenuService.loadCategories();
|
||||||
}
|
}
|
||||||
|
|
||||||
get categories(): Signal<DisplayableCategory[]> {
|
get categories$(): Observable<DisplayableCategory[]> {
|
||||||
return this.#sideMenuService.categories;
|
return this.sideMenuService.categories$;
|
||||||
}
|
}
|
||||||
|
|
||||||
setOpened(category: DisplayableCategory): void {
|
setOpenned(category: DisplayableCategory): void {
|
||||||
if (category.isOpenned) {
|
if (category.isOpenned) {
|
||||||
const categoryDiv = document.getElementById(`category-${category.id}`);
|
const categoryDiv = document.getElementById(`category-${category.id}`);
|
||||||
if (categoryDiv) {
|
if (categoryDiv) {
|
||||||
this.#closeAccordion(categoryDiv);
|
this.closeAccordion(categoryDiv);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const categoriesDivs = document.getElementsByClassName('category-header');
|
const categoriesDivs = document.getElementsByClassName('category-header');
|
||||||
Array.from(categoriesDivs)
|
Array.from(categoriesDivs)
|
||||||
.map(category => category as HTMLElement)
|
.map(category => category as HTMLElement)
|
||||||
.forEach(categoryDiv => this.#closeAccordion(categoryDiv));
|
.forEach(categoryDiv => this.closeAccordion(categoryDiv));
|
||||||
|
|
||||||
const categoryDiv = document.getElementById(`category-${category.id}`);
|
const categoryDiv = document.getElementById(`category-${category.id}`);
|
||||||
if (categoryDiv) {
|
if (categoryDiv) {
|
||||||
this.#openAccordion(categoryDiv);
|
this.openAccordion(categoryDiv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.#sideMenuService.setOpened(category);
|
this.sideMenuService.setOpenned(category);
|
||||||
}
|
}
|
||||||
|
|
||||||
#closeAccordion(categoryDiv: HTMLElement): void {
|
private closeAccordion(categoryDiv: HTMLElement): void {
|
||||||
const divContent = categoryDiv?.nextElementSibling as HTMLElement;
|
const divContent = categoryDiv?.nextElementSibling as HTMLElement;
|
||||||
divContent.style.maxHeight = '0';
|
divContent.style.maxHeight = '0';
|
||||||
}
|
}
|
||||||
|
|
||||||
#openAccordion(categoryDiv: HTMLElement): void {
|
private openAccordion(categoryDiv: HTMLElement): void {
|
||||||
const divContent = categoryDiv?.nextElementSibling as HTMLElement;
|
const divContent = categoryDiv?.nextElementSibling as HTMLElement;
|
||||||
divContent.style.maxHeight = `${divContent.scrollHeight}px`;
|
divContent.style.maxHeight = `${divContent.scrollHeight}px`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,6 @@
|
|||||||
</button>
|
</button>
|
||||||
</h1>
|
</h1>
|
||||||
<h2 i18n>Categories</h2>
|
<h2 i18n>Categories</h2>
|
||||||
<app-categories-menu (categoryClicked)="close()"/>
|
<app-categories-menu (categoryClicked)="close()"></app-categories-menu>
|
||||||
</div>
|
</div>
|
||||||
<div class="overlay {{ isOpened() ? 'displayed' : ''}}" (click)="close()"></div>
|
<div class="overlay {{ isOpened() ? 'displayed' : ''}}" (click)="close()"></div>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import {inject, Injectable, Signal, signal} from '@angular/core';
|
import {inject, Injectable} from '@angular/core';
|
||||||
import {MatSnackBar} from '@angular/material/snack-bar';
|
import {MatSnackBar} from '@angular/material/snack-bar';
|
||||||
|
import {BehaviorSubject, Observable} from 'rxjs';
|
||||||
import {CategoryRestService} from '../../core/rest-services/category/category.rest-service';
|
import {CategoryRestService} from '../../core/rest-services/category/category.rest-service';
|
||||||
import {Category} from '../../core/rest-services/category/model/category';
|
import {Category} from '../../core/rest-services/category/model/category';
|
||||||
|
|
||||||
@@ -19,60 +20,68 @@ export interface DisplayableSubCategory {
|
|||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class SideMenuService {
|
export class SideMenuService {
|
||||||
readonly #categoryRestService = inject(CategoryRestService);
|
private categoryRestService = inject(CategoryRestService);
|
||||||
readonly #snackBar = inject(MatSnackBar);
|
private snackBar = inject(MatSnackBar);
|
||||||
#categories = signal<DisplayableCategory[]>([]);
|
private categoriesSubject = new BehaviorSubject<DisplayableCategory[]>([]);
|
||||||
#isLoading = signal(false);
|
private isLoadingSubject = new BehaviorSubject<boolean>(false);
|
||||||
isLoaded = signal(false);
|
private isLoadedSubject = new BehaviorSubject<boolean>(false);
|
||||||
|
|
||||||
#mapToDisplayableCategory(category: Category): DisplayableCategory {
|
private mapToDisplayableCategory(category: Category): DisplayableCategory {
|
||||||
return {
|
return {
|
||||||
id: category.id,
|
id: category.id,
|
||||||
name: category.name,
|
name: category.name,
|
||||||
subCategories: category.subCategories.map(subCategory => this.#mapToDisplayableSubCategory(subCategory)),
|
subCategories: category.subCategories.map(subCategory => this.mapToDisplayableSubCategory(subCategory)),
|
||||||
isOpenned: false
|
isOpenned: false
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#mapToDisplayableSubCategory(subCategory: Category): DisplayableSubCategory {
|
private mapToDisplayableSubCategory(subCategory: Category): DisplayableSubCategory {
|
||||||
return {
|
return {
|
||||||
id: subCategory.id,
|
id: subCategory.id,
|
||||||
name: subCategory.name
|
name: subCategory.name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get categories(): Signal<DisplayableCategory[]> {
|
private get categories(): DisplayableCategory[] {
|
||||||
return this.#categories.asReadonly();
|
return this.categoriesSubject.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
get isLoading(): Signal<boolean> {
|
private save(categories: DisplayableCategory[]): void {
|
||||||
return this.#isLoading.asReadonly();
|
this.categoriesSubject.next(categories);
|
||||||
|
}
|
||||||
|
|
||||||
|
get categories$(): Observable<DisplayableCategory[]> {
|
||||||
|
return this.categoriesSubject.asObservable();
|
||||||
|
}
|
||||||
|
|
||||||
|
get isLoading$(): Observable<boolean> {
|
||||||
|
return this.isLoadingSubject.asObservable();
|
||||||
}
|
}
|
||||||
|
|
||||||
loadCategories(): void {
|
loadCategories(): void {
|
||||||
this.#isLoading.set(true);
|
this.isLoadingSubject.next(true);
|
||||||
this.isLoaded.set(false);
|
this.isLoadedSubject.next(false);
|
||||||
|
|
||||||
this.#categoryRestService.getCategories()
|
this.categoryRestService.getCategories()
|
||||||
.then(categories => {
|
.then(categories => {
|
||||||
const displayableCategories = categories
|
const displayableCategories = categories
|
||||||
.filter(category => category.subCategories?.length)
|
.filter(category => category.subCategories?.length)
|
||||||
.map(category => this.#mapToDisplayableCategory(category));
|
.map(category => this.mapToDisplayableCategory(category));
|
||||||
this.#categories.set(displayableCategories);
|
this.categoriesSubject.next(displayableCategories);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
const errorMessage = $localize`An error occured while loading categories.`;
|
const errorMessage = $localize`An error occured while loading categories.`;
|
||||||
console.error(errorMessage, error);
|
console.error(errorMessage, error);
|
||||||
this.#snackBar.open(errorMessage, $localize`Close`, {duration: 5000});
|
this.snackBar.open(errorMessage, $localize`Close`, {duration: 5000});
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
this.#isLoading.set(false);
|
this.isLoadingSubject.next(false);
|
||||||
this.isLoaded.set(true);
|
this.isLoadedSubject.next(true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
setOpened(category: DisplayableCategory): void {
|
setOpenned(category: DisplayableCategory): void {
|
||||||
const categories = this.#categories();
|
const categories = this.categories;
|
||||||
const matchingCategory = categories.find(categoryTemp => categoryTemp.id === category.id);
|
const matchingCategory = categories.find(categoryTemp => categoryTemp.id === category.id);
|
||||||
if (matchingCategory) {
|
if (matchingCategory) {
|
||||||
const actualOpennedCategory = categories.find(category => category.isOpenned);
|
const actualOpennedCategory = categories.find(category => category.isOpenned);
|
||||||
@@ -82,7 +91,7 @@ export class SideMenuService {
|
|||||||
categories.forEach(categoryTemp => categoryTemp.isOpenned = false);
|
categories.forEach(categoryTemp => categoryTemp.isOpenned = false);
|
||||||
matchingCategory.isOpenned = true;
|
matchingCategory.isOpenned = true;
|
||||||
}
|
}
|
||||||
this.#categories.set(categories);
|
this.save(categories);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user