|
|
|
@ -1,4 +1,5 @@ |
|
|
|
import { CreateAccessDto } from '@ghostfolio/api/app/access/create-access.dto'; |
|
|
|
import { UpdateAccessDto } from '@ghostfolio/api/app/access/update-access.dto'; |
|
|
|
import { NotificationService } from '@ghostfolio/client/core/notification/notification.service'; |
|
|
|
import { DataService } from '@ghostfolio/client/services/data.service'; |
|
|
|
import { validateObjectForForm } from '@ghostfolio/client/util/form.util'; |
|
|
|
@ -8,7 +9,8 @@ import { |
|
|
|
ChangeDetectorRef, |
|
|
|
Component, |
|
|
|
Inject, |
|
|
|
OnDestroy |
|
|
|
OnDestroy, |
|
|
|
OnInit |
|
|
|
} from '@angular/core'; |
|
|
|
import { |
|
|
|
FormBuilder, |
|
|
|
@ -47,8 +49,9 @@ import { CreateOrUpdateAccessDialogParams } from './interfaces/interfaces'; |
|
|
|
styleUrls: ['./create-or-update-access-dialog.scss'], |
|
|
|
templateUrl: 'create-or-update-access-dialog.html' |
|
|
|
}) |
|
|
|
export class GfCreateOrUpdateAccessDialog implements OnDestroy { |
|
|
|
export class GfCreateOrUpdateAccessDialog implements OnInit, OnDestroy { |
|
|
|
public accessForm: FormGroup; |
|
|
|
public isEditMode: boolean; |
|
|
|
|
|
|
|
private unsubscribeSubject = new Subject<void>(); |
|
|
|
|
|
|
|
@ -59,9 +62,14 @@ export class GfCreateOrUpdateAccessDialog implements OnDestroy { |
|
|
|
private dataService: DataService, |
|
|
|
private formBuilder: FormBuilder, |
|
|
|
private notificationService: NotificationService |
|
|
|
) {} |
|
|
|
) { |
|
|
|
this.isEditMode = !!data.accessId; |
|
|
|
} |
|
|
|
|
|
|
|
public ngOnInit() { |
|
|
|
console.log('Dialog init - Edit mode:', this.isEditMode); |
|
|
|
console.log('Dialog data:', this.data); |
|
|
|
|
|
|
|
this.accessForm = this.formBuilder.group({ |
|
|
|
alias: [this.data.access.alias], |
|
|
|
permissions: [this.data.access.permissions[0], Validators.required], |
|
|
|
@ -77,6 +85,7 @@ export class GfCreateOrUpdateAccessDialog implements OnDestroy { |
|
|
|
granteeUserIdControl.setValidators(Validators.required); |
|
|
|
} else { |
|
|
|
granteeUserIdControl.clearValidators(); |
|
|
|
granteeUserIdControl.setValue(null); |
|
|
|
permissionsControl.setValue(this.data.access.permissions[0]); |
|
|
|
} |
|
|
|
|
|
|
|
@ -84,6 +93,14 @@ export class GfCreateOrUpdateAccessDialog implements OnDestroy { |
|
|
|
|
|
|
|
this.changeDetectorRef.markForCheck(); |
|
|
|
}); |
|
|
|
|
|
|
|
// Initial validation setup based on current type
|
|
|
|
if (this.accessForm.get('type').value === 'PUBLIC') { |
|
|
|
const granteeUserIdControl = this.accessForm.get('granteeUserId'); |
|
|
|
granteeUserIdControl.clearValidators(); |
|
|
|
granteeUserIdControl.setValue(null); |
|
|
|
granteeUserIdControl.updateValueAndValidity(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public onCancel() { |
|
|
|
@ -91,12 +108,28 @@ export class GfCreateOrUpdateAccessDialog implements OnDestroy { |
|
|
|
} |
|
|
|
|
|
|
|
public async onSubmit() { |
|
|
|
if (!this.accessForm.valid) { |
|
|
|
console.error('Form is invalid:', this.accessForm.errors); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (this.isEditMode) { |
|
|
|
await this.updateAccess(); |
|
|
|
} else { |
|
|
|
await this.createAccess(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private async createAccess() { |
|
|
|
console.log('Creating access...'); |
|
|
|
const access: CreateAccessDto = { |
|
|
|
alias: this.accessForm.get('alias').value, |
|
|
|
granteeUserId: this.accessForm.get('granteeUserId').value, |
|
|
|
permissions: [this.accessForm.get('permissions').value] |
|
|
|
}; |
|
|
|
|
|
|
|
console.log('Access data:', access); |
|
|
|
|
|
|
|
try { |
|
|
|
await validateObjectForForm({ |
|
|
|
classDto: CreateAccessDto, |
|
|
|
@ -126,6 +159,46 @@ export class GfCreateOrUpdateAccessDialog implements OnDestroy { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private async updateAccess() { |
|
|
|
console.log('Updating access...'); |
|
|
|
const access: UpdateAccessDto = { |
|
|
|
alias: this.accessForm.get('alias').value, |
|
|
|
granteeUserId: this.accessForm.get('granteeUserId').value, |
|
|
|
permissions: [this.accessForm.get('permissions').value] |
|
|
|
}; |
|
|
|
|
|
|
|
console.log('Access data:', access); |
|
|
|
console.log('Access ID:', this.data.accessId); |
|
|
|
|
|
|
|
try { |
|
|
|
await validateObjectForForm({ |
|
|
|
classDto: UpdateAccessDto, |
|
|
|
form: this.accessForm, |
|
|
|
object: access |
|
|
|
}); |
|
|
|
|
|
|
|
this.dataService |
|
|
|
.putAccess(this.data.accessId, access) |
|
|
|
.pipe( |
|
|
|
catchError((error) => { |
|
|
|
if (error.status === StatusCodes.BAD_REQUEST) { |
|
|
|
this.notificationService.alert({ |
|
|
|
title: $localize`Oops! Could not update access.` |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
return EMPTY; |
|
|
|
}), |
|
|
|
takeUntil(this.unsubscribeSubject) |
|
|
|
) |
|
|
|
.subscribe(() => { |
|
|
|
this.dialogRef.close(access); |
|
|
|
}); |
|
|
|
} catch (error) { |
|
|
|
console.error(error); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public ngOnDestroy() { |
|
|
|
this.unsubscribeSubject.next(); |
|
|
|
this.unsubscribeSubject.complete(); |
|
|
|
|