Skip to content

Commit

Permalink
refactor: separate EntityTypeSelectComponent from its EditComponent (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayush8923 authored Dec 17, 2024
1 parent 9812d5c commit f453c2d
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 71 deletions.
8 changes: 4 additions & 4 deletions src/app/core/core-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ export const coreComponents: ComponentTuple[] = [
).then((c) => c.RelatedEntitiesWithSummaryComponent),
],
[
"EditEntityTypeDropdown",
"EditEntityType",
() =>
import(
"./entity/edit-entity-type-dropdown/edit-entity-type-dropdown.component"
).then((c) => c.EditEntityTypeDropdownComponent),
import("./entity/edit-entity-type/edit-entity-type.component").then(
(c) => c.EditEntityTypeComponent,
),
],
];

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<app-entity-type-select
[formControl]="formControl"
[allowMultiSelect]="multi"
[label]="label"
></app-entity-type-select>
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { ComponentFixture, TestBed } from "@angular/core/testing";

import { EditEntityTypeDropdownComponent } from "./edit-entity-type-dropdown.component";
import { EditEntityTypeComponent } from "./edit-entity-type.component";
import { MockedTestingModule } from "../../../utils/mocked-testing.module";
import { setupEditComponent } from "../default-datatype/edit-component.spec";

describe("EditConfigurableEnumComponent", () => {
let component: EditEntityTypeDropdownComponent;
let fixture: ComponentFixture<EditEntityTypeDropdownComponent>;
let component: EditEntityTypeComponent;
let fixture: ComponentFixture<EditEntityTypeComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
EditEntityTypeDropdownComponent,
MockedTestingModule.withState(),
],
imports: [EditEntityTypeComponent, MockedTestingModule.withState()],
}).compileComponents();

fixture = TestBed.createComponent(EditEntityTypeDropdownComponent);
fixture = TestBed.createComponent(EditEntityTypeComponent);
component = fixture.componentInstance;
setupEditComponent(component);
fixture.detectChanges();
Expand Down
26 changes: 26 additions & 0 deletions src/app/core/entity/edit-entity-type/edit-entity-type.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Component, OnInit } from "@angular/core";
import { EditComponent } from "../default-datatype/edit-component";
import { DynamicComponent } from "../../config/dynamic-components/dynamic-component.decorator";
import { EntityTypeSelectComponent } from "../entity-type-select/entity-type-select.component";

/**
* Edit component for selecting an entity type from a dropdown.
*/
@DynamicComponent("EditEntityType")
@Component({
selector: "app-edit-entity-type",
templateUrl: "./edit-entity-type.component.html",
imports: [EntityTypeSelectComponent],
standalone: true,
})
export class EditEntityTypeComponent
extends EditComponent<string | string[]>
implements OnInit
{
multi = false;

override ngOnInit() {
super.ngOnInit();
this.multi = this.formFieldConfig.isArray;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<mat-form-field>
<mat-label>{{ label }}</mat-label>

<app-basic-autocomplete
[formControl]="formControl"
[options]="entityTypes"
[optionToString]="optionToLabel"
[valueMapper]="optionToId"
[multi]="multi"
placeholder="Select an entity type"
[multi]="allowMultiSelect"
i18n-placeholder="placeholder for Entity Type dropdown input"
></app-basic-autocomplete>
</mat-form-field>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ComponentFixture, TestBed } from "@angular/core/testing";
import { EntityTypeSelectComponent } from "./entity-type-select.component";
import { MockedTestingModule } from "../../../utils/mocked-testing.module";
import { FormControl, ReactiveFormsModule } from "@angular/forms";

describe("EntityTypeSelectComponent", () => {
let component: EntityTypeSelectComponent;
let fixture: ComponentFixture<EntityTypeSelectComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
EntityTypeSelectComponent,
MockedTestingModule.withState(),
ReactiveFormsModule,
],
}).compileComponents();

fixture = TestBed.createComponent(EntityTypeSelectComponent);
component = fixture.componentInstance;
component.formControl = new FormControl();
fixture.detectChanges();
});

it("should create", () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Component, OnInit, Input } from "@angular/core";
import { BasicAutocompleteComponent } from "../../common-components/basic-autocomplete/basic-autocomplete.component";
import { FormControl, ReactiveFormsModule } from "@angular/forms";
import { EntityConstructor } from "../model/entity";
import { EntityRegistry } from "../database-entity.decorator";
import { MatFormField, MatLabel } from "@angular/material/form-field";

@Component({
selector: "app-entity-type-select",
templateUrl: "./entity-type-select.component.html",
imports: [
BasicAutocompleteComponent,
ReactiveFormsModule,
MatFormField,
MatLabel,
],
standalone: true,
})
export class EntityTypeSelectComponent implements OnInit {
@Input() formControl: FormControl;
@Input() allowMultiSelect = false;
@Input() label: string;

entityTypes: EntityConstructor[];
optionToLabel = (option: EntityConstructor) => option.label;
optionToId = (option: EntityConstructor) => option.ENTITY_TYPE;

constructor(private entityRegistry: EntityRegistry) {}

ngOnInit() {
this.entityTypes = this.entityRegistry
.getEntityTypes(true)
.map(({ value }) => value);
}
}
2 changes: 1 addition & 1 deletion src/app/features/public-form/public-form-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class PublicFormConfig extends Entity {
@DatabaseField({
label: $localize`:PublicFormConfig:Entity`,
description: $localize`:PublicFormConfig:The type of record that is created when a someone submits the form (e.g. if you select "Note" here, the form will create new entries in your "Notes List" and you can select only fields of your "Note" data structure for this form)`,
editComponent: "EditEntityTypeDropdown",
editComponent: "EditEntityType",
validators: {
required: true,
},
Expand Down
2 changes: 1 addition & 1 deletion src/app/features/template-export/template-export.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class TemplateExport extends Entity {
@DatabaseField({
label: $localize`:TemplateExport:Applicable Entity Types`,
labelShort: $localize`:TemplateExport:Entity Types`,
editComponent: "EditEntityTypeDropdown",
editComponent: "EditEntityType",
isArray: true,
})
applicableForEntityTypes: string[];
Expand Down

0 comments on commit f453c2d

Please sign in to comment.