Skip to content

Commit

Permalink
fix(Admin UI): hide special EventNote entity type from admin lists (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sleidig authored Jan 20, 2025
1 parent 364ee7c commit 393b635
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 41 deletions.
20 changes: 3 additions & 17 deletions src/app/child-dev-project/attendance/model/event-note.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
/*
* This file is part of ndb-core.
*
* ndb-core is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ndb-core is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ndb-core. If not, see <http://www.gnu.org/licenses/>.
*/

import { DatabaseEntity } from "../../../core/entity/database-entity.decorator";
import { Note } from "../../notes/model/note";

@DatabaseEntity("EventNote")
export class EventNote extends Note {
static override label = undefined; // hide the EventNote entity type from Admin UIs to avoid confusion with the Note entity type
static override labelPlural = undefined;

static override create(date: Date, subject: string = ""): EventNote {
const instance = new EventNote();
instance.date = date;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { TimePeriod } from "../../../core/entity-details/related-time-period-ent
*/
@DatabaseEntity("ChildSchoolRelation")
export class ChildSchoolRelation extends TimePeriod {
static override label = "School Enrollment";
static override labelPlural = "School Enrollments";
static override hasPII = true;

@DatabaseField({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { Validators } from "@angular/forms";
import { RecurringActivity } from "../../../../child-dev-project/attendance/model/recurring-activity";
import { AdminEntityService } from "../../admin-entity.service";
import { EntitySchemaField } from "../../../entity/schema/entity-schema-field";
import { TestEntity } from "../../../../utils/test-utils/TestEntity";

describe("AdminEntityFieldComponent", () => {
let component: AdminEntityFieldComponent;
Expand Down Expand Up @@ -85,6 +86,7 @@ describe("AdminEntityFieldComponent", () => {
it("should include 'additional' field only for relevant datatypes", fakeAsync(() => {
const dataTypeForm = component.schemaFieldsForm.get("dataType");
let additionalInput: MatInputHarness;

function findAdditionalInputComponent() {
loader
.getHarness(
Expand Down Expand Up @@ -179,7 +181,7 @@ describe("AdminEntityFieldComponent", () => {
}));

it("should init 'additional' options for entity datatypes", fakeAsync(() => {
const mockEntityTypes = [Entity, RecurringActivity];
const mockEntityTypes = [TestEntity, RecurringActivity];
const entityRegistry = TestBed.inject(EntityRegistry);
spyOn(entityRegistry, "getEntityTypes").and.returnValue(
mockEntityTypes.map((x) => ({ key: x.ENTITY_TYPE, value: x })),
Expand All @@ -192,7 +194,7 @@ describe("AdminEntityFieldComponent", () => {
tick();

expect(component.typeAdditionalOptions).toEqual([
{ value: "Entity", label: "Entity" },
{ value: TestEntity.ENTITY_TYPE, label: TestEntity.label },
{ value: RecurringActivity.ENTITY_TYPE, label: RecurringActivity.label },
]);
expect(component.additionalForm.value).toBe(RecurringActivity.ENTITY_TYPE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, inject, Input } from "@angular/core";
import { Component, inject, Input, OnInit } from "@angular/core";
import {
BASIC_AUTOCOMPLETE_COMPONENT_IMPORTS,
BasicAutocompleteComponent,
Expand All @@ -17,14 +17,20 @@ import { MatFormFieldControl } from "@angular/material/form-field";
{ provide: MatFormFieldControl, useExisting: EntityTypeSelectComponent },
],
})
export class EntityTypeSelectComponent extends BasicAutocompleteComponent<
EntityConstructor,
string
> {
export class EntityTypeSelectComponent
extends BasicAutocompleteComponent<EntityConstructor, string>
implements OnInit
{
@Input() override multi = false;
@Input() override placeholder =
$localize`:EntityTypeSelect placeholder:Select Entity Type`;

/**
* whether to include entity types without a human-readable label
* (usually only used internally for technical purposes)
*/
@Input() showInternalTypes: boolean = false;

private entityRegistry = inject(EntityRegistry);

override optionToString = (option: EntityConstructor) => option.label;
Expand All @@ -33,6 +39,7 @@ export class EntityTypeSelectComponent extends BasicAutocompleteComponent<
override ngOnInit() {
this.options = this.entityRegistry
.getEntityTypes(true)
.filter(({ value }) => this.showInternalTypes || !!value.label)
.map(({ value }) => value);

super.ngOnInit();
Expand Down
10 changes: 6 additions & 4 deletions src/app/core/entity/model/entity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ describe("Entity", () => {
expect(otherEntity).toBeInstanceOf(TestEntity);
});

it("should use entity type as default label if none is explicitly configured", () => {
it("should use label as default for labelPlural if none is explicitly configured", () => {
@DatabaseEntity("TestEntityForLabel")
class TestEntity extends Entity {}
class TestEntity extends Entity {
static override label = "X";
}

expect(TestEntity.label).toBe("TestEntityForLabel");
expect(TestEntity.labelPlural).toBe("TestEntityForLabel");
expect(TestEntity.label).toBe("X");
expect(TestEntity.labelPlural).toBe("X");
});

it("should return the route based on entity type name", () => {
Expand Down
10 changes: 1 addition & 9 deletions src/app/core/entity/model/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,7 @@ export class Entity {
/**
* human-readable name/label of the entity in the UI
*/
static get label(): string {
return this._label ?? this.ENTITY_TYPE;
}

static set label(value: string) {
this._label = value;
}

private static _label: string;
static label: string;

/**
* human-readable label for uses of plural of the entity in the UI
Expand Down
1 change: 0 additions & 1 deletion src/app/core/site-settings/site-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { ConfigurableEnumValue } from "../basic-datatypes/configurable-enum/conf
@DatabaseEntity("SiteSettings")
export class SiteSettings extends Entity {
static ENTITY_ID = "global";
static override label = $localize`Site settings`;

static create(value: Partial<SiteSettings>): SiteSettings {
return Object.assign(new SiteSettings(), value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ describe("MatchingEntitiesComponent", () => {
schoolId: testEntity.getId(),
childId: matchedEntity.getId(),
name:
"ChildSchoolRelation " + testEntity.toString() + " - matched child",
ChildSchoolRelation.label +
" " +
testEntity.toString() +
" - matched child",
} as Partial<ChildSchoolRelation>),
);
expect(saveSpy).toHaveBeenCalledWith(jasmine.any(ChildSchoolRelation));
Expand Down
2 changes: 0 additions & 2 deletions src/app/features/public-form/public-form-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { FieldGroup } from "app/core/entity-details/form/field-group";
*/
@DatabaseEntity("PublicFormConfig")
export class PublicFormConfig extends Entity {
static override label = $localize`:PublicFormConfig:Public Form`;
static override labelPlural = $localize`:PublicFormConfig:Public Forms`;
static override route = "admin/public-form";
static override toStringAttributes = ["title"];

Expand Down

0 comments on commit 393b635

Please sign in to comment.