mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-10 12:53:03 -05:00
feat(core): add destroyService
This commit is contained in:
68
apps/permissions/src/app/has-role.directive.ts
Normal file
68
apps/permissions/src/app/has-role.directive.ts
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
/* eslint-disable @angular-eslint/directive-selector */
|
||||||
|
import { NgIfContext } from '@angular/common';
|
||||||
|
import {
|
||||||
|
Directive,
|
||||||
|
Input,
|
||||||
|
OnDestroy,
|
||||||
|
OnInit,
|
||||||
|
TemplateRef,
|
||||||
|
ViewContainerRef,
|
||||||
|
} from '@angular/core';
|
||||||
|
import { Role } from './user.model';
|
||||||
|
import { UserStore } from './user.store';
|
||||||
|
|
||||||
|
@Directive({
|
||||||
|
selector: '[hasRole], [hasRoleIsAdmin]',
|
||||||
|
standalone: true,
|
||||||
|
providers: [provideDestroyService()],
|
||||||
|
})
|
||||||
|
export class HasRoleDirective implements OnInit, OnDestroy {
|
||||||
|
@Input('hasRole') role: Role | Role[] | undefined = undefined;
|
||||||
|
|
||||||
|
@Input('hasRoleIsAdmin') isAdmin = false;
|
||||||
|
|
||||||
|
@Input('hasRoleIsAdminElseTemplate')
|
||||||
|
elseTemplate?: TemplateRef<NgIfContext> | null;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private templateRef: TemplateRef<unknown>,
|
||||||
|
private viewContainer: ViewContainerRef,
|
||||||
|
private store: UserStore
|
||||||
|
) {}
|
||||||
|
|
||||||
|
ngOnDestroy(): void {
|
||||||
|
console.log('on destroy');
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnInit(): void {
|
||||||
|
console.log(this.role, this.isAdmin, this.elseTemplate);
|
||||||
|
if (this.isAdmin) {
|
||||||
|
this.store.isAdmin$.subscribe((isAdmin) => {
|
||||||
|
console.log(isAdmin);
|
||||||
|
isAdmin ? this.addTemplate() : this.addElseTemplate();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// else if (this.role) {
|
||||||
|
// this.store
|
||||||
|
// .hasAnyRole(this.role)
|
||||||
|
// .subscribe((hasPermission) =>
|
||||||
|
// hasPermission ? this.addTemplate() : this.addElseTemplate()
|
||||||
|
// );
|
||||||
|
// } else {
|
||||||
|
// this.addTemplate();
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
private addTemplate() {
|
||||||
|
console.log('Add');
|
||||||
|
this.viewContainer.clear();
|
||||||
|
this.viewContainer.createEmbeddedView(this.templateRef);
|
||||||
|
}
|
||||||
|
|
||||||
|
private addElseTemplate() {
|
||||||
|
console.log('ici');
|
||||||
|
this.viewContainer.clear();
|
||||||
|
this.elseTemplate &&
|
||||||
|
this.viewContainer.createEmbeddedView(this.elseTemplate);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1 +1,2 @@
|
|||||||
|
export * from './lib/destroy-service.service';
|
||||||
export * from './lib/random-http-error.utils';
|
export * from './lib/random-http-error.utils';
|
||||||
|
|||||||
39
libs/shared/utils/src/lib/destroy-service.service.ts
Normal file
39
libs/shared/utils/src/lib/destroy-service.service.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import { ClassProvider, inject, Injectable, OnDestroy } from '@angular/core';
|
||||||
|
import { Observable, Subject } from 'rxjs';
|
||||||
|
|
||||||
|
function describeDestroyService() {
|
||||||
|
@Injectable()
|
||||||
|
class DestroyService extends Subject<void> implements OnDestroy {
|
||||||
|
ngOnDestroy(): void {
|
||||||
|
this.next();
|
||||||
|
this.complete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function provideDestroyService(): ClassProvider {
|
||||||
|
return {
|
||||||
|
provide: DestroyService,
|
||||||
|
useClass: DestroyService,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function injectDestroyService(): Observable<void> {
|
||||||
|
const destroy$ = inject(DestroyService, { self: true, optional: true });
|
||||||
|
|
||||||
|
if (!destroy$) {
|
||||||
|
throw new Error(
|
||||||
|
'It seems that you forgot to provide DestroyService. Add "provideDestroyService()" to your declarable\'s providers.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return destroy$.asObservable();
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
provideDestroyService,
|
||||||
|
injectDestroyService,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const { provideDestroyService, injectDestroyService } =
|
||||||
|
describeDestroyService();
|
||||||
Reference in New Issue
Block a user