src/app/features/matching-entities/admin-matching-entities/admin-matching-entities.component.ts

Description

Configure the details of the MatchingEntitiesComponent.

Implements

OnInit

Example

Metadata

Relationships

Index

Properties
Methods

Methods

cancel
cancel()
Returns : void
Async save
save()

Save the updated matching entities configuration.

Returns : any

Properties

Readonly alertService
Type : unknown
Default value : inject(AlertService)
Readonly configService
Type : unknown
Default value : inject(ConfigService)
Readonly entityRegistry
Type : unknown
Default value : inject(EntityRegistry)
Readonly location
Type : unknown
Default value : inject(Location)
originalConfig
Type : MatchingEntitiesConfig
sides
Type : Record<"left" | "right", MatchingSideConfig>
Default value : { left: {}, right: {}, }

Holds matching configuration for both sides of the matching entities.

import {
  Component,
  inject,
  OnInit,
  ChangeDetectionStrategy,
} from "@angular/core";
import { MatButtonModule } from "@angular/material/button";
import { ConfigService } from "../../../core/config/config.service";
import { EntityRegistry } from "../../../core/entity/database-entity.decorator";
import {
  MatchingEntitiesConfig,
  MatchingSideConfig,
} from "#src/app/features/matching-entities/matching-entities/matching-entities-config";
import { EditNewMatchActionComponent } from "#src/app/features/matching-entities/admin-matching-entities/edit-new-match-action/edit-new-match-action.component";
import { Location } from "@angular/common";
import { AlertService } from "../../../core/alerts/alert.service";
import { EditMatchingEntitySideComponent } from "./edit-matching-entity-side/edit-matching-entity-side.component";
import { ViewTitleComponent } from "#src/app/core/common-components/view-title/view-title.component";
import { ViewActionsComponent } from "#src/app/core/common-components/view-actions/view-actions.component";
import { ColumnConfig } from "../../../core/common-components/entity-form/FormConfig";
import { buildMatchingSideConfig } from "#src/app/features/matching-entities/matching-entities/matching-entities.component";

/**
 * Configure the details of the MatchingEntitiesComponent.
 */
@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,
  selector: "app-admin-matching-entities",
  imports: [
    MatButtonModule,
    EditNewMatchActionComponent,
    EditMatchingEntitySideComponent,
    ViewTitleComponent,
    ViewActionsComponent,
  ],
  templateUrl: "./admin-matching-entities.component.html",
  styleUrls: ["./admin-matching-entities.component.scss"],
})
export class AdminMatchingEntitiesComponent implements OnInit {
  readonly configService = inject(ConfigService);
  readonly entityRegistry = inject(EntityRegistry);
  readonly location = inject(Location);
  readonly alertService = inject(AlertService);

  originalConfig: MatchingEntitiesConfig;

  /**
   * Holds matching configuration for both sides of the matching entities.
   */
  sides: Record<"left" | "right", MatchingSideConfig> = {
    left: {},
    right: {},
  };

  ngOnInit(): void {
    this.originalConfig =
      this.configService.getConfig("appConfig:matching-entities") || {};
    this.sides.left = buildMatchingSideConfig(
      this.originalConfig.leftSide,
      this.originalConfig.columns,
      0,
    );
    this.sides.right = buildMatchingSideConfig(
      this.originalConfig.rightSide,
      this.originalConfig.columns,
      1,
    );
  }

  /**
   * Save the updated matching entities configuration.
   */
  async save() {
    // Validate that columnsToReview has at least one field if onMatch is configured
    if (
      this.originalConfig.onMatch &&
      (!this.originalConfig.onMatch.columnsToReview ||
        this.originalConfig.onMatch.columnsToReview.length === 0)
    ) {
      this.alertService.addWarning(
        $localize`Please select at least one field to be shown in the confirmation dialog.`,
      );
      return;
    }

    const columns = this.generateColumnsFromSides();

    const newMatchingConfig: MatchingEntitiesConfig = {
      ...this.originalConfig,
      columns,
      leftSide: { ...this.sides.left },
      rightSide: { ...this.sides.right },
      onMatch: this.originalConfig.onMatch,
    };
    delete newMatchingConfig.leftSide.columns;
    delete newMatchingConfig.rightSide.columns;

    const fullConfig = this.configService.exportConfig(true);
    fullConfig["appConfig:matching-entities"] = newMatchingConfig;
    await this.configService.saveConfig(fullConfig);
    this.alertService.addInfo($localize`Configuration updated successfully.`);

    this.location.back();
  }

  /**
   * Generate the columns array for saving by pairing left and right side columns.
   */
  private generateColumnsFromSides(): [ColumnConfig, ColumnConfig][] {
    const columns: [ColumnConfig, ColumnConfig][] = [];
    const maxLength = Math.max(
      this.sides.left.columns.length,
      this.sides.right.columns.length,
    );
    for (let i = 0; i < maxLength; i++) {
      const left = this.sides.left.columns[i] ?? undefined;
      const right = this.sides.right.columns[i] ?? undefined;
      columns.push([left, right]);
    }
    return columns;
  }

  cancel(): void {
    this.location.back();
  }
}
<app-view-title i18n>Edit Matching View</app-view-title>

<app-view-actions>
  <div class="flex-row gap-small">
    <button mat-raised-button color="accent" (click)="save()" i18n>Save</button>
    <button mat-stroked-button (click)="cancel()" i18n>Cancel</button>
  </div>
</app-view-actions>

<div class="entity-config-container padding-regular">
  <p i18n>
    Configure the details for each "side" of the matching. A side represents one
    of the two record types that will be matched.
  </p>

  <div class="flex-wrap flex-row gap-small">
    <app-edit-matching-entity-side
      class="entity-side"
      [(sideConfig)]="sides.left"
    ></app-edit-matching-entity-side>

    <app-edit-matching-entity-side
      class="entity-side"
      [(sideConfig)]="sides.right"
    ></app-edit-matching-entity-side>
  </div>

  <div class="padding-top-regular">
    <app-new-match-action
      [(value)]="originalConfig.onMatch"
      [leftEntityType]="sides.left.entityType"
      [rightEntityType]="sides.right.entityType"
    ></app-new-match-action>
  </div>
</div>

./admin-matching-entities.component.scss

@use "@angular/material" as mat;
@use "variables/sizes";

.entity-config-container {
  background: #f5f5f5;
  border-radius: 8px;
}

.entity-side {
  flex: 1;
  margin: 0;
  background-color: #fff;

  border-radius: 4px;
  @include mat.elevation(2);
  padding: sizes.$regular;

  .columns-list {
    display: flex;
    flex-direction: column;
  }
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""