Skip to content

Commit

Permalink
fix(link-value): re-validate form when cancel button is clicked (#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelez committed Apr 8, 2022
1 parent 5ccd18d commit 7768fb9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/app/main/dialog/dialog.component.ts
Expand Up @@ -31,6 +31,10 @@ export interface ConfirmationWithComment {
comment?: string;
}

export enum DialogEvent {
DialogCanceled
}

@Component({
selector: 'app-material-dialog',
templateUrl: './dialog.component.html',
Expand Down
Expand Up @@ -16,7 +16,7 @@
</app-select-properties>
<div class="form-panel form-action">
<span>
<button mat-button type="button" (click)="closeDialog.emit()">
<button mat-button type="button" (click)="onCancel()">
{{ 'appLabels.form.action.cancel' | translate }}
</button>
</span>
Expand Down
Expand Up @@ -14,6 +14,7 @@ import {
ResourcePropertyDefinition
} from '@dasch-swiss/dsp-js';
import { DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { DialogEvent } from 'src/app/main/dialog/dialog.component';
import { ErrorHandlerService } from 'src/app/main/error/error-handler.service';
import { SelectPropertiesComponent } from '../../resource-instance-form/select-properties/select-properties.component';

Expand All @@ -29,7 +30,7 @@ export class CreateLinkResourceComponent implements OnInit {
@Input() resourceClassDef: string;
@Input() currentOntoIri: string;

@Output() closeDialog: EventEmitter<ReadResource> = new EventEmitter<ReadResource>();
@Output() closeDialog: EventEmitter<ReadResource | DialogEvent> = new EventEmitter<ReadResource | DialogEvent>();

@ViewChild('selectProps') selectPropertiesComponent: SelectPropertiesComponent;

Expand Down Expand Up @@ -140,6 +141,11 @@ export class CreateLinkResourceComponent implements OnInit {
}
}

onCancel() {
// emit DialogCanceled event
this.closeDialog.emit(DialogEvent.DialogCanceled);
}

setFileValue(file: CreateFileValue) {
this.fileValue = file;
}
Expand Down
Expand Up @@ -27,7 +27,7 @@ import {
} from '@dasch-swiss/dsp-js';
import { Subscription } from 'rxjs';
import { DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { DialogComponent } from 'src/app/main/dialog/dialog.component';
import { DialogComponent, DialogEvent } from 'src/app/main/dialog/dialog.component';
import { BaseValueDirective } from 'src/app/main/directive/base-value.directive';

export function resourceValidator(control: AbstractControl) {
Expand Down Expand Up @@ -264,14 +264,25 @@ export class LinkValueComponent extends BaseValueDirective implements OnInit, On

const dialogRef = this._dialog.open(DialogComponent, dialogConfig);

dialogRef.afterClosed().subscribe((event: any) => {
const newResource = event as ReadResource;
dialogRef.afterClosed().subscribe((event: ReadResource | DialogEvent) => {
// save button clicked
if (event instanceof ReadResource ) {
const newResource = event as ReadResource;

// set value of value form control to the newly created resource
this.form.controls.value.setValue(newResource);
// set value of value form control to the newly created resource
this.form.controls.value.setValue(newResource);

// hide the autocomplete results
this.autocomplete.closePanel();
// hide the autocomplete results
this.autocomplete.closePanel();
}

// cancel button clicked
if (event === DialogEvent.DialogCanceled){
this.resetFormControl();

// hide the autocomplete results
this.autocomplete.closePanel();
}
});
}
}

0 comments on commit 7768fb9

Please sign in to comment.