Browse Source

Task/improve type safety in create or update access dialog (#6685)

Improve type safety
pull/6690/head
Kenrick Tandrian 1 week ago
committed by GitHub
parent
commit
c3ca05bbdd
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 80
      apps/client/src/app/components/user-account-access/create-or-update-access-dialog/create-or-update-access-dialog.component.ts
  2. 4
      apps/client/src/app/components/user-account-access/create-or-update-access-dialog/create-or-update-access-dialog.html

80
apps/client/src/app/components/user-account-access/create-or-update-access-dialog/create-or-update-access-dialog.component.ts

@ -3,12 +3,13 @@ import { validateObjectForForm } from '@ghostfolio/common/utils';
import { NotificationService } from '@ghostfolio/ui/notifications'; import { NotificationService } from '@ghostfolio/ui/notifications';
import { DataService } from '@ghostfolio/ui/services'; import { DataService } from '@ghostfolio/ui/services';
import type { HttpErrorResponse } from '@angular/common/http';
import { import {
ChangeDetectionStrategy, ChangeDetectionStrategy,
ChangeDetectorRef, ChangeDetectorRef,
Component, Component,
DestroyRef, DestroyRef,
Inject, inject,
OnInit OnInit
} from '@angular/core'; } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
@ -50,18 +51,24 @@ import { CreateOrUpdateAccessDialogParams } from './interfaces/interfaces';
templateUrl: 'create-or-update-access-dialog.html' templateUrl: 'create-or-update-access-dialog.html'
}) })
export class GfCreateOrUpdateAccessDialogComponent implements OnInit { export class GfCreateOrUpdateAccessDialogComponent implements OnInit {
public accessForm: FormGroup; protected accessForm: FormGroup;
public mode: 'create' | 'update'; protected mode: 'create' | 'update';
public constructor( private readonly changeDetectorRef = inject(ChangeDetectorRef);
private changeDetectorRef: ChangeDetectorRef,
@Inject(MAT_DIALOG_DATA) private data: CreateOrUpdateAccessDialogParams, private readonly data =
public dialogRef: MatDialogRef<GfCreateOrUpdateAccessDialogComponent>, inject<CreateOrUpdateAccessDialogParams>(MAT_DIALOG_DATA);
private dataService: DataService,
private destroyRef: DestroyRef, private readonly dataService = inject(DataService);
private formBuilder: FormBuilder, private readonly destroyRef = inject(DestroyRef);
private notificationService: NotificationService
) { private readonly dialogRef =
inject<MatDialogRef<GfCreateOrUpdateAccessDialogComponent>>(MatDialogRef);
private readonly formBuilder = inject(FormBuilder);
private readonly notificationService = inject(NotificationService);
public constructor() {
this.mode = this.data.access?.id ? 'update' : 'create'; this.mode = this.data.access?.id ? 'update' : 'create';
} }
@ -81,22 +88,25 @@ export class GfCreateOrUpdateAccessDialogComponent implements OnInit {
] ]
}); });
this.accessForm.get('type').valueChanges.subscribe((accessType) => { this.accessForm
const granteeUserIdControl = this.accessForm.get('granteeUserId'); .get('type')
const permissionsControl = this.accessForm.get('permissions'); ?.valueChanges.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((accessType) => {
const granteeUserIdControl = this.accessForm.get('granteeUserId');
const permissionsControl = this.accessForm.get('permissions');
if (accessType === 'PRIVATE') { if (accessType === 'PRIVATE') {
granteeUserIdControl.setValidators(Validators.required); granteeUserIdControl?.setValidators(Validators.required);
} else { } else {
granteeUserIdControl.clearValidators(); granteeUserIdControl?.clearValidators();
granteeUserIdControl.setValue(null); granteeUserIdControl?.setValue(null);
permissionsControl.setValue(this.data.access.permissions[0]); permissionsControl?.setValue(this.data.access.permissions[0]);
} }
granteeUserIdControl.updateValueAndValidity(); granteeUserIdControl?.updateValueAndValidity();
this.changeDetectorRef.markForCheck(); this.changeDetectorRef.markForCheck();
}); });
} }
public onCancel() { public onCancel() {
@ -113,9 +123,9 @@ export class GfCreateOrUpdateAccessDialogComponent implements OnInit {
private async createAccess() { private async createAccess() {
const access: CreateAccessDto = { const access: CreateAccessDto = {
alias: this.accessForm.get('alias').value, alias: this.accessForm.get('alias')?.value,
granteeUserId: this.accessForm.get('granteeUserId').value, granteeUserId: this.accessForm.get('granteeUserId')?.value,
permissions: [this.accessForm.get('permissions').value] permissions: [this.accessForm.get('permissions')?.value]
}; };
try { try {
@ -128,7 +138,7 @@ export class GfCreateOrUpdateAccessDialogComponent implements OnInit {
this.dataService this.dataService
.postAccess(access) .postAccess(access)
.pipe( .pipe(
catchError((error) => { catchError((error: HttpErrorResponse) => {
if (error.status === StatusCodes.BAD_REQUEST) { if (error.status === StatusCodes.BAD_REQUEST) {
this.notificationService.alert({ this.notificationService.alert({
title: $localize`Oops! Could not grant access.` title: $localize`Oops! Could not grant access.`
@ -149,10 +159,10 @@ export class GfCreateOrUpdateAccessDialogComponent implements OnInit {
private async updateAccess() { private async updateAccess() {
const access: UpdateAccessDto = { const access: UpdateAccessDto = {
alias: this.accessForm.get('alias').value, alias: this.accessForm.get('alias')?.value,
granteeUserId: this.accessForm.get('granteeUserId').value, granteeUserId: this.accessForm.get('granteeUserId')?.value,
id: this.data.access.id, id: this.data.access.id,
permissions: [this.accessForm.get('permissions').value] permissions: [this.accessForm.get('permissions')?.value]
}; };
try { try {
@ -165,8 +175,8 @@ export class GfCreateOrUpdateAccessDialogComponent implements OnInit {
this.dataService this.dataService
.putAccess(access) .putAccess(access)
.pipe( .pipe(
catchError(({ status }) => { catchError(({ status }: HttpErrorResponse) => {
if (status.status === StatusCodes.BAD_REQUEST) { if (status === StatusCodes.BAD_REQUEST) {
this.notificationService.alert({ this.notificationService.alert({
title: $localize`Oops! Could not update access.` title: $localize`Oops! Could not update access.`
}); });

4
apps/client/src/app/components/user-account-access/create-or-update-access-dialog/create-or-update-access-dialog.html

@ -38,13 +38,13 @@
<mat-label i18n>Permission</mat-label> <mat-label i18n>Permission</mat-label>
<mat-select formControlName="permissions"> <mat-select formControlName="permissions">
<mat-option i18n value="READ_RESTRICTED">Restricted view</mat-option> <mat-option i18n value="READ_RESTRICTED">Restricted view</mat-option>
@if (accessForm.get('type').value === 'PRIVATE') { @if (accessForm.get('type')?.value === 'PRIVATE') {
<mat-option i18n value="READ">View</mat-option> <mat-option i18n value="READ">View</mat-option>
} }
</mat-select> </mat-select>
</mat-form-field> </mat-form-field>
</div> </div>
@if (accessForm.get('type').value === 'PRIVATE') { @if (accessForm.get('type')?.value === 'PRIVATE') {
<div> <div>
<mat-form-field appearance="outline" class="w-100"> <mat-form-field appearance="outline" class="w-100">
<mat-label> <mat-label>

Loading…
Cancel
Save