Browse Source

Task/improve type safety for admin tag component (#6893)

* fix(client): make tag as optional to cover tag creation use case

* feat(client): migrate constructor based DI to inject function

* feat(client): implement view child signal

* feat(client): replace deprecated getDeviceInfo

* feat(client): implement input signal

* fix(client): enforce encapsulation
pull/6905/head
Kenrick Tandrian 6 days ago
committed by GitHub
parent
commit
409fcf9344
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      apps/client/src/app/components/admin-tag/admin-tag.component.html
  2. 82
      apps/client/src/app/components/admin-tag/admin-tag.component.ts
  3. 4
      apps/client/src/app/components/admin-tag/create-or-update-tag-dialog/create-or-update-tag-dialog.component.ts
  4. 2
      apps/client/src/app/components/admin-tag/create-or-update-tag-dialog/create-or-update-tag-dialog.html
  5. 2
      apps/client/src/app/components/admin-tag/create-or-update-tag-dialog/interfaces/interfaces.ts

2
apps/client/src/app/components/admin-tag/admin-tag.component.html

@ -47,7 +47,7 @@
<td *matCellDef="let element" class="px-1 text-right" mat-cell> <td *matCellDef="let element" class="px-1 text-right" mat-cell>
<gf-value <gf-value
class="d-inline-block justify-content-end" class="d-inline-block justify-content-end"
[locale]="locale" [locale]="locale()"
[value]="element.activityCount" [value]="element.activityCount"
/> />
</td> </td>

82
apps/client/src/app/components/admin-tag/admin-tag.component.ts

@ -10,10 +10,12 @@ import {
ChangeDetectionStrategy, ChangeDetectionStrategy,
ChangeDetectorRef, ChangeDetectorRef,
Component, Component,
computed,
DestroyRef, DestroyRef,
Input, inject,
input,
OnInit, OnInit,
ViewChild viewChild
} from '@angular/core'; } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { MatButtonModule } from '@angular/material/button'; import { MatButtonModule } from '@angular/material/button';
@ -52,26 +54,33 @@ import { CreateOrUpdateTagDialogParams } from './create-or-update-tag-dialog/int
templateUrl: './admin-tag.component.html' templateUrl: './admin-tag.component.html'
}) })
export class GfAdminTagComponent implements OnInit { export class GfAdminTagComponent implements OnInit {
@Input() locale = getLocale(); public readonly locale = input(getLocale());
@ViewChild(MatSort) sort: MatSort; protected dataSource = new MatTableDataSource<Tag>();
protected readonly displayedColumns = [
public dataSource = new MatTableDataSource<Tag>(); 'name',
public deviceType: string; 'userId',
public displayedColumns = ['name', 'userId', 'activities', 'actions']; 'activities',
public tags: Tag[]; 'actions'
];
public constructor( protected tags: Tag[];
private changeDetectorRef: ChangeDetectorRef,
private dataService: DataService, private readonly deviceType = computed(
private destroyRef: DestroyRef, () => this.deviceDetectorService.deviceInfo().deviceType
private deviceDetectorService: DeviceDetectorService, );
private dialog: MatDialog, private readonly sort = viewChild.required(MatSort);
private notificationService: NotificationService,
private route: ActivatedRoute, private readonly changeDetectorRef = inject(ChangeDetectorRef);
private router: Router, private readonly dataService = inject(DataService);
private userService: UserService private readonly destroyRef = inject(DestroyRef);
) { private readonly deviceDetectorService = inject(DeviceDetectorService);
private readonly dialog = inject(MatDialog);
private readonly notificationService = inject(NotificationService);
private readonly route = inject(ActivatedRoute);
private readonly router = inject(Router);
private readonly userService = inject(UserService);
public constructor() {
this.route.queryParams this.route.queryParams
.pipe(takeUntilDestroyed(this.destroyRef)) .pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((params) => { .subscribe((params) => {
@ -83,7 +92,9 @@ export class GfAdminTagComponent implements OnInit {
return id === params['tagId']; return id === params['tagId'];
}); });
this.openUpdateTagDialog(tag); if (tag) {
this.openUpdateTagDialog(tag);
}
} else { } else {
this.router.navigate(['.'], { relativeTo: this.route }); this.router.navigate(['.'], { relativeTo: this.route });
} }
@ -94,12 +105,10 @@ export class GfAdminTagComponent implements OnInit {
} }
public ngOnInit() { public ngOnInit() {
this.deviceType = this.deviceDetectorService.getDeviceInfo().deviceType;
this.fetchTags(); this.fetchTags();
} }
public onDeleteTag(aId: string) { protected onDeleteTag(aId: string) {
this.notificationService.confirm({ this.notificationService.confirm({
confirmFn: () => { confirmFn: () => {
this.deleteTag(aId); this.deleteTag(aId);
@ -109,7 +118,7 @@ export class GfAdminTagComponent implements OnInit {
}); });
} }
public onUpdateTag({ id }: Tag) { protected onUpdateTag({ id }: Tag) {
this.router.navigate([], { this.router.navigate([], {
queryParams: { editTagDialog: true, tagId: id } queryParams: { editTagDialog: true, tagId: id }
}); });
@ -139,7 +148,7 @@ export class GfAdminTagComponent implements OnInit {
this.tags = tags; this.tags = tags;
this.dataSource = new MatTableDataSource(this.tags); this.dataSource = new MatTableDataSource(this.tags);
this.dataSource.sort = this.sort; this.dataSource.sort = this.sort();
this.dataSource.sortingDataAccessor = get; this.dataSource.sortingDataAccessor = get;
this.dataService.updateInfo(); this.dataService.updateInfo();
@ -153,14 +162,9 @@ export class GfAdminTagComponent implements OnInit {
GfCreateOrUpdateTagDialogComponent, GfCreateOrUpdateTagDialogComponent,
CreateOrUpdateTagDialogParams CreateOrUpdateTagDialogParams
>(GfCreateOrUpdateTagDialogComponent, { >(GfCreateOrUpdateTagDialogComponent, {
data: { data: {} satisfies CreateOrUpdateTagDialogParams,
tag: { height: this.deviceType() === 'mobile' ? '98vh' : undefined,
id: null, width: this.deviceType() === 'mobile' ? '100vw' : '50rem'
name: null
}
},
height: this.deviceType === 'mobile' ? '98vh' : undefined,
width: this.deviceType === 'mobile' ? '100vw' : '50rem'
}); });
dialogRef dialogRef
@ -197,9 +201,9 @@ export class GfAdminTagComponent implements OnInit {
id, id,
name name
} }
}, } satisfies CreateOrUpdateTagDialogParams,
height: this.deviceType === 'mobile' ? '98vh' : undefined, height: this.deviceType() === 'mobile' ? '98vh' : undefined,
width: this.deviceType === 'mobile' ? '100vw' : '50rem' width: this.deviceType() === 'mobile' ? '100vw' : '50rem'
}); });
dialogRef dialogRef

4
apps/client/src/app/components/admin-tag/create-or-update-tag-dialog/create-or-update-tag-dialog.component.ts

@ -43,7 +43,7 @@ export class GfCreateOrUpdateTagDialogComponent {
private formBuilder: FormBuilder private formBuilder: FormBuilder
) { ) {
this.tagForm = this.formBuilder.group({ this.tagForm = this.formBuilder.group({
name: [this.data.tag.name] name: [this.data.tag?.name]
}); });
} }
@ -57,7 +57,7 @@ export class GfCreateOrUpdateTagDialogComponent {
name: this.tagForm.get('name')?.value name: this.tagForm.get('name')?.value
}; };
if (this.data.tag.id) { if (this.data.tag?.id) {
(tag as UpdateTagDto).id = this.data.tag.id; (tag as UpdateTagDto).id = this.data.tag.id;
await validateObjectForForm({ await validateObjectForForm({
classDto: UpdateTagDto, classDto: UpdateTagDto,

2
apps/client/src/app/components/admin-tag/create-or-update-tag-dialog/create-or-update-tag-dialog.html

@ -4,7 +4,7 @@
(keyup.enter)="tagForm.valid && onSubmit()" (keyup.enter)="tagForm.valid && onSubmit()"
(ngSubmit)="onSubmit()" (ngSubmit)="onSubmit()"
> >
@if (data.tag.id) { @if (data.tag?.id) {
<h1 i18n mat-dialog-title>Update tag</h1> <h1 i18n mat-dialog-title>Update tag</h1>
} @else { } @else {
<h1 i18n mat-dialog-title>Add tag</h1> <h1 i18n mat-dialog-title>Add tag</h1>

2
apps/client/src/app/components/admin-tag/create-or-update-tag-dialog/interfaces/interfaces.ts

@ -1,5 +1,5 @@
import { Tag } from '@prisma/client'; import { Tag } from '@prisma/client';
export interface CreateOrUpdateTagDialogParams { export interface CreateOrUpdateTagDialogParams {
tag: Pick<Tag, 'id' | 'name'>; tag?: Pick<Tag, 'id' | 'name'>;
} }

Loading…
Cancel
Save