RED-9571: fixed initials-avatar not updating in some cases.

This commit is contained in:
Nicoleta Panaghiu 2024-07-30 11:12:16 +03:00
parent 98ac49fbc8
commit 43bfeb4e1e
3 changed files with 55 additions and 81 deletions

View File

@ -1,14 +1,14 @@
@if (_user && _user | name: namePipeOptions; as userName) { @if (_user() && _user() | name: namePipeOptions(); as userName) {
<div class="wrapper"> <div class="wrapper">
<div <div
[className]="colorClass + ' oval ' + size + (hasBorder ? ' border' : '')" [className]="colorClass() + ' oval ' + size() + (hasBorder() ? ' border' : '')"
[matTooltipPosition]="tooltipPosition" [matTooltipPosition]="tooltipPosition()"
[matTooltip]="showTooltip ? userName : ''" [matTooltip]="showTooltip() ? userName : ''"
> >
{{ _user | name: { showInitials: true } }} {{ _user() | name: { showInitials: true } }}
</div> </div>
@if (withName) { @if (withName()) {
<div [class.disabled]="disabled" class="clamp-1 username" id="avatarUsername"> <div [class.disabled]="disabled()" class="clamp-1 username" id="avatarUsername">
{{ userName }} {{ userName }}
</div> </div>
} }

View File

@ -1,4 +1,4 @@
import { ChangeDetectionStrategy, Component, inject, Input, OnChanges, OnInit } from '@angular/core'; import { ChangeDetectionStrategy, Component, computed, inject, input } from '@angular/core';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { IqserUser } from '../../iqser-user.model'; import { IqserUser } from '../../iqser-user.model';
import { IqserUserService } from '../../services/iqser-user.service'; import { IqserUserService } from '../../services/iqser-user.service';
@ -7,6 +7,7 @@ import { IIqserUser } from '../../types/user.response';
import { NgIf } from '@angular/common'; import { NgIf } from '@angular/common';
import { NamePipe } from '../../name.pipe'; import { NamePipe } from '../../name.pipe';
import { MatTooltip } from '@angular/material/tooltip'; import { MatTooltip } from '@angular/material/tooltip';
import { toSignal } from '@angular/core/rxjs-interop';
@Component({ @Component({
selector: 'iqser-initials-avatar', selector: 'iqser-initials-avatar',
@ -16,80 +17,45 @@ import { MatTooltip } from '@angular/material/tooltip';
standalone: true, standalone: true,
imports: [NgIf, NamePipe, MatTooltip], imports: [NgIf, NamePipe, MatTooltip],
}) })
export class InitialsAvatarComponent<Interface extends IIqserUser = IIqserUser, Class extends IqserUser & Interface = IqserUser & Interface> export class InitialsAvatarComponent<
implements OnInit, OnChanges Interface extends IIqserUser = IIqserUser,
{ Class extends IqserUser & Interface = IqserUser & Interface,
> {
readonly #userService = inject<IqserUserService<Interface, Class>>(IqserUserService);
readonly #translateService = inject(TranslateService); readonly #translateService = inject(TranslateService);
@Input() color = 'lightgray'; readonly #isSystemUser = computed(() => this._user()?.id?.toLowerCase() === 'system');
@Input() size: 'small' | 'large' = 'small'; readonly #users = toSignal(this.#userService.all$);
@Input() withName = false;
@Input() showYou = false;
@Input() tooltipPosition: 'below' | 'above' = 'above';
@Input() defaultValue: string = this.#translateService.instant('initials-avatar.unassigned');
@Input() showTooltip = true;
colorClass?: string;
namePipeOptions?: NamePipeOptions;
constructor(private readonly _userService: IqserUserService<Interface, Class>) {} readonly color = input('lightgray');
readonly size = input<'small' | 'large'>('small');
readonly withName = input(false);
readonly showYou = input(false);
readonly tooltipPosition = input<'below' | 'above'>('above');
readonly defaultValue = input<string>(this.#translateService.instant('initials-avatar.unassigned'));
readonly showTooltip = input(true);
readonly user = input.required<Class | string>();
readonly showBorderCondition = input<<T extends Class = Class>(user: T) => boolean>(user => user.isSpecial);
_user?: Class; readonly _user = computed(() => {
const user = this.user();
@Input()
set user(user: Class | string) {
if (typeof user === 'string') { if (typeof user === 'string') {
this._user = this._userService.find(user); if (user?.toLowerCase() === 'system') return this.#userService.newSystemUser();
} else { if (!user) return undefined;
this._user = user; return this.#users()?.find(u => u.id === user) ?? this.#userService.newDeletedUser();
} }
} return user;
});
get hasBorder(): boolean { readonly isCurrentUser = computed(() => this.#userService.currentUser?.id === this._user()?.id);
return !!this._user && !this.isCurrentUser && this.showBorderCondition(this._user); readonly hasBorder = computed(() => !!this._user() && !this.isCurrentUser() && this.showBorderCondition()(this._user()!));
} readonly disabled = computed(() => !!this._user() && !this.#isSystemUser() && !this._user()?.hasAnyRole);
readonly colorClass = computed(() => {
get disabled(): boolean { if (this.isCurrentUser()) return 'primary-white';
return !!this._user && !this.#isSystemUser && !this._user.hasAnyRole; if (this.disabled()) return 'inactive';
} if (this.color().includes('-')) return this.color();
if (this.#isSystemUser()) return 'primary-white primary';
get isCurrentUser(): boolean { return `${this.color()}-dark`;
return this._userService.currentUser?.id === this._user?.id; });
} readonly namePipeOptions = computed<NamePipeOptions>(
() => ({ showYou: this.showYou(), defaultValue: this.defaultValue() }) as NamePipeOptions,
get #colorClass() { );
if (this.isCurrentUser) {
return 'primary-white';
}
if (this.disabled) {
return 'inactive';
}
if (this.color.includes('-')) {
return this.color;
}
return `${this.color}-dark`;
}
get #isSystemUser() {
return this._user?.id?.toLowerCase() === 'system';
}
@Input() showBorderCondition: <T extends Class = Class>(user: T) => boolean = user => user.isSpecial;
ngOnChanges(): void {
if (this.#isSystemUser) {
this.colorClass = 'primary-white primary';
return;
}
this.colorClass = this.#colorClass;
}
ngOnInit(): void {
this.namePipeOptions = {
showYou: this.showYou,
defaultValue: this.defaultValue,
};
}
} }

View File

@ -164,14 +164,22 @@ export abstract class IqserUserService<
find(id: string): Class | undefined { find(id: string): Class | undefined {
if (id?.toLowerCase() === 'system') { if (id?.toLowerCase() === 'system') {
return new this._entityClass({ username: 'System' }, [], 'system'); return this.newSystemUser();
} }
if (!id) { if (!id) {
return undefined; return undefined;
} }
return super.find(id) ?? new this._entityClass({ username: 'Deleted User' }, [], 'deleted'); return super.find(id) ?? this.newDeletedUser();
}
newSystemUser() {
return new this._entityClass({ username: 'System' }, [], 'system');
}
newDeletedUser() {
return new this._entityClass({ username: 'Deleted User' }, [], 'deleted');
} }
} }