Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: edit single entity correctly works in forms #1144

Merged
merged 4 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,21 @@ const cols = [
label: "Assigned school(s)",
},
],
["school"],
];

Child.schema.set("has_rationCard", {
dataType: "configurable-enum",
innerDataType: "document-status",
});
Child.schema.set("assignedTo", { dataType: "array", innerDataType: "string" });
Child.schema.set("school", {
dataType: "string",
label: "Assigned School",
additional: School.ENTITY_TYPE,
viewComponent: "DisplayEntity",
editComponent: "EditSingleEntity",
});

const Template: Story<EntityFormComponent> = (args: EntityFormComponent) => ({
component: EntityFormComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<input
#inputElement
matInput
[fxHide]="!formControl.enabled"
[placeholder]="placeholder"
[formControl]="entityNameFormControl"
[matAutocomplete]="autoSuggestions"
Expand All @@ -28,13 +27,12 @@
</mat-error>
</mat-form-field>

<div *ngIf="selectedEntity && !editingSelectedEntity" style="margin-top: 7px">
<div *ngIf="selectedEntity && !editingSelectedEntity" class="wrapper-readonly">
<app-display-entity
class="block-wrapper"
[entityToDisplay]="selectedEntity"
[linkDisabled]="true"
></app-display-entity>
<button mat-icon-button (click)="editSelectedEntity()">
<button *ngIf="formControl.enabled" mat-icon-button (click)="editSelectedEntity()">
<fa-icon icon="pen"></fa-icon>
</button>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
.block-wrapper {
@include entity-block-border;
}

.wrapper-readonly {
margin-top: 7px;
margin-bottom: 16px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { School } from "../../../../../child-dev-project/schools/model/school";
import { EntityUtilsModule } from "../../entity-utils.module";
import { Child } from "../../../../../child-dev-project/children/model/child";
import { TypedFormControl } from "../edit-component";
import { ChangeDetectorRef } from "@angular/core";
import { FontAwesomeTestingModule } from "@fortawesome/angular-fontawesome/testing";
import { RouterTestingModule } from "@angular/router/testing";
import {
EntityRegistry,
entityRegistry,
Expand All @@ -31,7 +34,12 @@ describe("EditSingleEntityComponent", () => {
mockEntityMapper.loadType.and.resolveTo([]);

await TestBed.configureTestingModule({
imports: [EntityUtilsModule, NoopAnimationsModule],
imports: [
EntityUtilsModule,
NoopAnimationsModule,
FontAwesomeTestingModule,
RouterTestingModule,
],
declarations: [EditSingleEntityComponent],
providers: [
EntityFormService,
Expand All @@ -41,6 +49,10 @@ describe("EditSingleEntityComponent", () => {
provide: EntityRegistry,
useValue: entityRegistry,
},
{
provide: ChangeDetectorRef,
useValue: jasmine.createSpyObj(["detectChanges"]),
},
],
}).compileComponents();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Component, ElementRef, ViewChild } from "@angular/core";
import {
ChangeDetectorRef,
Component,
ElementRef,
ViewChild,
} from "@angular/core";
import { EditComponent, EditPropertyConfig } from "../edit-component";
import { Entity } from "../../../../entity/model/entity";
import { Observable } from "rxjs";
Expand All @@ -25,7 +30,10 @@ export class EditSingleEntityComponent extends EditComponent<string> {

@ViewChild("inputElement") input: ElementRef;

constructor(private entityMapperService: EntityMapperService) {
constructor(
private changeDetection: ChangeDetectorRef,
private entityMapperService: EntityMapperService
) {
super();
this.filteredEntities = this.entityNameFormControl.valueChanges.pipe(
untilDestroyed(this),
Expand All @@ -47,6 +55,7 @@ export class EditSingleEntityComponent extends EditComponent<string> {

async onInitFromDynamicConfig(config: EditPropertyConfig) {
super.onInitFromDynamicConfig(config);
this.connectFormControlDisabledStatus();
this.placeholder = $localize`:Placeholder for input to set an entity|context Select User:Select ${this.label}`;
const entityType: string =
config.formFieldConfig.additional || config.propertySchema.additional;
Expand All @@ -66,6 +75,20 @@ export class EditSingleEntityComponent extends EditComponent<string> {
} else {
this.entityNameFormControl.setValue("");
}
this.changeDetection.detectChanges();
}

private connectFormControlDisabledStatus() {
if (this.formControl.disabled) {
this.entityNameFormControl.disable();
}
this.formControl.registerOnDisabledChange((isDisabled) => {
if (isDisabled) {
this.entityNameFormControl.disable();
} else {
this.entityNameFormControl.enable();
}
});
}

select(entityName: string) {
Expand Down