130 lines
3.2 KiB
TypeScript
130 lines
3.2 KiB
TypeScript
import {Component} from '@angular/core';
|
|
import {MenuItem} from 'primeng/api';
|
|
import {MenubarModule} from 'primeng/menubar';
|
|
import {ToastModule} from 'primeng/toast';
|
|
import {Author} from '../../models/author';
|
|
import {AuthService} from '../../auth.service';
|
|
import {Role} from '../../models/role'
|
|
|
|
@Component({
|
|
selector: 'app-header',
|
|
standalone: true,
|
|
imports: [
|
|
MenubarModule,
|
|
ToastModule
|
|
],
|
|
templateUrl: './header.component.html',
|
|
styleUrls: ['./header.component.css']
|
|
})
|
|
export class HeaderComponent {
|
|
actualAuthor: Author | undefined;
|
|
items: MenuItem[] = [];
|
|
|
|
constructor(private authService: AuthService) {
|
|
this.initializeMenu();
|
|
}
|
|
|
|
private initializeMenu(): void {
|
|
const authenticatedAuthor = this.authService.getAuthenticatedAuthor();
|
|
if (!(this.authService.isSessionExpired()) && this.authService.isAuthenticated() && authenticatedAuthor) {
|
|
this.actualAuthor = authenticatedAuthor;
|
|
}
|
|
|
|
if (this.actualAuthor) {
|
|
this.items = this.getMenuForAuthor(this.actualAuthor);
|
|
} else {
|
|
this.items = this.getDefaultMenu();
|
|
}
|
|
}
|
|
|
|
private getMenuForAuthor(author: Author): MenuItem[] {
|
|
const commonItems: MenuItem[] = [
|
|
{
|
|
label: 'Accueil',
|
|
icon: 'pi pi-home',
|
|
routerLink: '/'
|
|
}
|
|
];
|
|
|
|
if (author.role === Role.WRITER) {
|
|
return [
|
|
...commonItems,
|
|
this.getWriterMenu(),
|
|
this.getUserMenu(author)
|
|
];
|
|
} else if (author.role === Role.ADMIN) {
|
|
return [
|
|
...commonItems,
|
|
this.getAdminMenu(),
|
|
this.getUserMenu(author)
|
|
];
|
|
} else if (author.role === Role.READER) {
|
|
return [
|
|
...commonItems,
|
|
this.getUserMenu(author)
|
|
];
|
|
}
|
|
|
|
return commonItems;
|
|
}
|
|
|
|
private getWriterMenu(): MenuItem {
|
|
return {
|
|
label: 'Mes posts',
|
|
icon: 'pi pi-file-edit',
|
|
items: [
|
|
{ label: 'Ajouter un post', icon: 'pi pi-plus', routerLink: '/new-post' },
|
|
{ label: 'Voir mes posts', icon: 'pi pi-eye', routerLink: '/my-posts' }
|
|
]
|
|
};
|
|
}
|
|
|
|
private getAdminMenu(): MenuItem {
|
|
return {
|
|
label: 'Administration',
|
|
icon: 'pi pi-cog',
|
|
items: [
|
|
{
|
|
label: 'Gérer les posts',
|
|
icon: 'pi pi-file',
|
|
routerLink: '/admin/posts'
|
|
},
|
|
{
|
|
label: 'Gérer les auteurs',
|
|
icon: 'pi pi-users',
|
|
routerLink: '/admin/authors'
|
|
}
|
|
]
|
|
};
|
|
}
|
|
|
|
private getUserMenu(author: Author): MenuItem {
|
|
return {
|
|
label: 'MOI',
|
|
icon: 'pi pi-user',
|
|
items: [
|
|
{ label: 'Voir mon profil', icon: 'pi pi-user', routerLink: [`/profile`, `${author.id}`] },
|
|
{ label: 'Se déconnecter', icon: 'pi pi-sign-out', routerLink: '/logout' }
|
|
]
|
|
};
|
|
}
|
|
|
|
private getDefaultMenu(): MenuItem[] {
|
|
return [
|
|
{
|
|
label: 'Accueil',
|
|
icon: 'pi pi-home',
|
|
routerLink: '/'
|
|
},
|
|
{
|
|
label: "S'authentifier",
|
|
icon: 'pi pi-sign-in',
|
|
items: [
|
|
{ label: 'Se connecter', icon: 'pi pi-sign-in', routerLink: '/login' },
|
|
{ label: "S'inscrire", icon: 'pi pi-user-plus', routerLink: '/register' }
|
|
]
|
|
}
|
|
];
|
|
}
|
|
}
|