|
|
@ -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.` |
|
|
}); |
|
|
}); |
|
|
|